Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShRefCount.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHREFCOUNT_HPP
00025 #define SHREFCOUNT_HPP
00026 
00027 #include <utility>
00028 #include "ShDllExport.hpp"
00029 
00030 // #define SH_REFCOUNT_DEBUGGING
00031 
00032 #ifdef SH_REFCOUNT_DEBUGGING
00033 #include <iostream>
00034 #include <iomanip>
00035 #include "ShDebug.hpp"
00036 
00037 #define SH_RCDEBUG_GREEN std::cerr << ""
00038 #define SH_RCDEBUG_RED std::cerr << ""
00039 #define SH_RCDEBUG_BLUE std::cerr << ""
00040 #define SH_RCDEBUG_NORMAL std::cerr << ""
00041 
00042 #endif
00043 
00044 namespace SH {
00045 
00050 class 
00051 SH_DLLEXPORT
00052 ShRefCountable 
00053 {
00054 public:
00055   ShRefCountable()
00056     : m_refCount(0)
00057   {
00058   }
00059 
00060   ShRefCountable(const ShRefCountable&)
00061     : m_refCount(0)
00062     // any new RefCountable should have a zero refcount, even if it's
00063     // made as a copy
00064   {
00065   }
00066 
00067   ShRefCountable& operator=(const ShRefCountable&)
00068   {
00069     // we don't actually change refCount here
00070     // this is indeed the intended behaviour
00071     return *this;
00072   }
00073 
00074 #ifdef SH_REFCOUNT_DEBUGGING
00075   // Just to make this polymorphic, so typeid() works as expected
00076   virtual ~ShRefCountable() {}   
00077 #endif
00078   
00079   int acquireRef() const
00080   {
00081 #ifdef SH_REFCOUNT_DEBUGGING
00082     SH_RCDEBUG_GREEN;
00083     std::cerr << "   [+] " << std::setw(10) << this << " <" << typeid(*this).name() << ">"
00084               << ": " << m_refCount << "->" << (m_refCount + 1) << std::endl;
00085     SH_RCDEBUG_NORMAL;
00086 #endif
00087     return ++m_refCount;
00088   }
00089   
00090   int releaseRef() const
00091   {
00092 #ifdef SH_REFCOUNT_DEBUGGING
00093     SH_RCDEBUG_RED;
00094     std::cerr << "   [-] " << std::setw(10) << this << " <" << typeid(*this).name() << ">"
00095               << ": " << m_refCount << "->" << (m_refCount - 1) << std::endl;
00096     SH_RCDEBUG_NORMAL;
00097 #endif
00098     return --m_refCount;
00099   }
00100 
00101   int refCount() const
00102   {
00103     return m_refCount;
00104   }
00105 
00106 private:
00107   mutable int m_refCount;
00108 };
00109 
00112 template<typename T>
00113 class ShPointer 
00114 {
00115 public:
00116   ShPointer();
00117   ShPointer(T* object);
00118   ShPointer(const ShPointer& other);
00119   template<typename S>
00120   ShPointer(const ShPointer<S>& other);
00121   
00122   ~ShPointer();
00123 
00124   ShPointer& operator=(T* other);
00125   ShPointer& operator=(const ShPointer& other);
00126   template<typename S>
00127   ShPointer& operator=(const ShPointer<S>& other);
00128 
00130   bool operator==(const ShPointer& other) const;
00131 
00133   bool operator!=(const ShPointer& other) const;
00134 
00136   bool operator<(const ShPointer& other) const;
00137 
00138   T& operator*() const;
00139   T* operator->() const;
00140 
00143   operator bool() const;
00144 
00146   int refCount() const;
00147 
00149   T* object() const;
00150 
00151   void swap(ShPointer& other);
00152 
00153 private:
00154   void releaseRef();
00155   
00156   T* m_object;
00157 };
00158 
00159 template<typename T, typename S>
00160 ShPointer<T> shref_static_cast(const ShPointer<S>& other);
00161 
00162 template<typename T, typename S>
00163 ShPointer<T> shref_dynamic_cast(const ShPointer<S>& other);
00164 
00165 template<typename T, typename S>
00166 ShPointer<T> shref_const_cast(const ShPointer<S>& other);
00167 
00168 } // namespace SH
00169 
00170 #include "ShRefCountImpl.hpp"
00171 
00172 #endif

Generated on Wed Jun 15 18:12:41 2005 for Sh by  doxygen 1.4.3-20050530