diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
| commit | ac517a2b7625e0aa0862679e961c6349f859ea3b (patch) | |
| tree | e3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/sptr.h | |
| parent | f8d4301e9fa4f3709258505941e37fab2eadadc6 (diff) | |
| parent | bd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff) | |
| download | libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2 libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip | |
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to '')
| -rw-r--r-- | src/sptr.h | 272 |
1 files changed, 160 insertions, 112 deletions
| @@ -1,144 +1,192 @@ | |||
| 1 | #ifndef SPTR_H | 1 | #ifndef BU_SPTR_H |
| 2 | #define SPTR_H | 2 | #define BU_SPTR_H |
| 3 | 3 | ||
| 4 | #include <stdint.h> | 4 | #include <stdint.h> |
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | 6 | ||
| 7 | template<typename T> class SPtr; | 7 | namespace Bu |
| 8 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ); | ||
| 9 | |||
| 10 | template<typename T> | ||
| 11 | class SPtr | ||
| 12 | { | 8 | { |
| 13 | template<typename Tb, typename Ta> | 9 | template<typename T> class SPtr; |
| 14 | friend SPtr<Tb> SPtrCast( SPtr<Ta> pt ); | 10 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ); |
| 15 | public: | 11 | |
| 16 | SPtr() : | 12 | template<typename T> |
| 17 | pRefCnt( NULL ), | 13 | class SPtr |
| 18 | pData( NULL ) | 14 | { |
| 19 | { | 15 | template<typename Tb, typename Ta> |
| 20 | } | 16 | friend SPtr<Tb> SPtrCast( SPtr<Ta> pt ); |
| 17 | public: | ||
| 18 | SPtr() : | ||
| 19 | pRefCnt( NULL ), | ||
| 20 | pData( NULL ) | ||
| 21 | { | ||
| 22 | } | ||
| 21 | 23 | ||
| 22 | ~SPtr() | 24 | ~SPtr() |
| 23 | { | 25 | { |
| 24 | decCount(); | 26 | decCount(); |
| 25 | } | 27 | } |
| 26 | 28 | ||
| 27 | SPtr( const SPtr<T> &src ) : | 29 | SPtr( const SPtr<T> &src ) : |
| 28 | pRefCnt( src.pRefCnt ), | 30 | pRefCnt( src.pRefCnt ), |
| 29 | pData( src.pData ) | 31 | pData( src.pData ) |
| 30 | { | 32 | { |
| 31 | if( pRefCnt ) | 33 | if( pRefCnt ) |
| 32 | (*pRefCnt) += 1; | 34 | (*pRefCnt) += 1; |
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | SPtr( T *pSrc ) : | 37 | SPtr( T *pSrc ) : |
| 36 | pRefCnt( NULL ), | 38 | pRefCnt( NULL ), |
| 37 | pData( pSrc ) | 39 | pData( pSrc ) |
| 38 | { | ||
| 39 | if( pData ) | ||
| 40 | { | 40 | { |
| 41 | pRefCnt = new int32_t; | 41 | if( pData ) |
| 42 | (*pRefCnt) = 1; | 42 | { |
| 43 | pRefCnt = new int32_t; | ||
| 44 | (*pRefCnt) = 1; | ||
| 45 | } | ||
| 43 | } | 46 | } |
| 44 | } | ||
| 45 | 47 | ||
| 46 | int32_t count() const | 48 | /** |
| 47 | { | 49 | * Get the number of references to this pointer. |
| 48 | return *pRefCnt; | 50 | *@returns (int32_t) The number of references to this pointer. |
| 49 | } | 51 | */ |
| 52 | int32_t count() const | ||
| 53 | { | ||
| 54 | return *pRefCnt; | ||
| 55 | } | ||
| 50 | 56 | ||
| 51 | const T *operator->() const | 57 | /** |
| 52 | { | 58 | * Pointer access operator. |
| 53 | return pData; | 59 | *@returns (const T *) |
| 54 | } | 60 | */ |
| 61 | const T *operator->() const | ||
| 62 | { | ||
| 63 | return pData; | ||
| 64 | } | ||
| 55 | 65 | ||
| 56 | const T &operator*() const | 66 | /** |
| 57 | { | 67 | * Dereference operator. |
| 58 | return *pData; | 68 | *@returns (const T &) The value at the end of the pointer. |
| 59 | } | 69 | */ |
| 60 | 70 | const T &operator*() const | |
| 61 | T *operator->() | 71 | { |
| 62 | { | 72 | return *pData; |
| 63 | return pData; | 73 | } |
| 64 | } | 74 | |
| 75 | /** | ||
| 76 | * Pointer access operator. | ||
| 77 | *@returns (T *) | ||
| 78 | */ | ||
| 79 | T *operator->() | ||
| 80 | { | ||
| 81 | return pData; | ||
| 82 | } | ||
| 65 | 83 | ||
| 66 | T &operator*() | 84 | /** |
| 67 | { | 85 | * Dereference operator. |
| 68 | return *pData; | 86 | *@returns (T &) The value at the end of the pointer. |
| 69 | } | 87 | */ |
| 88 | T &operator*() | ||
| 89 | { | ||
| 90 | return *pData; | ||
| 91 | } | ||
| 70 | 92 | ||
| 71 | SPtr<T> operator=( const SPtr<T> &src ) | 93 | /** |
| 72 | { | 94 | * Assignment operator. |
| 73 | decCount(); | 95 | *@param src (const SPtr<T> &) |
| 74 | pRefCnt = src.pRefCnt; | 96 | */ |
| 75 | pData = src.pData; | 97 | SPtr<T> operator=( const SPtr<T> &src ) |
| 76 | if( pRefCnt ) | 98 | { |
| 77 | (*pRefCnt) += 1; | 99 | decCount(); |
| 100 | pRefCnt = src.pRefCnt; | ||
| 101 | pData = src.pData; | ||
| 102 | if( pRefCnt ) | ||
| 103 | (*pRefCnt) += 1; | ||
| 78 | 104 | ||
| 79 | return *this; | 105 | return *this; |
| 80 | } | 106 | } |
| 81 | 107 | ||
| 82 | const SPtr<T> operator=( const SPtr<T> &src ) const | 108 | /** |
| 83 | { | 109 | * Assignment operator. |
| 84 | decCount(); | 110 | *@param src (const SPtr<T> &) |
| 85 | pRefCnt = src.pRefCnt; | 111 | */ |
| 86 | pData = src.pData; | 112 | const SPtr<T> operator=( const SPtr<T> &src ) const |
| 87 | if( pRefCnt ) | 113 | { |
| 88 | (*pRefCnt) += 1; | 114 | decCount(); |
| 115 | pRefCnt = src.pRefCnt; | ||
| 116 | pData = src.pData; | ||
| 117 | if( pRefCnt ) | ||
| 118 | (*pRefCnt) += 1; | ||
| 89 | 119 | ||
| 90 | return *this; | 120 | return *this; |
| 91 | } | 121 | } |
| 92 | 122 | ||
| 93 | bool operator==( const SPtr<T> &src ) const | 123 | /** |
| 94 | { | 124 | * Equals comparison operator. |
| 95 | return pData == src.pData; | 125 | *@param src (const SPtr<T> &) The SPtr to compare to. |
| 96 | } | 126 | *@returns (bool) Are the equal? |
| 127 | */ | ||
| 128 | bool operator==( const SPtr<T> &src ) const | ||
| 129 | { | ||
| 130 | return pData == src.pData; | ||
| 131 | } | ||
| 97 | 132 | ||
| 98 | bool operator==( const T *src ) const | 133 | /** |
| 99 | { | 134 | * Equals comparison operator. |
| 100 | return pData == src; | 135 | *@param src (const T *) The pointer to compare to. |
| 101 | } | 136 | *@returns (bool) Are the equal? |
| 137 | */ | ||
| 138 | bool operator==( const T *src ) const | ||
| 139 | { | ||
| 140 | return pData == src; | ||
| 141 | } | ||
| 102 | 142 | ||
| 103 | operator bool() const | 143 | /** |
| 104 | { | 144 | * Boolean cast operator. Do we have a pointer? |
| 105 | return pRefCnt != NULL; | 145 | */ |
| 106 | } | 146 | operator bool() const |
| 147 | { | ||
| 148 | return pRefCnt != NULL; | ||
| 149 | } | ||
| 107 | 150 | ||
| 108 | bool isSet() const | 151 | /** |
| 109 | { | 152 | * Do we have a pointer? |
| 110 | return pRefCnt != NULL; | 153 | *@returns (bool) Do we have a pointer? |
| 111 | } | 154 | */ |
| 155 | bool isSet() const | ||
| 156 | { | ||
| 157 | return pRefCnt != NULL; | ||
| 158 | } | ||
| 112 | 159 | ||
| 113 | private: | 160 | private: |
| 114 | void decCount() const | 161 | void decCount() const |
| 115 | { | ||
| 116 | if( pRefCnt ) | ||
| 117 | { | 162 | { |
| 118 | (*pRefCnt) -= 1; | 163 | if( pRefCnt ) |
| 119 | //printf("Decrementing ref-count to %d\n", *pRefCnt ); | ||
| 120 | if( (*pRefCnt) == 0 ) | ||
| 121 | { | 164 | { |
| 122 | delete pRefCnt; | 165 | (*pRefCnt) -= 1; |
| 123 | delete pData; | 166 | //printf("Decrementing ref-count to %d\n", *pRefCnt ); |
| 124 | pRefCnt = NULL; | 167 | if( (*pRefCnt) == 0 ) |
| 125 | pData = NULL; | 168 | { |
| 169 | delete pRefCnt; | ||
| 170 | delete pData; | ||
| 171 | pRefCnt = NULL; | ||
| 172 | pData = NULL; | ||
| 173 | } | ||
| 126 | } | 174 | } |
| 127 | } | 175 | } |
| 128 | } | ||
| 129 | 176 | ||
| 130 | mutable int32_t *pRefCnt; | 177 | mutable int32_t *pRefCnt; |
| 131 | mutable T *pData; | 178 | mutable T *pData; |
| 132 | }; | 179 | }; |
| 133 | 180 | ||
| 134 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ) | 181 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ) |
| 135 | { | 182 | { |
| 136 | SPtr<Tb> ret; | 183 | SPtr<Tb> ret; |
| 137 | ret.pRefCnt = src.pRefCnt; | 184 | ret.pRefCnt = src.pRefCnt; |
| 138 | ret.pData = dynamic_cast<Tb *>(src.pData); | 185 | ret.pData = dynamic_cast<Tb *>(src.pData); |
| 139 | if( ret.pRefCnt ) | 186 | if( ret.pRefCnt ) |
| 140 | (*(ret.pRefCnt)) += 1; | 187 | (*(ret.pRefCnt)) += 1; |
| 141 | return ret; | 188 | return ret; |
| 189 | } | ||
| 142 | } | 190 | } |
| 143 | 191 | ||
| 144 | #endif | 192 | #endif |
