Go to the documentation of this file.00001
00002 #ifndef vbl_shared_pointer_h_
00003 #define vbl_shared_pointer_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <vcl_compiler.h>
00016
00017 #define vbl_shared_pointer_zero(var) (var) = 0
00018
00019
00020 struct vbl_shared_pointer_data
00021 {
00022 int use_count;
00023 vbl_shared_pointer_data(int u) : use_count(u) { }
00024 };
00025
00026
00027
00028
00029
00030
00031
00032
00033 template <class T>
00034 class vbl_shared_pointer
00035 {
00036 public:
00037 typedef T element_type;
00038 typedef vbl_shared_pointer<T> self;
00039
00040 typedef vbl_shared_pointer_data data_t;
00041
00042 vbl_shared_pointer() : pointer(0), count_data(0) { }
00043
00044 explicit
00045 vbl_shared_pointer(T *p) {
00046 if (p) {
00047 pointer = p;
00048 count_data = new data_t(1);
00049 } else {
00050 pointer = 0;
00051 count_data = 0;
00052 }
00053 }
00054
00055 vbl_shared_pointer(self const &that)
00056 : pointer( that.pointer ),
00057 count_data( that.count_data )
00058 {
00059 up_ref();
00060 }
00061
00062 #if VCL_HAS_MEMBER_TEMPLATES
00063 template<class U> friend class vbl_shared_pointer;
00064
00065
00066 template <class U>
00067 vbl_shared_pointer( vbl_shared_pointer<U> const &that )
00068 : pointer( that.pointer ),
00069 count_data( that.count_data )
00070 {
00071 up_ref();
00072 }
00073
00074 template <class U>
00075 self &operator=( vbl_shared_pointer<U> const &that) {
00076 that.up_ref();
00077 down_ref();
00078 pointer = that.pointer;
00079 count_data = that.count_data;
00080 return *this;
00081 }
00082
00083 #endif
00084
00085 #if 0
00086
00087
00088
00089
00090
00091 #if VCL_HAS_MEMBER_TEMPLATES
00092
00093
00094
00095
00096
00097 template < class V1>
00098 explicit vbl_shared_pointer(V1 const &v1)
00099 : data(new data_t(new T(v1), 1)) { }
00100
00101 template <class V1, class V2>
00102 explicit vbl_shared_pointer(V1 const &v1, V2 const &v2)
00103 : data(new data_t(new T(v1, v2), 1)) { }
00104
00105 template <class V1, class V2, class V3>
00106 explicit vbl_shared_pointer(V1 const &v1, V2 const &v2, V3 const &v3)
00107 : data(new data_t(new T(v1, v2, v3), 1)) { }
00108
00109 template <class V1, class V2, class V3, class V4>
00110 explicit vbl_shared_pointer(V1 const &v1, V2 const &v2, V3 const &v3, V4 const &v4)
00111 : data(new data_t(new T(v1, v2, v3, v4), 1)) { }
00112 #endif
00113 #endif
00114
00115 self &operator=(self const &that) {
00116 that.up_ref();
00117 down_ref();
00118 pointer = that.pointer;
00119 count_data = that.count_data;
00120 return *this;
00121 }
00122
00123 ~vbl_shared_pointer() {
00124 down_ref();
00125 }
00126
00127 private:
00128 VCL_SAFE_BOOL_DEFINE;
00129 public:
00130
00131 operator safe_bool () const
00132 { return (pointer != 0)? VCL_SAFE_BOOL_TRUE : 0; }
00133
00134
00135 bool operator!() const
00136 { return (pointer != 0)? false : true; }
00137
00138
00139 #if !defined VBL_SHARED_POINTER_OF_NON_COMPOUND // Get rid of warning with vbl_shared_pointer<int>
00140
00141
00142 T const *operator->() const { return as_pointer(); }
00143 T *operator->() { return as_pointer(); }
00144 #endif
00145
00146
00147 T const &operator*() const { return *as_pointer(); }
00148 T &operator*() { return *as_pointer(); }
00149
00150
00151 bool operator!=(self const &that) const { return pointer != that.pointer; }
00152 bool operator==(self const &that) const { return pointer == that.pointer; }
00153 bool operator< (self const &that) const { return pointer < that.pointer; }
00154
00155
00156 T *as_pointer() const {
00157 return pointer;
00158 }
00159 void up_ref() const {
00160 if (count_data)
00161 ++ count_data->use_count;
00162 }
00163 void down_ref() const {
00164 if (count_data && (-- count_data->use_count == 0)) {
00165 delete pointer;
00166 delete count_data;
00167 }
00168 }
00169 private:
00170 T *pointer;
00171 data_t *count_data;
00172 };
00173
00174 #define VBL_SHARED_POINTER_INSTANTIATE(T) // template class vbl_shared_pointer<T >
00175
00176 #endif // vbl_shared_pointer_h_