pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_DynamicContainer.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_DYNAMICCONTAINER_H
00040 #define PLONK_DYNAMICCONTAINER_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 #include "../core/plonk_TypeUtility.h"
00049 
00050 
00051 class DynamicInternal : public SmartPointer
00052 {
00053 public:
00054     typedef SmartPointerContainer<SmartPointer> GenericContainer;
00055     
00056     DynamicInternal() throw()
00057     :   item (static_cast<SmartPointer*> (0)),
00058         typeCode (0)
00059     {
00060     }
00061     
00062     ~DynamicInternal()
00063     {
00064     }
00065     
00066     template<class ContainerType>
00067     DynamicInternal (ContainerType const& other) throw()
00068     :   item (static_cast<SmartPointer*> (other.getInternal())),
00069         typeCode (TypeUtility<ContainerType>::getTypeCode())
00070     {
00071     }
00072     
00073     template<class ContainerType>
00074     void setItem (ContainerType const& other) throw()
00075     {
00076         typeCode = 0; // unknown
00077         AtomicOps::memoryBarrier();
00078         item = static_cast<SmartPointer*> (other.getInternal());
00079         typeCode = TypeUtility<ContainerType>::getTypeCode();
00080     }
00081     
00082     inline const GenericContainer& getItem() const throw()
00083     {
00084         return item;
00085     }
00086     
00087     inline GenericContainer& getItem() throw()
00088     {
00089         return item;
00090     }    
00091     
00092     inline int getTypeCode() const throw()
00093     {
00094         return typeCode;
00095     }
00096     
00097     inline bool isNull() const throw()
00098     {
00099         return (typeCode == 0) && (item.getInternal() == 0);
00100     }
00101     
00102     inline bool isNotNull() const throw()
00103     {
00104         return ! this->isNull();
00105     }
00106     
00107 private:
00108     GenericContainer item;
00109     int typeCode;
00110 };
00111 
00113 class Dynamic : public SmartPointerContainer<DynamicInternal>
00114 {
00115 public:
00116     typedef SmartPointerContainer<DynamicInternal>  Base;
00117     typedef SmartPointerContainer<SmartPointer>     GenericContainer;
00118     typedef WeakPointerContainer<Dynamic>           Weak;
00119     typedef DynamicInternal                         Internal;
00120 
00121     Dynamic() throw();
00122     
00123     Dynamic (Dynamic const& copy) throw();
00124     Dynamic& operator= (Dynamic const& other) throw();
00125 
00126     template<class ContainerType>
00127     Dynamic (ContainerType const& other) throw()
00128     :   Base (new DynamicInternal (other))
00129     {
00130     }
00131     
00132     explicit Dynamic (Internal* internalToUse) throw();
00133     static Dynamic fromWeak (Weak const& weak) throw();
00134         
00135     template<class ContainerType>
00136     void setItem (ContainerType const& other) throw()
00137     {
00138         this->getInternal()->setItem (other);
00139     }
00140     
00141     //inline Dynamic containerCopy() const throw()            { return *this; }
00142     inline const GenericContainer& getItem() const throw()  { return this->getInternal()->getItem(); }
00143     inline GenericContainer& getItem() throw()              { return this->getInternal()->getItem(); }    
00144     inline int getTypeCode() const throw()                  { return this->getInternal()->getTypeCode(); }
00145 #if !PLANK_APPLE_LLVM
00146     inline operator const Dynamic& () const throw()         { return *this; }        
00147     inline operator Dynamic& () throw()                     { return *this; } 
00148 #endif
00149     inline bool isItemNull() const throw()                  { return this->getInternal()->isNull(); }
00150     inline bool isItemNotNull() const throw()               { return this->getInternal()->isNotNull(); }
00151         
00152     template<class ContainerType>
00153     inline const ContainerType& asUnchecked() const throw()
00154     {              
00155         plonk_assert (this->isItemNotNull());
00156         plonk_assert (TypeUtility<ContainerType>::getTypeCode() == this->getTypeCode());
00157         return reinterpret_cast<const ContainerType&> (this->getItem());
00158     }
00159     
00160     template<class ContainerType>
00161     inline ContainerType& asUnchecked() throw()
00162     {              
00163         plonk_assert (this->isItemNotNull());
00164         plonk_assert (TypeUtility<ContainerType>::getTypeCode() == this->getTypeCode());
00165         return reinterpret_cast<ContainerType&> (this->getItem());
00166     }
00167         
00168     template<class ContainerType>
00169     inline ContainerType as() const throw()
00170     {              
00171         if ((this->isItemNull()) || (TypeUtility<ContainerType>::getTypeCode() != this->getTypeCode()))
00172             return ContainerType();
00173         else
00174             return reinterpret_cast<const ContainerType&> (this->getItem());
00175     }
00176         
00177     Dynamic getMaxBlockSize() const throw();    
00178     Dynamic getMaxSampleRate() const throw();
00179     int getNumChannels() const throw();
00180     Dynamic getChannel (const int index) throw();
00181     
00182     inline static const Dynamic& getNull() throw()
00183         {
00184                 static Dynamic null;
00185                 return null;
00186         }
00187     
00188     template<class OtherType>
00189     bool operator== (OtherType const& other) const throw()
00190     {
00191         if (this->getTypeCode() != TypeUtility<OtherType>::getTypeCode())
00192             return false;
00193         
00194         return this->as<OtherType>() == other;
00195     }
00196     
00197     template<class OtherType>
00198     inline bool operator!= (OtherType const& other) const throw()
00199     {
00200         return !this->operator== (other);
00201     }
00202     
00203     PLONK_OBJECTARROWOPERATOR(Dynamic);
00204 
00205 private:
00206     template<class ContainerType> operator const ContainerType& () const;
00207     template<class ContainerType> operator ContainerType& ();
00208 };
00209 
00210 #endif // PLONK_DYNAMICCONTAINER_H
 All Classes Functions Typedefs Enumerations Enumerator Properties