00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
#ifndef USHAREDPTR_HPP
00029 
#define USHAREDPTR_HPP
00030 
00031 
#include "config/ufo_config.hpp"
00032 
00033 
namespace ufo {
00034 
00040 
template<
typename T>
00041 class USharedPtr {
00042 
private:
00043     T * m_object; 
00044     
int * m_refCount; 
00045 
00046 
public:
00047     
explicit USharedPtr() : m_object(NULL), m_refCount(NULL) {}
00048 
00049     
explicit USharedPtr(
const USharedPtr<T> & right) {
00050         
if (right.m_object) {
00051             m_object = right.m_object;
00052             m_refCount = right.m_refCount;
00053             ++*m_refCount;
00054         } 
else {
00055             m_object = NULL;
00056             m_refCount = NULL;
00057         }
00058     }
00059 
00060     
explicit USharedPtr(T * right) : m_object(NULL), m_refCount(NULL) {
00061         assign(right);
00062     }
00063     
00064 
00065 
00066 
00067 
00068     ~
USharedPtr() {
00069         release();
00070     }
00071 
00072     
USharedPtr<T>& operator =(
const USharedPtr<T>& right) {
00073         
if (right.m_object) {
00074             ++*right.m_refCount;
00075         }
00076 
00077         release();
00078 
00079         m_object = right.m_object;
00080         m_refCount = right.m_refCount;
00081 
00082         
return *
this;
00083     }
00084 
00085     
USharedPtr<T>& operator =(T* right) {
00086         assign(right);
00087 
00088         
return *
this;
00089     }
00090     
00091 
00092 
00093 
00094 
00095 
00096 
00097     
bool valid() {
00098         
return (m_object != NULL);
00099     }
00100     
bool operator()()
 const {
00101         
return (m_object != NULL);
00102     }
00103     
bool operator!()
 const {
00104         
return (m_object == NULL);
00105     }
00106 
00107     T * operator ->()
 const {
00108         
return m_object;
00109     }
00110 
00111     T & operator *()
 const {
00112         
return *m_object;
00113     }
00114 
00115     operator T*()
 const {
00116         
return m_object;
00117     }
00118 
00119 
00120 
00121 
00122 
00123     
int refCount() {
00124         
return *m_refCount;
00125     }
00126 
private: 
00127     
void assign(T * t) {
00128         
if (t == m_object) { 
return; }
00129 
00130         release();
00131         
if (t) {
00132             m_object = t;
00133             m_refCount = 
new int;
00134             *m_refCount = 1;
00135         }
00136     }
00137     
void release() {
00138         
if (m_object) {
00139             
if (*m_refCount == 1) {
00140                 
delete m_refCount;
00141                 
delete m_object;
00142             } 
else {
00143                 --*m_refCount;
00144             }
00145             m_object = NULL;
00146             m_refCount = NULL;
00147         }
00148     }
00149 };
00150 
00151 } 
00152 
00153 
#endif // USHAREDPTR_HPP