00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_REFERENCE_HPP_)
00012 #define _XRB_REFERENCE_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 namespace Xrb
00017 {
00018
00019
00020
00021
00022
00023 template <typename T>
00024 class Instance
00025 {
00026 public:
00027
00028 inline Instance ()
00029 {
00030 m_reference_count = 0;
00031 }
00032 inline Instance (T const &content)
00033 :
00034 m_content(content)
00035 {
00036 m_reference_count = 0;
00037 }
00038 inline Instance (Instance<T> const &source)
00039 {
00040 ASSERT0(false && "Don't use Instance<T> this way");
00041 }
00042 inline ~Instance ()
00043 {
00044 ASSERT1(m_reference_count == 0);
00045 }
00046
00047 inline void operator = (Instance<T> const &operand)
00048 {
00049 ASSERT0(false && "Don't use Instance<T> this way");
00050 }
00051
00052 inline T const &operator * () const
00053 {
00054 return m_content;
00055 }
00056 inline T &operator * ()
00057 {
00058 return m_content;
00059 }
00060 inline T const *operator -> () const
00061 {
00062 return &m_content;
00063 }
00064 inline T *operator -> ()
00065 {
00066 return &m_content;
00067 }
00068
00069 inline Uint32 ReferenceCount () const
00070 {
00071 return m_reference_count;
00072 }
00073
00074 inline void IncrementReferenceCount ()
00075 {
00076 ASSERT1(m_reference_count < 0xFFFFFFFF);
00077 ++m_reference_count;
00078 }
00079 inline void DecrementReferenceCount ()
00080 {
00081 ASSERT1(m_reference_count > 0);
00082 --m_reference_count;
00083 }
00084
00085 private:
00086
00087 Uint32 m_reference_count;
00088 T m_content;
00089 };
00090
00091
00092
00093
00094
00095 template <typename T>
00096 class Reference
00097 {
00098 public:
00099
00100 inline Reference ()
00101 {
00102 m_instance = NULL;
00103 }
00104 inline Reference (Instance<T> *const instance)
00105 {
00106 ASSERT1(instance != NULL);
00107 m_instance = instance;
00108 m_instance->IncrementReferenceCount();
00109 }
00110 inline Reference (Reference<T> const &source)
00111 {
00112 m_instance = source.m_instance;
00113 if (m_instance != NULL)
00114 m_instance->IncrementReferenceCount();
00115 }
00116 inline ~Reference ()
00117 {
00118 if (m_instance != NULL)
00119 m_instance->DecrementReferenceCount();
00120 }
00121
00122 inline void operator = (Instance<T> *const instance)
00123 {
00124 if (m_instance != NULL)
00125 m_instance->DecrementReferenceCount();
00126
00127 ASSERT1(instance != NULL);
00128 m_instance = instance;
00129 if (m_instance != NULL)
00130 m_instance->IncrementReferenceCount();
00131 }
00132 inline void operator = (Reference<T> const &operand)
00133 {
00134 if (m_instance != NULL)
00135 m_instance->DecrementReferenceCount();
00136
00137 m_instance = operand.m_instance;
00138 if (m_instance != NULL)
00139 m_instance->IncrementReferenceCount();
00140 }
00141
00142 inline T const &operator * () const
00143 {
00144 ASSERT1(m_instance != NULL);
00145 Instance<T> const *instance = m_instance;
00146 return **instance;
00147 }
00148 inline T &operator * ()
00149 {
00150 ASSERT1(m_instance != NULL);
00151 return **m_instance;
00152 }
00153
00154 inline T const *operator -> () const
00155 {
00156 ASSERT1(m_instance != NULL);
00157 Instance<T> const *instance = m_instance;
00158 return &**instance;
00159 }
00160 inline T *operator -> ()
00161 {
00162 ASSERT1(m_instance != NULL);
00163 return &**m_instance;
00164 }
00165
00166 inline bool operator == (Instance<T> *const instance)
00167 {
00168 return m_instance == instance;
00169 }
00170
00171 inline bool IsValid () const
00172 {
00173 return m_instance != NULL;
00174 }
00175 inline Uint32 ReferenceCount () const
00176 {
00177 if (m_instance != NULL)
00178 return m_instance->ReferenceCount();
00179 else
00180 return 0;
00181 }
00182 inline void Release ()
00183 {
00184 if (m_instance != NULL)
00185 m_instance->DecrementReferenceCount();
00186 m_instance = NULL;
00187 }
00188
00189 private:
00190
00191 Instance<T> *m_instance;
00192 };
00193
00194 }
00195
00196 #endif // !defined(_XRB_REFERENCE_HPP_)
00197