xref: /aoo42x/main/salhelper/inc/salhelper/queue.hxx (revision 8a25e0a8)
1*8a25e0a8SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*8a25e0a8SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*8a25e0a8SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*8a25e0a8SAndrew Rist  * distributed with this work for additional information
6*8a25e0a8SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*8a25e0a8SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*8a25e0a8SAndrew Rist  * "License"); you may not use this file except in compliance
9*8a25e0a8SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8a25e0a8SAndrew Rist  *
11*8a25e0a8SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8a25e0a8SAndrew Rist  *
13*8a25e0a8SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*8a25e0a8SAndrew Rist  * software distributed under the License is distributed on an
15*8a25e0a8SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8a25e0a8SAndrew Rist  * KIND, either express or implied.  See the License for the
17*8a25e0a8SAndrew Rist  * specific language governing permissions and limitations
18*8a25e0a8SAndrew Rist  * under the License.
19*8a25e0a8SAndrew Rist  *
20*8a25e0a8SAndrew Rist  *************************************************************/
21*8a25e0a8SAndrew Rist 
22*8a25e0a8SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _SALHELPER_QUEUE_HXX_
25cdf0e10cSrcweir #define _SALHELPER_QUEUE_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <osl/mutex.hxx>
30cdf0e10cSrcweir #ifndef _OSL_SEMAPHOR_HXX_
31cdf0e10cSrcweir #include <osl/semaphor.hxx>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #ifndef __LIST__
35cdf0e10cSrcweir #include <list>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace salhelper
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //----------------------------------------------------------------------------
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #ifndef SALHELPER_COPYCTOR_API
44cdf0e10cSrcweir #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //----------------------------------------------------------------------------
48cdf0e10cSrcweir 
49cdf0e10cSrcweir template<class element_type>
50cdf0e10cSrcweir class QueueBase : protected std::list<element_type>
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	/** Representation.
53cdf0e10cSrcweir 	 */
54cdf0e10cSrcweir 	osl::Mutex m_aMutex;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	/** Not implemented.
57cdf0e10cSrcweir 	 */
58cdf0e10cSrcweir 	SALHELPER_COPYCTOR_API(QueueBase<element_type>);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir public:
QueueBase()61cdf0e10cSrcweir 	inline QueueBase()
62cdf0e10cSrcweir 	{}
63cdf0e10cSrcweir 
~QueueBase()64cdf0e10cSrcweir 	inline ~QueueBase()
65cdf0e10cSrcweir 	{
66cdf0e10cSrcweir 		erase (this->begin(), this->end());
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir 
put(const element_type & element)69cdf0e10cSrcweir 	inline void put (const element_type& element)
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		osl::MutexGuard aGuard (m_aMutex);
72cdf0e10cSrcweir 		push_back (element);
73cdf0e10cSrcweir 	}
74cdf0e10cSrcweir 
get()75cdf0e10cSrcweir 	inline element_type get()
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		element_type element;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 		osl::MutexGuard aGuard (m_aMutex);
80cdf0e10cSrcweir 		if (!this->empty())
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			element = this->front();
83cdf0e10cSrcweir 			this->pop_front();
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 		return (element);
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir };
89cdf0e10cSrcweir 
90cdf0e10cSrcweir //----------------------------------------------------------------------------
91cdf0e10cSrcweir 
92cdf0e10cSrcweir /** Queue.
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     @deprecated
95cdf0e10cSrcweir     Must not be used, as it internally uses unnamed semaphores, which are not
96cdf0e10cSrcweir     supported on Mac OS X.
97cdf0e10cSrcweir */
98cdf0e10cSrcweir template<class element_type>
99cdf0e10cSrcweir class Queue : protected QueueBase<element_type>
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	/** Representation.
102cdf0e10cSrcweir 	 */
103cdf0e10cSrcweir 	osl::Semaphore m_aNotEmpty;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	/** Not implemented.
106cdf0e10cSrcweir 	 */
107cdf0e10cSrcweir 	SALHELPER_COPYCTOR_API(Queue<element_type>);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir public:
Queue()110cdf0e10cSrcweir 	inline Queue() : m_aNotEmpty (static_cast< sal_uInt32 >(0))
111cdf0e10cSrcweir 	{}
112cdf0e10cSrcweir 
~Queue()113cdf0e10cSrcweir 	inline ~Queue()
114cdf0e10cSrcweir 	{}
115cdf0e10cSrcweir 
put(const element_type & element)116cdf0e10cSrcweir 	inline void put (const element_type& element)
117cdf0e10cSrcweir 	{
118cdf0e10cSrcweir 		QueueBase<element_type>::put (element);
119cdf0e10cSrcweir 		m_aNotEmpty.release();
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 
get()122cdf0e10cSrcweir 	inline element_type get()
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		element_type element;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		m_aNotEmpty.acquire();
127cdf0e10cSrcweir 		element = QueueBase<element_type>::get();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		return (element);
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir };
132cdf0e10cSrcweir 
133cdf0e10cSrcweir //----------------------------------------------------------------------------
134cdf0e10cSrcweir 
135cdf0e10cSrcweir /** Bounded queue.
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     @deprecated
138cdf0e10cSrcweir     Must not be used, as it internally uses unnamed semaphores, which are not
139cdf0e10cSrcweir     supported on Mac OS X.
140cdf0e10cSrcweir */
141cdf0e10cSrcweir template<class element_type>
142cdf0e10cSrcweir class BoundedQueue : protected Queue<element_type>
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	/** Representation.
145cdf0e10cSrcweir 	 */
146cdf0e10cSrcweir 	osl::Semaphore m_aNotFull;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	/** Not implemented.
149cdf0e10cSrcweir 	 */
150cdf0e10cSrcweir 	SALHELPER_COPYCTOR_API(BoundedQueue<element_type>);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir public:
BoundedQueue(sal_uInt32 capacity)153cdf0e10cSrcweir 	inline BoundedQueue (sal_uInt32 capacity) : m_aNotFull (capacity)
154cdf0e10cSrcweir 	{
155cdf0e10cSrcweir 		OSL_POSTCOND(capacity, "BoundedQueue:BoundedQueue(): no capacity");
156cdf0e10cSrcweir 	}
157cdf0e10cSrcweir 
~BoundedQueue()158cdf0e10cSrcweir 	inline ~BoundedQueue()
159cdf0e10cSrcweir 	{}
160cdf0e10cSrcweir 
put(const element_type & element)161cdf0e10cSrcweir 	inline void put (const element_type& element)
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		m_aNotFull.acquire();
164cdf0e10cSrcweir 		Queue<element_type>::put (element);
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir 
get()167cdf0e10cSrcweir 	inline element_type get()
168cdf0e10cSrcweir 	{
169cdf0e10cSrcweir 		element_type element;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 		element = Queue<element_type>::get();
172cdf0e10cSrcweir 		m_aNotFull.release();
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		return (element);
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir };
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //----------------------------------------------------------------------------
179cdf0e10cSrcweir 
180cdf0e10cSrcweir } // namespace salhelper
181cdf0e10cSrcweir 
182cdf0e10cSrcweir #endif /* !_SALHELPER_QUEUE_HXX_ */
183