Show
Ignore:
Timestamp:
06/09/10 14:00:40 (14 years ago)
Author:
mido
Message:

astyle applied all over the library

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/shared_ptr.h

    r756 r1064  
    3333template <typename T> 
    3434class shared_ptr { 
    35         template<class U> friend class shared_ptr; 
     35    template<class U> friend class shared_ptr; 
    3636 
    3737private: 
    38         T *payload; 
    39         unsigned *refCnt; 
     38    T *payload; 
     39    unsigned *refCnt; 
    4040 
    4141public: 
    42         /*! 
    43           \brief Default constructor 
    44  
    45           Creates an empty shared_ptr - one that doesn't point anywhere. 
    46         */ 
    47         shared_ptr() : 
    48                         payload ( 0 ), 
    49                         refCnt ( 0 ) { 
    50         } 
    51  
    52         /*! 
    53           Constructs a shared_ptr that owns the pointer p (unless p 
    54           is NULL, in which case this constructor creates an empty 
    55           shared_ptr). When p isn't null, it must have been alllocated 
    56           by new! 
    57         */ 
    58         shared_ptr ( T *p ) : 
    59                         payload ( p ), 
    60                         refCnt ( p ? new unsigned ( 1 ) : 0 ) { 
    61         } 
    62  
    63         /*! 
    64           \brief Copy constructor 
    65  
    66           If other is empty, constructs an empty shared_ptr; otherwise, 
    67           constructs a shared_ptr that shares ownership with other. 
    68         */ 
    69         shared_ptr ( const shared_ptr<T> &other ) : 
    70                         payload ( other.payload ), 
    71                         refCnt ( other.refCnt ) { 
    72                 add_ref(); 
    73         } 
    74  
    75         /*! 
    76           \brief Generalized copy 
    77  
    78           Allows initialization of shared pointer of a base type from 
    79           raw pointer to a derived type. 
    80  
    81           If other is empty, constructs an empty shared_ptr; otherwise, 
    82           constructs a shared_ptr that shares ownership with other. 
    83         */ 
    84         template<typename U> 
    85         shared_ptr ( const shared_ptr<U> &other ) : 
    86                         payload ( other.payload ), 
    87                         refCnt ( other.refCnt ) { 
    88                 add_ref(); 
    89         } 
    90  
    91         /*! 
    92           Destructor. 
    93         */ 
    94         ~shared_ptr() { 
    95                 del_ref(); 
    96         } 
    97  
    98         /*! 
    99           \brief Assignment operator 
    100         */ 
    101         shared_ptr<T> &operator= ( const shared_ptr<T> &other ) { 
    102                 other.add_ref(); 
    103                 del_ref(); 
    104  
    105                 payload = other.payload; 
    106                 refCnt = other.refCnt; 
    107  
    108                 return *this; 
    109         } 
    110  
    111         /*! 
    112           Returns the stored pointer (which remains owned by this 
    113           instance). For empty instances, this method returns NULL. 
    114         */ 
    115         T *get() { 
    116                 return payload; 
    117         } 
    118  
    119         /*! 
    120           Dereferences the stored pointer (which remains owned by 
    121           this instance). This method may only be called when the 
    122           stored pointer isn't NULL. 
    123         */ 
    124         T *operator->() { 
    125                 bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
    126                 return payload; 
    127         } 
    128  
    129         //! Returns a reference to the object pointed to by the stored 
    130         //! pointer. This method may only be called when the stored pointer 
    131         //! isn't NULL. 
    132         T &operator*() { 
    133                 bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
    134                 return *payload; 
    135         } 
    136  
    137         /*! 
    138           Returns the stored pointer (which remains owned by this 
    139           instance). For empty instances, this method returns NULL. 
    140         */ 
    141         const T* get() const { 
    142                 return payload; 
    143         } 
    144  
    145         //! Returns the stored pointer (which remains owned by this 
    146         //! instance). This method may only be called when the stored 
    147         //! pointer isn't NULL. 
    148         const T *operator->() const { 
    149                 bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
    150                 return payload; 
    151         } 
    152  
    153         //! Returns a reference to the object pointed to by the stored 
    154         //! pointer. This method may only be called when the stored pointer 
    155         //! isn't NULL. 
    156         const T &operator*() const { 
    157                 bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
    158                 return *payload; 
    159         } 
    160  
    161         //! Returns use_count() == 1 
    162         bool unique() const { 
    163                 return refCnt && ( *refCnt == 1 ); 
    164         } 
    165  
    166         /*! 
    167           Returns the number of shared_ptr instances (including 
    168           this instance) that share ownership with this instance. For 
    169           empty instances, this method returns 0. 
    170         */ 
    171         long use_count() const { 
    172                 return refCnt ? *refCnt : 0; 
    173         } 
    174  
    175         /*! 
    176           \brief Boolean cast 
    177  
    178           This operator returns true if and only if the instance isn't empty. 
    179         */ 
    180         operator bool() const { 
    181                 return !!payload; 
    182         } 
    183  
    184         /*! 
    185           \brief const cast 
    186  
    187           Shared pointer to T can be converted to shared pointer to 
    188           const T, just like T * can be converted to T const *. 
    189         */ 
    190         template<typename U> 
    191         operator shared_ptr<const U>() const { 
    192                 shared_ptr<const U> cptr; 
    193                 cptr.refCnt = refCnt; 
    194                 cptr.payload = payload; 
    195                 add_ref(); 
    196                 return cptr; 
    197         } 
    198  
    199         /*! 
    200           \brief Efficient swap for shared_ptr. 
    201         */ 
    202         void swap ( shared_ptr &other ) { 
    203                 std::swap ( payload, other.payload ); 
    204                 std::swap ( refCnt, other.refCnt ); 
    205         } 
     42    /*! 
     43      \brief Default constructor 
     44 
     45      Creates an empty shared_ptr - one that doesn't point anywhere. 
     46    */ 
     47    shared_ptr() : 
     48        payload ( 0 ), 
     49        refCnt ( 0 ) { 
     50    } 
     51 
     52    /*! 
     53      Constructs a shared_ptr that owns the pointer p (unless p 
     54      is NULL, in which case this constructor creates an empty 
     55      shared_ptr). When p isn't null, it must have been alllocated 
     56      by new! 
     57    */ 
     58    shared_ptr ( T *p ) : 
     59        payload ( p ), 
     60        refCnt ( p ? new unsigned ( 1 ) : 0 ) { 
     61    } 
     62 
     63    /*! 
     64      \brief Copy constructor 
     65 
     66      If other is empty, constructs an empty shared_ptr; otherwise, 
     67      constructs a shared_ptr that shares ownership with other. 
     68    */ 
     69    shared_ptr ( const shared_ptr<T> &other ) : 
     70        payload ( other.payload ), 
     71        refCnt ( other.refCnt ) { 
     72        add_ref(); 
     73    } 
     74 
     75    /*! 
     76      \brief Generalized copy 
     77 
     78      Allows initialization of shared pointer of a base type from 
     79      raw pointer to a derived type. 
     80 
     81      If other is empty, constructs an empty shared_ptr; otherwise, 
     82      constructs a shared_ptr that shares ownership with other. 
     83    */ 
     84    template<typename U> 
     85    shared_ptr ( const shared_ptr<U> &other ) : 
     86        payload ( other.payload ), 
     87        refCnt ( other.refCnt ) { 
     88        add_ref(); 
     89    } 
     90 
     91    /*! 
     92      Destructor. 
     93    */ 
     94    ~shared_ptr() { 
     95        del_ref(); 
     96    } 
     97 
     98    /*! 
     99      \brief Assignment operator 
     100    */ 
     101    shared_ptr<T> &operator= ( const shared_ptr<T> &other ) { 
     102        other.add_ref(); 
     103        del_ref(); 
     104 
     105        payload = other.payload; 
     106        refCnt = other.refCnt; 
     107 
     108        return *this; 
     109    } 
     110 
     111    /*! 
     112      Returns the stored pointer (which remains owned by this 
     113      instance). For empty instances, this method returns NULL. 
     114    */ 
     115    T *get() { 
     116        return payload; 
     117    } 
     118 
     119    /*! 
     120      Dereferences the stored pointer (which remains owned by 
     121      this instance). This method may only be called when the 
     122      stored pointer isn't NULL. 
     123    */ 
     124    T *operator->() { 
     125        bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
     126        return payload; 
     127    } 
     128 
     129    //! Returns a reference to the object pointed to by the stored 
     130    //! pointer. This method may only be called when the stored pointer 
     131    //! isn't NULL. 
     132    T &operator*() { 
     133        bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
     134        return *payload; 
     135    } 
     136 
     137    /*! 
     138      Returns the stored pointer (which remains owned by this 
     139      instance). For empty instances, this method returns NULL. 
     140    */ 
     141    const T* get() const { 
     142        return payload; 
     143    } 
     144 
     145    //! Returns the stored pointer (which remains owned by this 
     146    //! instance). This method may only be called when the stored 
     147    //! pointer isn't NULL. 
     148    const T *operator->() const { 
     149        bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
     150        return payload; 
     151    } 
     152 
     153    //! Returns a reference to the object pointed to by the stored 
     154    //! pointer. This method may only be called when the stored pointer 
     155    //! isn't NULL. 
     156    const T &operator*() const { 
     157        bdm_assert_debug ( payload, "dereferencing NULL shared pointer" ); 
     158        return *payload; 
     159    } 
     160 
     161    //! Returns use_count() == 1 
     162    bool unique() const { 
     163        return refCnt && ( *refCnt == 1 ); 
     164    } 
     165 
     166    /*! 
     167      Returns the number of shared_ptr instances (including 
     168      this instance) that share ownership with this instance. For 
     169      empty instances, this method returns 0. 
     170    */ 
     171    long use_count() const { 
     172        return refCnt ? *refCnt : 0; 
     173    } 
     174 
     175    /*! 
     176      \brief Boolean cast 
     177 
     178      This operator returns true if and only if the instance isn't empty. 
     179    */ 
     180    operator bool() const { 
     181        return !!payload; 
     182    } 
     183 
     184    /*! 
     185      \brief const cast 
     186 
     187      Shared pointer to T can be converted to shared pointer to 
     188      const T, just like T * can be converted to T const *. 
     189    */ 
     190    template<typename U> 
     191    operator shared_ptr<const U>() const { 
     192        shared_ptr<const U> cptr; 
     193        cptr.refCnt = refCnt; 
     194        cptr.payload = payload; 
     195        add_ref(); 
     196        return cptr; 
     197    } 
     198 
     199    /*! 
     200      \brief Efficient swap for shared_ptr. 
     201    */ 
     202    void swap ( shared_ptr &other ) { 
     203        std::swap ( payload, other.payload ); 
     204        std::swap ( refCnt, other.refCnt ); 
     205    } 
    206206 
    207207private: 
    208         void add_ref() const { 
    209                 if ( refCnt ) { 
    210                         if ( *refCnt == UINT_MAX ) { 
    211                                 throw std::overflow_error ( 
    212                                     std::string ( "Shared pointer has too many references." ) ); 
    213                         } 
    214  
    215                         ++*refCnt; 
    216                 } 
    217         } 
    218  
    219         void del_ref() { 
    220                 if ( refCnt ) { 
    221                         if ( ! ( --*refCnt ) ) { 
    222                                 delete payload; 
    223                                 delete refCnt; 
    224                         } 
    225                 } 
    226         } 
     208    void add_ref() const { 
     209        if ( refCnt ) { 
     210            if ( *refCnt == UINT_MAX ) { 
     211                throw std::overflow_error ( 
     212                    std::string ( "Shared pointer has too many references." ) ); 
     213            } 
     214 
     215            ++*refCnt; 
     216        } 
     217    } 
     218 
     219    void del_ref() { 
     220        if ( refCnt ) { 
     221            if ( ! ( --*refCnt ) ) { 
     222                delete payload; 
     223                delete refCnt; 
     224            } 
     225        } 
     226    } 
    227227}; 
    228228 
     
    230230template<typename T, typename U> 
    231231bool operator== ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { 
    232         return a.get() == b.get(); 
     232    return a.get() == b.get(); 
    233233} 
    234234 
     
    236236template<typename T, typename U> 
    237237bool operator!= ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { 
    238         return a.get() != b.get(); 
     238    return a.get() != b.get(); 
    239239} 
    240240 
     
    242242template<typename T, typename U> 
    243243bool operator< ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { 
    244         return a.get() < b.get(); 
     244    return a.get() < b.get(); 
    245245} 
    246246 
     
    255255class object_ptr : public shared_ptr<T> { 
    256256public: 
    257         /*! 
    258           \brief Default constructor 
    259  
    260           Calls T's default constructor. 
    261         */ 
    262         object_ptr() : shared_ptr<T> ( new T() ) { } 
    263  
    264         /*! 
    265           \brief Upcast from shared_ptr<T> to object_ptr<T> 
    266  
    267           \param b The shared pointer, which must not be empty. 
    268         */ 
    269         object_ptr ( const shared_ptr<T> &b ) : shared_ptr<T> ( b ) { 
    270                 bdm_assert_debug ( this->get(), "object_ptr cannot be empty" ); 
    271         } 
    272  
    273         /*! 
    274           Constructs an object_ptr that owns the pointer p. p must 
    275           have been alllocated by new! 
    276         */ 
    277         object_ptr ( T *p ) : shared_ptr<T> ( p ) { 
    278                 bdm_assert_debug ( p, "object_ptr cannot be empty" ); 
    279         } 
    280  
    281         /*! 
    282           \brief Assignment operator 
    283         */ 
    284         object_ptr<T> &operator= ( const object_ptr<T> &other ) { 
    285                 shared_ptr<T>::operator= ( other ); 
    286                 return *this; 
    287         } 
     257    /*! 
     258      \brief Default constructor 
     259 
     260      Calls T's default constructor. 
     261    */ 
     262    object_ptr() : shared_ptr<T> ( new T() ) { } 
     263 
     264    /*! 
     265      \brief Upcast from shared_ptr<T> to object_ptr<T> 
     266 
     267      \param b The shared pointer, which must not be empty. 
     268    */ 
     269    object_ptr ( const shared_ptr<T> &b ) : shared_ptr<T> ( b ) { 
     270        bdm_assert_debug ( this->get(), "object_ptr cannot be empty" ); 
     271    } 
     272 
     273    /*! 
     274      Constructs an object_ptr that owns the pointer p. p must 
     275      have been alllocated by new! 
     276    */ 
     277    object_ptr ( T *p ) : shared_ptr<T> ( p ) { 
     278        bdm_assert_debug ( p, "object_ptr cannot be empty" ); 
     279    } 
     280 
     281    /*! 
     282      \brief Assignment operator 
     283    */ 
     284    object_ptr<T> &operator= ( const object_ptr<T> &other ) { 
     285        shared_ptr<T>::operator= ( other ); 
     286        return *this; 
     287    } 
    288288}; 
    289289