00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#ifndef _CSELFDESTROYPOINTER
00024
#define _CSELFDESTROYPOINTER
00025
#include "libMRML/include/uses-declarations.h"
00029
template<
class T>
00030 class CSelfDestroyPointer{
00032
mutable bool mIsSelfDestroyer;
00034
protected:
00035 T* mPointer;
00037
public:
00039
void resetWithoutDeleting();
00041
inline bool isSelfDestroyer()
const;
00043
inline void setIsSelfDestroyer(
bool inisSelfDestroyer=
true)
const;
00045
void unsetIsSelfDestroyer()
const;
00047 T* operator= (T* inPointer);
00049 T& operator*();
00051 T
const& operator*()
const;
00053 T* operator -> ();
00055 T
const* operator -> ()
const;
00057 ~
CSelfDestroyPointer();
00059
CSelfDestroyPointer(T*,
00060
bool =
true);
00062
CSelfDestroyPointer(
const CSelfDestroyPointer<T>&
00063 inSelfDestroyPointer);
00065
CSelfDestroyPointer();
00067 operator bool()
const;
00069 operator T*()
const;
00070 };
00071
00072
00074
template<
class T>
00075
void CSelfDestroyPointer<T>::resetWithoutDeleting(){
00076 mPointer=0;
00077 }
00078
00079
template<
class T>
00080 T*
CSelfDestroyPointer<T>::operator=(T* inPointer){
00081
00082
if(mIsSelfDestroyer){
00083
#ifdef _DEBUG_SELF_DESTROY_
00084
cout <<
"£"<<flush;
00085
#endif
00086
delete mPointer;
00087 }
00088 mPointer=inPointer;
00089 }
00090
00091
template<
class T>
00092 T
const&
CSelfDestroyPointer<T>::operator *()const{
00093
return *mPointer;
00094 }
00095
00096
template<
class T>
00097 T
const*
CSelfDestroyPointer<T>::operator ->()const{
00098
return mPointer;
00099 }
00100
00101
template<
class T>
00102 T&
CSelfDestroyPointer<T>::operator *(){
00103
return *mPointer;
00104 }
00105
00106
template<
class T>
00107 T*
CSelfDestroyPointer<T>::operator ->(){
00108
return mPointer;
00109 }
00110
00111
template<
class T>
00112
CSelfDestroyPointer<T>::CSelfDestroyPointer(T* inPointer,
00113
bool inIsSelfDestroyer):
00114 mPointer(inPointer),
00115 mIsSelfDestroyer(inIsSelfDestroyer)
00116 {
00117 }
00119
template<
class T>
00120
CSelfDestroyPointer<T>::CSelfDestroyPointer(
const CSelfDestroyPointer<T>& in):
00121 mPointer(in.mPointer),
00122 mIsSelfDestroyer(in.mIsSelfDestroyer)
00123 {
00124 };
00125
00126
template<
class T>
00127
CSelfDestroyPointer<T>::CSelfDestroyPointer():
00128 mPointer(0),
00129 mIsSelfDestroyer(true)
00130 {
00131 }
00132
00133
template<
class T>
00134
CSelfDestroyPointer<T>::~CSelfDestroyPointer()
00135 {
00136
if(mIsSelfDestroyer){
00137
00138
delete mPointer;
00139 }
00140 }
00141
00142
00143
template<
class T>
00144
void CSelfDestroyPointer<T>::setIsSelfDestroyer(
bool inIsSelfDestroyer)
const{
00145 mIsSelfDestroyer= inIsSelfDestroyer;
00146 };
00147
00148
template<
class T>
00149
bool CSelfDestroyPointer<T>::isSelfDestroyer()const{
00150
return mIsSelfDestroyer;
00151 };
00152
00153
template<
class T>
00154
void CSelfDestroyPointer<T>::unsetIsSelfDestroyer()const{
00155 mIsSelfDestroyer=0;
00156 };
00157
00158
template<
class T>
00159
CSelfDestroyPointer<T>::operator bool()const{
00160
return mPointer;
00161 };
00162
00163
template<
class T>
00164
CSelfDestroyPointer<T>::operator T*()const{
00165
return mPointer;
00166 };
00167
00168
template<
class T>
00169
class CSelfClonePointer:
public CSelfDestroyPointer<T>{
00171
mutable bool mIsSelfCloner;
00173
public:
00175 CSelfClonePointer(T*,
00176
bool =
true);
00178 CSelfClonePointer<T>& operator= (T* in);
00180 CSelfClonePointer<T>& operator= (
const CSelfClonePointer<T>& in);
00182 CSelfClonePointer(
const CSelfClonePointer<T>&);
00184 CSelfClonePointer();
00186 operator bool()const;
00188 operator T*()const;
00189 };
00190
00191
00192 template<class T>
00193 CSelfClonePointer<T>& CSelfClonePointer<T>::operator=(T* in){
00194
00195
00196
CSelfDestroyPointer<T>::operator=(in);
00197
return *
this;
00198 };
00199
00200
template<
class T>
00201 CSelfClonePointer<T>& CSelfClonePointer<T>::operator= (
const CSelfClonePointer<T>& in){
00202
00203 this->mPointer=in.mPointer;
00204 setIsSelfDestroyer(in.isSelfDestroyer());
00205
return *
this;
00206 };
00207
00208
template<
class T>
00209 CSelfClonePointer<T>::CSelfClonePointer(T* inPointer,
bool inIsSelfCloner):
00210
CSelfDestroyPointer<T>(inPointer,
00211 inIsSelfCloner)
00212 {
00213 }
00214
template<
class T>
00215 CSelfClonePointer<T>::CSelfClonePointer():
00216
CSelfDestroyPointer<T>(0,
00217 true)
00218 {
00219 }
00220
template<
class T>
00221 CSelfClonePointer<T>::CSelfClonePointer(
const CSelfClonePointer<T>& in):
00222
CSelfDestroyPointer<T>(in)
00223 {
00224
if(in.mPointer && in.isSelfDestroyer()){
00225 this->mPointer=in.mPointer->clone();
00226 }
else{
00227 this->mPointer=in.mPointer;
00228 }
00229 }
00230
00231
#endif