My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
queue.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _SALHELPER_QUEUE_HXX_
30 #define _SALHELPER_QUEUE_HXX_
31 
32 #include <sal/types.h>
33 #include <osl/diagnose.h>
34 #include <osl/mutex.hxx>
35 #include <osl/semaphor.hxx>
36 
37 #ifndef __LIST__
38 #include <list>
39 #endif
40 
41 namespace salhelper
42 {
43 
44 //----------------------------------------------------------------------------
45 
46 #ifndef SALHELPER_COPYCTOR_API
47 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
48 #endif
49 
50 //----------------------------------------------------------------------------
51 
52 template<class element_type>
53 class QueueBase : protected std::list<element_type>
54 {
57  osl::Mutex m_aMutex;
58 
61  SALHELPER_COPYCTOR_API(QueueBase<element_type>);
62 
63 public:
64  inline QueueBase()
65  {}
66 
67  inline ~QueueBase()
68  {
69  erase (this->begin(), this->end());
70  }
71 
72  inline void put (const element_type& element)
73  {
74  osl::MutexGuard aGuard (m_aMutex);
75  push_back (element);
76  }
77 
78  inline element_type get()
79  {
80  element_type element;
81 
82  osl::MutexGuard aGuard (m_aMutex);
83  if (!this->empty())
84  {
85  element = this->front();
86  this->pop_front();
87  }
88 
89  return (element);
90  }
91 };
92 
93 //----------------------------------------------------------------------------
94 
101 template<class element_type>
102 class Queue : protected QueueBase<element_type>
103 {
106  osl::Semaphore m_aNotEmpty;
107 
110  SALHELPER_COPYCTOR_API(Queue<element_type>);
111 
112 public:
113  inline Queue() : m_aNotEmpty (static_cast< sal_uInt32 >(0))
114  {}
115 
116  inline ~Queue()
117  {}
118 
119  inline void put (const element_type& element)
120  {
122  m_aNotEmpty.release();
123  }
124 
125  inline element_type get()
126  {
127  element_type element;
128 
129  m_aNotEmpty.acquire();
130  element = QueueBase<element_type>::get();
131 
132  return (element);
133  }
134 };
135 
136 //----------------------------------------------------------------------------
137 
144 template<class element_type>
145 class BoundedQueue : protected Queue<element_type>
146 {
149  osl::Semaphore m_aNotFull;
150 
153  SALHELPER_COPYCTOR_API(BoundedQueue<element_type>);
154 
155 public:
156  inline BoundedQueue (sal_uInt32 capacity) : m_aNotFull (capacity)
157  {
158  OSL_POSTCOND(capacity, "BoundedQueue:BoundedQueue(): no capacity");
159  }
160 
161  inline ~BoundedQueue()
162  {}
163 
164  inline void put (const element_type& element)
165  {
166  m_aNotFull.acquire();
167  Queue<element_type>::put (element);
168  }
169 
170  inline element_type get()
171  {
172  element_type element;
173 
174  element = Queue<element_type>::get();
175  m_aNotFull.release();
176 
177  return (element);
178  }
179 };
180 
181 //----------------------------------------------------------------------------
182 
183 } // namespace salhelper
184 
185 #endif /* !_SALHELPER_QUEUE_HXX_ */
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */