xref: /aoo42x/main/salhelper/inc/salhelper/queue.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SALHELPER_QUEUE_HXX_
29 #define _SALHELPER_QUEUE_HXX_
30 
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
34 #ifndef _OSL_SEMAPHOR_HXX_
35 #include <osl/semaphor.hxx>
36 #endif
37 
38 #ifndef __LIST__
39 #include <list>
40 #endif
41 
42 namespace salhelper
43 {
44 
45 //----------------------------------------------------------------------------
46 
47 #ifndef SALHELPER_COPYCTOR_API
48 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
49 #endif
50 
51 //----------------------------------------------------------------------------
52 
53 template<class element_type>
54 class QueueBase : protected std::list<element_type>
55 {
56 	/** Representation.
57 	 */
58 	osl::Mutex m_aMutex;
59 
60 	/** Not implemented.
61 	 */
62 	SALHELPER_COPYCTOR_API(QueueBase<element_type>);
63 
64 public:
65 	inline QueueBase()
66 	{}
67 
68 	inline ~QueueBase()
69 	{
70 		erase (this->begin(), this->end());
71 	}
72 
73 	inline void put (const element_type& element)
74 	{
75 		osl::MutexGuard aGuard (m_aMutex);
76 		push_back (element);
77 	}
78 
79 	inline element_type get()
80 	{
81 		element_type element;
82 
83 		osl::MutexGuard aGuard (m_aMutex);
84 		if (!this->empty())
85 		{
86 			element = this->front();
87 			this->pop_front();
88 		}
89 
90 		return (element);
91 	}
92 };
93 
94 //----------------------------------------------------------------------------
95 
96 /** Queue.
97 
98     @deprecated
99     Must not be used, as it internally uses unnamed semaphores, which are not
100     supported on Mac OS X.
101 */
102 template<class element_type>
103 class Queue : protected QueueBase<element_type>
104 {
105 	/** Representation.
106 	 */
107 	osl::Semaphore m_aNotEmpty;
108 
109 	/** Not implemented.
110 	 */
111 	SALHELPER_COPYCTOR_API(Queue<element_type>);
112 
113 public:
114 	inline Queue() : m_aNotEmpty (static_cast< sal_uInt32 >(0))
115 	{}
116 
117 	inline ~Queue()
118 	{}
119 
120 	inline void put (const element_type& element)
121 	{
122 		QueueBase<element_type>::put (element);
123 		m_aNotEmpty.release();
124 	}
125 
126 	inline element_type get()
127 	{
128 		element_type element;
129 
130 		m_aNotEmpty.acquire();
131 		element = QueueBase<element_type>::get();
132 
133 		return (element);
134 	}
135 };
136 
137 //----------------------------------------------------------------------------
138 
139 /** Bounded queue.
140 
141     @deprecated
142     Must not be used, as it internally uses unnamed semaphores, which are not
143     supported on Mac OS X.
144 */
145 template<class element_type>
146 class BoundedQueue : protected Queue<element_type>
147 {
148 	/** Representation.
149 	 */
150 	osl::Semaphore m_aNotFull;
151 
152 	/** Not implemented.
153 	 */
154 	SALHELPER_COPYCTOR_API(BoundedQueue<element_type>);
155 
156 public:
157 	inline BoundedQueue (sal_uInt32 capacity) : m_aNotFull (capacity)
158 	{
159 		OSL_POSTCOND(capacity, "BoundedQueue:BoundedQueue(): no capacity");
160 	}
161 
162 	inline ~BoundedQueue()
163 	{}
164 
165 	inline void put (const element_type& element)
166 	{
167 		m_aNotFull.acquire();
168 		Queue<element_type>::put (element);
169 	}
170 
171 	inline element_type get()
172 	{
173 		element_type element;
174 
175 		element = Queue<element_type>::get();
176 		m_aNotFull.release();
177 
178 		return (element);
179 	}
180 };
181 
182 //----------------------------------------------------------------------------
183 
184 } // namespace salhelper
185 
186 #endif /* !_SALHELPER_QUEUE_HXX_ */
187