shared_ptr.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _SHARED_PTR_H_
00023 #define _SHARED_PTR_H_
00024
00025 namespace dbp {
00026
00027 namespace local {
00028
00029 template <class T>
00030 class smart_ptr {
00031 public:
00032 smart_ptr(): _data(new T), _refcount(0) { };
00033 smart_ptr(T *data): _data(data), _refcount(0) { };
00034 smart_ptr(const smart_ptr<T> &src):
00035 _data(new T(*(src._data))), _refcount(0) { };
00036 ~smart_ptr() {
00037 delete _data;
00038 };
00039 smart_ptr<T>& operator=(const smart_ptr<T> &src) {
00040 if (this == &src)
00041 return *this;
00042 delete _data;
00043 _data = new T(*(src._data));
00044 return *this;
00045 };
00046 T* operator->() const {
00047 return _data;
00048 };
00049 T& operator*() const {
00050 return *_data;
00051 };
00052 void invoke() { _refcount++; };
00053 void release() {
00054 if (_refcount > 0)
00055 _refcount--;
00056 if (_refcount == 0) {
00057 delete this;
00058 };
00059 };
00060 private:
00061 T *_data;
00062 int _refcount;
00063 };
00064
00065 }
00066
00068
00073 template <class T>
00074 class shared_ptr {
00075 public:
00077 shared_ptr(): _data(new local::smart_ptr<T>) {
00078 _data->invoke();
00079 };
00081 shared_ptr(T *data): _data(new local::smart_ptr<T>(data)) {
00082 _data->invoke();
00083 };
00085 shared_ptr(const shared_ptr<T> &src): _data(src._data) {
00086 _data->invoke();
00087 };
00089 ~shared_ptr() {
00090 _data->release();
00091 };
00093 shared_ptr<T>& operator=(const shared_ptr<T> &src) {
00094 if ((this == &src) || (_data == src._data))
00095 return *this;
00096 _data->release();
00097 _data = src._data;
00098 _data->invoke();
00099 return *this;
00100 };
00102 T* operator->() const {
00103 return &(**_data);
00104 };
00106 T& operator*() const {
00107 return *_data;
00108 };
00110 bool operator<(const shared_ptr<T> &src) const {
00111 return _data < src._data;
00112 };
00113 private:
00114 local::smart_ptr<T> *_data;
00115 };
00116
00117 }
00118
00119 #endif
00120