UDK 3.2.7 C/C++ API Reference
salhelper/queue.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _SALHELPER_QUEUE_HXX_
00030 #define _SALHELPER_QUEUE_HXX_
00031 
00032 #include <sal/types.h>
00033 #include <osl/diagnose.h>
00034 #include <osl/mutex.hxx>
00035 #include <osl/semaphor.hxx>
00036 
00037 #ifndef __LIST__
00038 #include <list>
00039 #endif
00040 
00041 namespace salhelper
00042 {
00043 
00044 //----------------------------------------------------------------------------
00045 
00046 #ifndef SALHELPER_COPYCTOR_API
00047 #define SALHELPER_COPYCTOR_API(C) C (const C&); C& operator= (const C&)
00048 #endif
00049 
00050 //----------------------------------------------------------------------------
00051 
00052 template<class element_type>
00053 class QueueBase : protected std::list<element_type>
00054 {
00057     osl::Mutex m_aMutex;
00058 
00061     SALHELPER_COPYCTOR_API(QueueBase<element_type>);
00062 
00063 public:
00064     inline QueueBase()
00065     {}
00066 
00067     inline ~QueueBase()
00068     {
00069         erase (this->begin(), this->end());
00070     }
00071 
00072     inline void put (const element_type& element)
00073     {
00074         osl::MutexGuard aGuard (m_aMutex);
00075         push_back (element);
00076     }
00077 
00078     inline element_type get()
00079     {
00080         element_type element;
00081 
00082         osl::MutexGuard aGuard (m_aMutex);
00083         if (!this->empty())
00084         {
00085             element = this->front();
00086             this->pop_front();
00087         }
00088 
00089         return (element);
00090     }
00091 };
00092 
00093 //----------------------------------------------------------------------------
00094 
00101 template<class element_type>
00102 class Queue : protected QueueBase<element_type>
00103 {
00106     osl::Semaphore m_aNotEmpty;
00107 
00110     SALHELPER_COPYCTOR_API(Queue<element_type>);
00111 
00112 public:
00113     inline Queue() : m_aNotEmpty (static_cast< sal_uInt32 >(0))
00114     {}
00115 
00116     inline ~Queue()
00117     {}
00118 
00119     inline void put (const element_type& element)
00120     {
00121         QueueBase<element_type>::put (element);
00122         m_aNotEmpty.release();
00123     }
00124 
00125     inline element_type get()
00126     {
00127         element_type element;
00128 
00129         m_aNotEmpty.acquire();
00130         element = QueueBase<element_type>::get();
00131 
00132         return (element);
00133     }
00134 };
00135 
00136 //----------------------------------------------------------------------------
00137 
00144 template<class element_type>
00145 class BoundedQueue : protected Queue<element_type>
00146 {
00149     osl::Semaphore m_aNotFull;
00150 
00153     SALHELPER_COPYCTOR_API(BoundedQueue<element_type>);
00154 
00155 public:
00156     inline BoundedQueue (sal_uInt32 capacity) : m_aNotFull (capacity)
00157     {
00158         OSL_POSTCOND(capacity, "BoundedQueue:BoundedQueue(): no capacity");
00159     }
00160 
00161     inline ~BoundedQueue()
00162     {}
00163 
00164     inline void put (const element_type& element)
00165     {
00166         m_aNotFull.acquire();
00167         Queue<element_type>::put (element);
00168     }
00169 
00170     inline element_type get()
00171     {
00172         element_type element;
00173 
00174         element = Queue<element_type>::get();
00175         m_aNotFull.release();
00176 
00177         return (element);
00178     }
00179 };
00180 
00181 //----------------------------------------------------------------------------
00182 
00183 } // namespace salhelper
00184 
00185 #endif /* !_SALHELPER_QUEUE_HXX_ */
00186 
00187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines