pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_SmartPointer.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_SMARTPOINTER_H
00040 #define PLONK_SMARTPOINTER_H
00041 
00042 #include "plonk_CoreForwardDeclarations.h"
00043 #include "../containers/plonk_ContainerForwardDeclarations.h"
00044 #include "../containers/plonk_Atomic.h"
00045 
00046 #define PLONK_SMARTPOINTER_DEBUG 0
00047 #define PLONK_SMARTPOINTER_DEBUGLOG 0
00048 
00049 #ifndef PLONK_SMARTPOINTER_DEBUG
00050     #ifdef PLONK_DEBUG
00051         #define PLONK_SMARTPOINTER_DEBUG 1
00052         #ifndef PLONK_SMARTPOINTER_DEBUGLOG
00053             #define PLONK_SMARTPOINTER_DEBUGLOG 1
00054         #endif
00055     #else
00056         #define PLONK_SMARTPOINTER_DEBUG 0
00057         #ifndef PLONK_SMARTPOINTER_DEBUGLOG
00058             #define PLONK_SMARTPOINTER_DEBUGLOG 0
00059         #endif
00060     #endif
00061 #endif
00062 
00063 //------------------------------------------------------------------------------
00064 
00065 #if PLONK_SMARTPOINTER_DEBUG
00066 const Long getTotalSmartPointers() throw();
00067 const Long getTotalSmartPointerCounters() throw();
00068 #endif
00069 
00070 class PlonkBase
00071 {
00072 public:
00073     PlonkBase() throw() { }
00074     ~PlonkBase() { } // doesn't need to be virtual as we never need to call delete on a PlonkBase pointer
00075     static void* operator new (size_t size); 
00076     static void operator delete (void* ptr);
00077     static void* operator new[] (size_t size); 
00078     static void operator delete[] (void* ptr);
00079 
00080 };
00081 
00092 class SmartPointer : public PlonkBase
00093 {
00094 public:
00095         
00098         
00099         SmartPointer (const bool allocateWeakPointer = true) throw();
00100     virtual ~SmartPointer(); // MUST be virtual unless PlonkBase gains the need to be virtual    
00101     
00102         void incrementRefCount() throw();    
00103     void decrementRefCount() throw();
00104     
00106         
00109         
00110 //    inline void update (Text const& message, Dynamic const& payload) throw() { (void)message; (void)payload; } // needed as a dummy in place of Sender::update?
00111     void* getWeak() const throw();
00112     int getRefCount() const throw();
00113     
00114     virtual SmartPointer* deepCopy() const throw() { return 0; }
00115     
00117     
00118     friend class WeakPointer;
00119     
00120 protected:    
00121     SmartPointerCounter* counter;
00122     AtomicValue<void*> weakPointer;
00123         
00124 private:
00125         SmartPointer (const SmartPointer&);
00126     SmartPointer& operator= (const SmartPointer&);
00127 };
00128 
00129 //------------------------------------------------------------------------------
00130 
00131 class SmartPointerCounter : public PlonkBase
00132 {
00133 public:
00134     SmartPointerCounter (SmartPointer* smartPointer) throw();    
00135     ~SmartPointerCounter();    
00136     
00137     SmartPointer* getSmartPointer() const throw();
00138     
00139     void incrementRefCount() throw();    
00140     void decrementRefCount() throw();
00141     int getRefCount() const throw();   
00142     
00143     void incrementWeakCount() throw();    
00144     void decrementWeakCount() throw();
00145     int getWeakCount() const throw();    
00146 
00147     void incrementCounts() throw();    
00148     void decrementCounts() throw();    
00149 
00150 private:
00151     AtomicExtended<SmartPointer*> atom;
00152     
00153 //    struct Counts
00154 //    {
00155 //        unsigned int refCount:PLONK_HALFWORDBITS;
00156 //        unsigned int weakCount:PLONK_HALFWORDBITS;
00157 //    };
00158 //    
00159 //    struct Parts
00160 //    {
00161 //        SmartPointer* smartPointer;
00162 //        Counts counts;    
00163 //    };
00164 //    
00165 //    struct Halves
00166 //    {
00167 //        SmartPointer* ptr;
00168 //        Long extra;
00169 //    };
00170 //    
00171 //    union Element
00172 //    {
00173 //        Parts parts;
00174 //        Halves halves;
00175 //    };        
00176 };
00177 
00178 
00179 //------------------------------------------------------------------------------
00180 
00181 
00182 #endif // PLONK_SMARTPOINTER_H
 All Classes Functions Typedefs Enumerations Enumerator Properties