xref: /AOO41X/main/salhelper/inc/salhelper/queue.hxx (revision 8a25e0a89046e766b2f8dcc3b5f33bc291c2c738)
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_QUEUE_HXX_
25 #define _SALHELPER_QUEUE_HXX_
26 
27 #include <sal/types.h>
28 #include <osl/diagnose.h>
29 #include <osl/mutex.hxx>
30 #ifndef _OSL_SEMAPHOR_HXX_
31 #include <osl/semaphor.hxx>
32 #endif
33 
34 #ifndef __LIST__
35 #include <list>
36 #endif
37 
38 namespace salhelper
39 {
40 
41 //----------------------------------------------------------------------------
42 
43 #ifndef SALHELPER_COPYCTOR_API
44 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
45 #endif
46 
47 //----------------------------------------------------------------------------
48 
49 template<class element_type>
50 class QueueBase : protected std::list<element_type>
51 {
52     /** Representation.
53      */
54     osl::Mutex m_aMutex;
55 
56     /** Not implemented.
57      */
58     SALHELPER_COPYCTOR_API(QueueBase<element_type>);
59 
60 public:
QueueBase()61     inline QueueBase()
62     {}
63 
~QueueBase()64     inline ~QueueBase()
65     {
66         erase (this->begin(), this->end());
67     }
68 
put(const element_type & element)69     inline void put (const element_type& element)
70     {
71         osl::MutexGuard aGuard (m_aMutex);
72         push_back (element);
73     }
74 
get()75     inline element_type get()
76     {
77         element_type element;
78 
79         osl::MutexGuard aGuard (m_aMutex);
80         if (!this->empty())
81         {
82             element = this->front();
83             this->pop_front();
84         }
85 
86         return (element);
87     }
88 };
89 
90 //----------------------------------------------------------------------------
91 
92 /** Queue.
93 
94     @deprecated
95     Must not be used, as it internally uses unnamed semaphores, which are not
96     supported on Mac OS X.
97 */
98 template<class element_type>
99 class Queue : protected QueueBase<element_type>
100 {
101     /** Representation.
102      */
103     osl::Semaphore m_aNotEmpty;
104 
105     /** Not implemented.
106      */
107     SALHELPER_COPYCTOR_API(Queue<element_type>);
108 
109 public:
Queue()110     inline Queue() : m_aNotEmpty (static_cast< sal_uInt32 >(0))
111     {}
112 
~Queue()113     inline ~Queue()
114     {}
115 
put(const element_type & element)116     inline void put (const element_type& element)
117     {
118         QueueBase<element_type>::put (element);
119         m_aNotEmpty.release();
120     }
121 
get()122     inline element_type get()
123     {
124         element_type element;
125 
126         m_aNotEmpty.acquire();
127         element = QueueBase<element_type>::get();
128 
129         return (element);
130     }
131 };
132 
133 //----------------------------------------------------------------------------
134 
135 /** Bounded queue.
136 
137     @deprecated
138     Must not be used, as it internally uses unnamed semaphores, which are not
139     supported on Mac OS X.
140 */
141 template<class element_type>
142 class BoundedQueue : protected Queue<element_type>
143 {
144     /** Representation.
145      */
146     osl::Semaphore m_aNotFull;
147 
148     /** Not implemented.
149      */
150     SALHELPER_COPYCTOR_API(BoundedQueue<element_type>);
151 
152 public:
BoundedQueue(sal_uInt32 capacity)153     inline BoundedQueue (sal_uInt32 capacity) : m_aNotFull (capacity)
154     {
155         OSL_POSTCOND(capacity, "BoundedQueue:BoundedQueue(): no capacity");
156     }
157 
~BoundedQueue()158     inline ~BoundedQueue()
159     {}
160 
put(const element_type & element)161     inline void put (const element_type& element)
162     {
163         m_aNotFull.acquire();
164         Queue<element_type>::put (element);
165     }
166 
get()167     inline element_type get()
168     {
169         element_type element;
170 
171         element = Queue<element_type>::get();
172         m_aNotFull.release();
173 
174         return (element);
175     }
176 };
177 
178 //----------------------------------------------------------------------------
179 
180 } // namespace salhelper
181 
182 #endif /* !_SALHELPER_QUEUE_HXX_ */
183