Show
Ignore:
Timestamp:
08/14/09 09:03:02 (15 years ago)
Author:
vbarta
Message:

defined *_ptr wrappers of shared pointers

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/shared_ptr.h

    r477 r529  
    11/*! 
    22  \file 
    3   \brief BDM's own smart pointer. 
     3  \brief BDM's own smart pointers. 
    44  \author Vaclav Barta. 
    55 
     
    2222namespace bdm { 
    2323 
    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*/ 
    2833template <typename T> 
    2934class shared_ptr { 
     
    4146        } 
    4247 
    43         //! Constructs a shared_ptr that owns the pointer p (unless p is 
    44         //! null, in which case this constructor creates an empty 
    45         //! shared_ptr). 
     48        /*!  
     49          Constructs a shared_ptr that owns the pointer p (unless p 
     50          is NULL, in which case this constructor creates an empty 
     51          shared_ptr). When p isn't null, it must have been alllocated 
     52          by new! 
     53        */ 
    4654        shared_ptr ( T *p ) : 
    4755                        payload ( p ), 
     
    7078        } 
    7179 
    72         shared_ptr &operator= ( const shared_ptr &other ) { 
     80        shared_ptr<T> &operator= ( const shared_ptr<T> &other ) { 
    7381                other.add_ref(); 
    7482                del_ref(); 
     
    178186} 
    179187 
     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 */ 
     195template <typename T> 
     196class object_ptr : public shared_ptr<T> 
     197{ 
     198public: 
     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 
    180233} 
    181234