pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plank_SharedPtr.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 PLANK_SHAREDPTR_H
00040 #define PLANK_SHAREDPTR_H
00041 
00042 #include "plank_DynamicArray.h"
00043 #include "atomic/plank_Atomic.h"
00044 
00045 #define PLANK_SHAREDPTR_XGETREFCOUNT(EXTRA)             (EXTRA & PLANK_ATOMIC_XREFCOUNTMASK)
00046 #define PLANK_SHAREDPTR_XGETWEAKCOUNT(EXTRA)            ((EXTRA & PLANK_ATOMIC_XWEAKCOUNTMASK) >> PLANK_ATOMIC_XREFCOUNTBITS)
00047 #define PLANK_SHAREDPTR_XCOUNTS(RCOUNT, WCOUNT)         (((WCOUNT << PLANK_ATOMIC_XREFCOUNTBITS) & PLANK_ATOMIC_XWEAKCOUNTMASK) | (RCOUNT & PLANK_ATOMIC_XREFCOUNTMASK))
00048 #define PLANK_SHAREDPTR_XREFCOUNT(EXTRA, RCOUNT)        ((EXTRA & PLANK_ATOMIC_XWEAKCOUNTMASK) | (RCOUNT & PLANK_ATOMIC_XREFCOUNTMASK))
00049 #define PLANK_SHAREDPTR_XWEAKCOUNT(EXTRA, WCOUNT)       (((WCOUNT << PLANK_ATOMIC_XREFCOUNTBITS) & PLANK_ATOMIC_XWEAKCOUNTMASK) | PLANK_SHAREDPTR_XGETREFCOUNT(EXTRA))
00050 
00051 
00052 #define PLANKSHAREDPTR_CREATEANDINIT_DEFINE(NAME,TYPE)\
00053     PlankResult pl_##NAME##_CreateSharedPtr (Plank##NAME##Ref* pp) {\
00054         return pl_SharedPtr_CreateAndInitWithSizeAndFunctions ((PlankSharedPtrRef*)pp, sizeof (Plank##NAME), TYPE, (PlankM)pl_##NAME##_Init, (PlankM)pl_##NAME##_DeInit);\
00055     }
00056 
00057 #define PLANKSHAREDPTR_INCREMENTREFCOUNTANDGET_DEFINE(NAME)\
00058     Plank##NAME##Ref pl_##NAME##_IncrementRefCountAndGet (Plank##NAME##Ref p) {\
00059         return (Plank##NAME##Ref)pl_SharefPtr_IncrementRefCountAndGetPtr ((PlankSharedPtrRef)p);\
00060     }
00061 
00062 #define PLANKSHAREDPTR_DECREMENTREFCOUNT_DEFINE(NAME)\
00063     PlankResult pl_##NAME##_DecrementRefCount (Plank##NAME##Ref p) {\
00064         return pl_SharedPtr_DecrementRefCount((PlankSharedPtrRef)p);\
00065     }
00066 
00067 #define PLANKSHAREDPTR_GETWEAKPTR_DEFINE(NAME)\
00068     PlankWeakPtrRef pl_##NAME##_GetWeakPtr (Plank##NAME##Ref p){\
00069         return pl_SharedPtr_GetWeakPtr ((PlankSharedPtrRef)p);\
00070     }
00071 
00072 #define PLANKSHAREDPTR_GETFROMWEAKPTR_DEFINE(NAME)\
00073     Plank##NAME##Ref pl_##NAME##GetFromWeakPtr (PlankWeakPtrRef w){\
00074         return (Plank##NAME##Ref)pl_WeakPtr_GetSharedPtr (w);\
00075     }
00076 
00077 
00078 #define PLANKSHAREDPTR_TYPE_WEAKPTR             pl_FourCharCode("weak")
00079 #define PLANKSHAREDPTR_TYPE_SHAREDPTRARRAY      pl_FourCharCode("arry")
00080 
00081 
00082 PLANK_BEGIN_C_LINKAGE
00083 
00091 typedef struct PlankSharedPtr* PlankSharedPtrRef;
00092 typedef struct PlankWeakPtr* PlankWeakPtrRef;
00093 typedef struct PlankSharedPtrCounter* PlankSharedPtrCounterRef;
00094 typedef PlankResult (*PlankSharedPtrFunction)(void*);
00095 
00096 PlankResult pl_SharedPtr_CreateAndInitWithSizeAndFunctions (PlankSharedPtrRef* pp, const PlankL size, const PlankFourCharCode type, PlankM initFunction, PlankM deInitFunction);
00097 PlankSharedPtrRef pl_SharefPtr_IncrementRefCountAndGetPtr (PlankSharedPtrRef p);
00098 PlankResult pl_SharedPtr_DecrementRefCount (PlankSharedPtrRef p);
00099 PlankResult pl_SharedPtr_DecrementWeakCount (PlankSharedPtrRef p);
00100 PlankFourCharCode pl_SharedPtr_GetType (PlankSharedPtrRef p);
00101 PlankWeakPtrRef pl_SharedPtr_GetWeakPtr (PlankSharedPtrRef p);
00102 
00103 PlankSharedPtrRef pl_WeakPtr_GetSharedPtr (PlankWeakPtrRef p);
00104 PlankResult pl_WeakPtr_DecrementRefCount (PlankWeakPtrRef p);
00105 
00106 
00107 PlankResult pl_SharedPtrSwap (PlankSharedPtrRef* p1, PlankSharedPtrRef* p2);
00117 typedef struct PlankSharedPtrArray* PlankSharedPtrArrayRef;
00118 typedef PlankDynamicArrayCompareFunction PlankSharedPtrArrayCompareFunction;
00119 
00120 PlankResult pl_SharedPtrArray_CreateSharedPtr (PlankSharedPtrArrayRef* pp, const PlankFourCharCode type);
00121 PlankSharedPtrArrayRef pl_SharefPtrArray_IncrementRefCountAndGetPtr (PlankSharedPtrArrayRef p);
00122 PlankResult pl_SharedPtrArray_DecrementRefCount (PlankSharedPtrArrayRef p);
00123 PlankResult pl_SharedPtrArray_SetElementType (PlankSharedPtrArrayRef p, const PlankFourCharCode type);
00124 PlankFourCharCode pl_SharedPtrArray_GetElementType (PlankSharedPtrArrayRef p);
00125 PlankResult pl_SharedPtrArray_Clear (PlankSharedPtrArrayRef p);
00126 PlankL pl_SharedPtrArray_GetLength (PlankSharedPtrArrayRef p);
00127 PlankAtomicP* pl_SharedPtrArray_GetArray (PlankSharedPtrArrayRef p);
00128 PlankResult pl_SharedPtrArray_AddSharedPtr (PlankSharedPtrArrayRef p, PlankSharedPtrRef item);
00129 PlankResult pl_SharedPtrArray_PutSharedPtr (PlankSharedPtrArrayRef p, const PlankL index, PlankSharedPtrRef item);
00130 PlankResult pl_SharedPtrArray_RemoveSharedPtr (PlankSharedPtrArrayRef p, const PlankL index);
00131 PlankSharedPtrRef pl_SharedPtrArray_GetSharedPtr (PlankSharedPtrArrayRef p, const PlankL index);
00132 PlankResult pl_SharedPtrArray_Swap (PlankSharedPtrArrayRef p, const PlankL indexA, const PlankL indexB);
00133 PlankResult pl_SharedPtrArray_Sort (PlankSharedPtrArrayRef p, PlankSharedPtrArrayCompareFunction comparator);
00134 
00135 
00138 PLANK_END_C_LINKAGE
00139 
00141 
00142 #if !DOXYGEN
00143 typedef struct PlankSharedPtr
00144 {
00145     PlankUI size;
00146     PlankFourCharCode type;
00147     PlankSharedPtrCounterRef sharedCounter;
00148     PlankSharedPtrFunction deInitFunction;
00149     PlankWeakPtrRef weakPtr;
00150 } PlankSharedPtr;
00151 
00152 typedef struct PlankSharedPtrArray
00153 {
00154     PlankSharedPtr sharedHeader;
00155     PlankFourCharCode elementType;
00156     PlankDynamicArray array;
00157 } PlankSharedPtrArray;
00158 #endif
00159 
00160 #endif // PLANK_SHAREDPTR_H
 All Classes Functions Typedefs Enumerations Enumerator Properties