24 | | //! A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really just roughly - it ignores memory exceptions, for example; also note I didn't read the spec). |
25 | | // The standard template would naturally be preferable, _if_ it was |
26 | | // included in the standard libraries of all supported compilers - but |
27 | | // that's exactly what remains to be seen... |
| 24 | /*! \brief A naive implementation of roughly a subset of the std::tr1:shared_ptr spec |
| 25 | |
| 26 | Really just roughly - it ignores memory |
| 27 | exceptions, for example; also note I didn't read the spec. |
| 28 | |
| 29 | The standard template would naturally be preferable, _if_ it was |
| 30 | included in the standard libraries of all supported compilers - but |
| 31 | as of 2009, that's still a problem... |
| 32 | */ |
| 188 | /*! \brief A wrapper of shared_ptr which is never empty. |
| 189 | |
| 190 | T must have a default constructor. |
| 191 | |
| 192 | Note that shared_ptr's destructor isn't virtual - don't call delete |
| 193 | on pointers to instances of this class. |
| 194 | */ |
| 195 | template <typename T> |
| 196 | class object_ptr : public shared_ptr<T> |
| 197 | { |
| 198 | public: |
| 199 | /*! |
| 200 | \brief Default constructor |
| 201 | |
| 202 | Calls T's default constructor. |
| 203 | */ |
| 204 | object_ptr() : shared_ptr<T> ( new T() ) { } |
| 205 | |
| 206 | /*! |
| 207 | \brief Upcast from shared_ptr<T> to object_ptr<T> |
| 208 | |
| 209 | \param b The shared pointer, which must not be empty. |
| 210 | */ |
| 211 | object_ptr ( const shared_ptr<T> &b ) : shared_ptr<T> ( b ) { |
| 212 | it_assert_debug ( this->get(), "object_ptr cannot be empty" ); |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | Constructs an object_ptr that owns the pointer p. p must |
| 217 | have been alllocated by new! |
| 218 | */ |
| 219 | object_ptr ( T *p ) : shared_ptr<T> ( p ) { |
| 220 | it_assert_debug ( p, "object_ptr cannot be empty" ); |
| 221 | } |
| 222 | |
| 223 | object_ptr<T> &operator= ( const object_ptr<T> &other ) { |
| 224 | shared_ptr<T>::operator= ( other ); |
| 225 | return *this; |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | #define SHAREDPTR(class_name) typedef bdm::object_ptr< class_name > class_name##_ptr |
| 230 | |
| 231 | #define SHAREDPTR2(template_class_name, template_parameter_name) typedef bdm::object_ptr< template_class_name < template_parameter_name > > template_class_name##_##template_parameter_name##_ptr |
| 232 | |