1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SALHELPER_FUTUREQUEUE_HXX_
25 #define _SALHELPER_FUTUREQUEUE_HXX_
26 
27 #include <sal/types.h>
28 #include <rtl/ref.hxx>
29 #include <osl/mutex.hxx>
30 #include <salhelper/future.hxx>
31 #include <salhelper/queue.hxx>
32 
33 namespace salhelper
34 {
35 
36 //----------------------------------------------------------------------------
37 
38 #ifndef SALHELPER_COPYCTOR_API
39 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
40 #endif
41 
42 //----------------------------------------------------------------------------
43 
44 template<class element_type>
45 class FutureQueue : protected osl::Mutex
46 {
47 	/** Representation.
48 	 */
49 	typedef salhelper::Future<element_type> future_type;
50 
51 	salhelper::QueueBase< rtl::Reference<future_type> > m_aDelayed;
52 	salhelper::QueueBase< rtl::Reference<future_type> > m_aPresent;
53 
54 	/** Not implemented.
55 	 */
56 	SALHELPER_COPYCTOR_API(FutureQueue<element_type>);
57 
58 public:
59 	/** Construction.
60 	 */
FutureQueue()61 	inline FutureQueue()
62 	{}
63 
64 	/** Destruction.
65 	 */
~FutureQueue()66 	inline ~FutureQueue()
67 	{}
68 
69 	/** Enqueue element at queue tail.
70 	 */
put(const element_type & element)71 	inline void put (const element_type& element)
72 	{
73 		osl::MutexGuard aGuard (*this);
74 
75 		rtl::Reference<future_type> xFuture (m_aDelayed.get());
76 		if (!xFuture.is())
77 		{
78 			xFuture = new future_type();
79 			m_aPresent.put (xFuture);
80 		}
81 		xFuture->set (element);
82 	}
83 
84 	/** Dequeue a future to element at queue head.
85 	 */
get()86 	inline rtl::Reference< salhelper::Future<element_type> > get()
87 	{
88 		osl::MutexGuard aGuard (*this);
89 
90 		rtl::Reference<future_type> xFuture (m_aPresent.get());
91 		if (!xFuture.is())
92 		{
93 			xFuture = new future_type();
94 			m_aDelayed.put (xFuture);
95 		}
96 		return (xFuture);
97 	}
98 };
99 
100 //----------------------------------------------------------------------------
101 
102 } // namespace salhelper
103 
104 #endif /* !_SALHELPER_FUTUREQUEUE */
105