pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_SimpleQueue.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_SIMPLEQUEUE_H
00040 #define PLONK_SIMPLEQUEUE_H
00041 
00042 #include "../core/plonk_CoreForwardDeclarations.h"
00043 #include "plonk_ContainerForwardDeclarations.h"
00044 
00045 #include "../core/plonk_SmartPointer.h"
00046 #include "../core/plonk_WeakPointer.h"
00047 
00048 template<class ValueType>                                               
00049 class SimpleQueueInternal : public SmartPointer
00050 {
00051 public:
00052     typedef SimpleQueue<ValueType>        StackType;
00053 
00054     SimpleQueueInternal() throw()
00055     {
00056         pl_SimpleQueue_Init (&queue);
00057         pl_SimpleQueue_SetFreeElementDataFunction (&queue, SimpleQueueInternal::freeElement);
00058     }
00059     
00060     ~SimpleQueueInternal()
00061     {
00062         ResultCode result;
00063         
00064         result = pl_SimpleQueue_Clear (&queue);
00065         plonk_assert (result == PlankResult_OK);
00066 
00067         result = pl_SimpleQueue_DeInit (&queue);
00068         plonk_assert (result == PlankResult_OK);
00069         
00070 #ifndef PLONK_DEBUG
00071         (void)result;
00072 #endif
00073     }
00074     
00075     void push (ValueType const& value) throw()
00076     {
00077         PlankSimpleQueueElementRef element = pl_SimpleQueueElement_CreateAndInit();
00078         plonk_assert (element != 0);
00079         pl_SimpleQueueElement_SetData (element, new ValueType (value));
00080         ResultCode result = pl_SimpleQueue_Push (&queue, element);
00081         plonk_assert (result == PlankResult_OK);
00082 #ifndef PLONK_DEBUG
00083         (void)result;
00084 #endif
00085     }
00086     
00087     ValueType pop() throw()
00088     {
00089         ValueType returnValue;
00090         
00091         PlankSimpleQueueElementRef element;
00092         ResultCode result = pl_SimpleQueue_Pop (&queue, &element);
00093         plonk_assert (result == PlankResult_OK);
00094 
00095         if (element != 0)
00096         {
00097             ValueType* valuePtr = static_cast <ValueType*> (pl_SimpleQueueElement_GetData (element));
00098             
00099             if (valuePtr != 0)
00100             {
00101                 returnValue = *valuePtr;
00102                 delete valuePtr;
00103             }
00104             
00105             result = pl_SimpleQueueElement_Destroy (element);
00106             plonk_assert (result == PlankResult_OK);
00107         }
00108         
00109 #ifndef PLONK_DEBUG
00110         (void)result;
00111 #endif
00112         
00113         return returnValue;
00114     }
00115     
00116     inline LongLong length() throw()
00117     {
00118         return pl_SimpleQueue_GetSize (&queue);
00119     }
00120     
00121     friend class SimpleQueue<ValueType>;
00122     
00123 private:
00124     PlankSimpleQueue queue;
00125     
00126     static ResultCode freeElement (Pointer data)
00127     {
00128         delete static_cast<ValueType*> (data);
00129         return PlankResult_OK;
00130     }
00131 };
00132 
00133 
00134 template<class ValueType>                                               
00135 class SimpleQueueInternal<ValueType*> : public SmartPointer
00136 {
00137 public:
00138     typedef SimpleQueue<ValueType*>        StackType;
00139     
00140     SimpleQueueInternal() throw()
00141     {
00142         pl_SimpleQueue_Init (&queue);
00143     }
00144     
00145     ~SimpleQueueInternal()
00146     {
00147         plonk_assert (pl_SimpleQueue_GetSize (&queue) == 0); // pointers were left in the queue
00148         pl_SimpleQueue_Clear (&queue);
00149         pl_SimpleQueue_DeInit (&queue);        
00150     }
00151         
00152     void push (ValueType* const value) throw()
00153     {
00154         PlankSimpleQueueElementRef element = pl_SimpleQueueElement_CreateAndInit();
00155         plonk_assert (element != 0);
00156         pl_SimpleQueueElement_SetData (element, value);
00157         ResultCode result = pl_SimpleQueue_Push (&queue, element);
00158         plonk_assert (result == PlankResult_OK);
00159 #ifndef PLONK_DEBUG
00160         (void)result;
00161 #endif
00162     }
00163     
00164     ValueType* pop() throw()
00165     {
00166         ValueType* returnValue;
00167         
00168         PlankSimpleQueueElementRef element;
00169         ResultCode result = pl_SimpleQueue_Pop (&queue, &element);
00170         plonk_assert (result == PlankResult_OK);
00171         
00172         if (element != 0)
00173         {
00174             returnValue = static_cast <ValueType*> (pl_SimpleQueueElement_GetData (element));                        
00175             result = pl_SimpleQueueElement_Destroy (element);
00176             plonk_assert (result == PlankResult_OK);
00177         }
00178         
00179 #ifndef PLONK_DEBUG
00180         (void)result;
00181 #endif
00182         
00183         return returnValue;
00184     }
00185     
00186     inline LongLong length() throw()
00187     {
00188         return pl_SimpleQueue_GetSize (&queue);
00189     }
00190     
00191     friend class SimpleQueue<ValueType*>;
00192     
00193 private:
00194     PlankSimpleQueue queue;
00195 };
00196 
00197 
00198 
00199 //------------------------------------------------------------------------------
00200 
00202 template<class ValueType>                                               
00203 class SimpleQueue : public SmartPointerContainer<SimpleQueueInternal<ValueType> >
00204 {
00205 public:
00206     typedef SimpleQueueInternal<ValueType>    Internal;
00207     typedef SmartPointerContainer<Internal>   Base;
00208     typedef WeakPointerContainer<SimpleQueue> Weak;    
00209 
00210     SimpleQueue()
00211     :   Base (new Internal())
00212     {
00213     }
00214     
00215     explicit SimpleQueue (Internal* internalToUse) throw() 
00216         :       Base (internalToUse)
00217         {
00218         }
00219     
00223     static SimpleQueue fromWeak (Weak const& weak) throw()
00224     {
00225         return weak.fromWeak();
00226     }    
00227     
00230     SimpleQueue (SimpleQueue const& copy) throw()
00231     :   Base (static_cast<Base const&> (copy))
00232     {
00233     }
00234     
00235     SimpleQueue (Dynamic const& other) throw()
00236     :   Base (other.as<SimpleQueue>().getInternal())
00237     {
00238     }    
00239     
00241     SimpleQueue& operator= (SimpleQueue const& other) throw()
00242         {
00243                 if (this != &other)
00244             this->setInternal (other.getInternal());
00245         
00246         return *this;
00247         }
00248     
00249     void push (ValueType const& value) throw()
00250     {
00251         this->getInternal()->push (value);
00252     }
00253     
00254     ValueType pop() throw()
00255     {
00256         return this->getInternal()->pop();
00257     }
00258     
00259     inline LongLong length() throw()
00260     {
00261         return this->getInternal()->length();
00262     }
00263     
00264     PLONK_OBJECTARROWOPERATOR(SimpleQueue);
00265 
00266 };
00267 
00268 
00269 #endif // PLONK_SIMPLEQUEUE_H
 All Classes Functions Typedefs Enumerations Enumerator Properties