![]() |
pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
|
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_WEAKPOINTERCONTAINER_H 00040 #define PLONK_WEAKPOINTERCONTAINER_H 00041 00042 #include "plonk_CoreForwardDeclarations.h" 00043 #include "plonk_SmartPointer.h" 00044 #include "plonk_SmartPointerContainer.h" 00045 00052 template<class OriginalType> 00053 class WeakPointerContainer : public SmartPointerContainer<WeakPointer,false> 00054 { 00055 public: 00056 typedef SmartPointerContainer<WeakPointer,false> Base; 00057 typedef typename OriginalType::Internal OriginalInternal; 00058 00059 WeakPointerContainer() throw() 00060 : Base (0) 00061 { 00062 } 00063 00064 ~WeakPointerContainer() 00065 { 00066 decrementWeakCount (this->getInternal()); 00067 } 00068 00071 WeakPointerContainer (WeakPointerContainer const& copy) throw() 00072 : Base (static_cast<Base const&> (copy)) 00073 { 00074 incrementWeakCount (this->getInternal()); 00075 } 00076 00078 WeakPointerContainer& operator= (WeakPointerContainer const& other) throw() 00079 { 00080 if (this != &other) 00081 { 00082 WeakPointer* const weakPointer = this->getInternal(); 00083 WeakPointer* const otherWeakPointer = other.getInternal(); 00084 00085 incrementWeakCount (otherWeakPointer); 00086 decrementWeakCount (weakPointer); 00087 00088 this->setInternal (otherWeakPointer); 00089 } 00090 00091 return *this; 00092 } 00093 00094 WeakPointerContainer (OriginalType const& original) throw() 00095 : Base (original.getWeakPointer()) 00096 { 00097 incrementWeakCount (this->getInternal()); 00098 } 00099 00100 bool isAlive() const throw() 00101 { 00102 WeakPointer* const weakPointer = this->getInternal(); 00103 00104 if (weakPointer != 0) 00105 return weakPointer->getWeakPointer() != 0; 00106 00107 return false; 00108 } 00109 00110 OriginalType fromWeak() const throw() 00111 { 00112 OriginalType result = OriginalType(); 00113 WeakPointer* const weakPointer = this->getInternal(); 00114 00115 if (weakPointer != 0) 00116 { 00117 // retain it, this ensures that if weakPointer->getWeakPointer() returns 00118 // a non-null pointer that it will still be valid when passed to 00119 // construct the container object (which retains it again) OR... 00120 // the pointer will be null 00121 weakPointer->incrementPeerRefCount(); 00122 00123 OriginalInternal* smartPointer = static_cast<OriginalInternal*> (weakPointer->getWeakPointer()); 00124 00125 if (smartPointer != 0) 00126 result = OriginalType (smartPointer); 00127 00128 // release it 00129 weakPointer->decrementPeerRefCount(); 00130 } 00131 00132 return result; 00133 } 00134 00135 private: 00136 inline static void incrementWeakCount (WeakPointer* const weakPointer) throw() 00137 { 00138 if (weakPointer != 0) 00139 weakPointer->incrementWeakCount(); 00140 } 00141 00142 inline static void decrementWeakCount (WeakPointer* const weakPointer) throw() 00143 { 00144 if (weakPointer != 0) 00145 weakPointer->decrementWeakCount(); 00146 } 00147 }; 00148 00149 00150 #endif // PLONK_WEAKPOINTERCONTAINER_H