diff options
| author | manuel <manuel@nc8430.lan> | 2009-06-13 22:20:39 +0200 |
|---|---|---|
| committer | manuel <manuel@nc8430.lan> | 2009-06-13 22:20:39 +0200 |
| commit | 1243cb834e83c350dbee887d096e551406ec31e2 (patch) | |
| tree | 56670e2945b7753ef24433dcbc5dbae91014ba00 | |
| parent | 4be4cac77dfa62a8a864f13c96c2a588ca8388c5 (diff) | |
| download | ooprog-1243cb834e83c350dbee887d096e551406ec31e2.tar.gz ooprog-1243cb834e83c350dbee887d096e551406ec31e2.tar.bz2 ooprog-1243cb834e83c350dbee887d096e551406ec31e2.zip | |
| -rw-r--r-- | ue5/array.hpp | 181 | ||||
| -rw-r--r-- | ue5/mean_mark.hpp | 51 | ||||
| -rw-r--r-- | ue5/shared_ptr.hpp | 282 |
3 files changed, 508 insertions, 6 deletions
diff --git a/ue5/array.hpp b/ue5/array.hpp index 0acd37d..a78ff56 100644 --- a/ue5/array.hpp +++ b/ue5/array.hpp | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /** | ||
| 2 | * @module array | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Class template array | ||
| 5 | * @date 13.06.2009 | ||
| 6 | */ | ||
| 7 | |||
| 1 | #ifndef ARRAY_H | 8 | #ifndef ARRAY_H |
| 2 | #define ARRAY_H | 9 | #define ARRAY_H |
| 3 | 10 | ||
| @@ -10,72 +17,208 @@ | |||
| 10 | 17 | ||
| 11 | namespace Ti | 18 | namespace Ti |
| 12 | { | 19 | { |
| 20 | /** | ||
| 21 | * @class array | ||
| 22 | * | ||
| 23 | * Array defines a class template for storing fixed-size sequences of objects. | ||
| 24 | */ | ||
| 13 | template<typename T, std::size_t N> | 25 | template<typename T, std::size_t N> |
| 14 | struct array | 26 | struct array |
| 15 | { | 27 | { |
| 28 | /** lvalue of T */ | ||
| 16 | typedef T& reference; | 29 | typedef T& reference; |
| 30 | /** constant lvalue of T */ | ||
| 17 | typedef const T& const_reference; | 31 | typedef const T& const_reference; |
| 32 | /** iterator type whose value type is T */ | ||
| 18 | typedef T* iterator; | 33 | typedef T* iterator; |
| 34 | /** constant iterator type whose value type is T */ | ||
| 19 | typedef const T* const_iterator; | 35 | typedef const T* const_iterator; |
| 36 | /** unsigned integral type */ | ||
| 20 | typedef std::size_t size_type; | 37 | typedef std::size_t size_type; |
| 21 | 38 | ||
| 39 | /* we don't suppport zero-sized arrays */ | ||
| 22 | static_assert(N != 0, "array<T, 0> not allowed"); | 40 | static_assert(N != 0, "array<T, 0> not allowed"); |
| 23 | 41 | ||
| 42 | /** | ||
| 43 | * @method fill | ||
| 44 | * @brief Fills container with elements of u | ||
| 45 | * @param u object to fill array with | ||
| 46 | * @return return N | ||
| 47 | * @globalvars none | ||
| 48 | * @exception none | ||
| 49 | * @pre requires CopyAssignable<T> void fill(const T& u) | ||
| 50 | * @post none | ||
| 51 | */ | ||
| 24 | void fill(const T& u) | 52 | void fill(const T& u) |
| 25 | { | 53 | { |
| 26 | std::fill_n(begin(), N, u); | 54 | std::fill_n(begin(), N, u); |
| 27 | } | 55 | } |
| 28 | 56 | ||
| 29 | void swap(array<T,N> & other) | 57 | /** |
| 58 | * @method swap | ||
| 59 | * @brief Exchanges the contents of *this and other. | ||
| 60 | * @param other object to swap with | ||
| 61 | * @return - | ||
| 62 | * @globalvars none | ||
| 63 | * @exception none | ||
| 64 | * @pre none | ||
| 65 | * @post none | ||
| 66 | */ | ||
| 67 | void swap(array<T,N>& other) | ||
| 30 | { | 68 | { |
| 31 | std::swap_ranges(begin(), end(), other.begin()); | 69 | std::swap_ranges(begin(), end(), other.begin()); |
| 32 | } | 70 | } |
| 33 | 71 | ||
| 72 | /** | ||
| 73 | * @method begin | ||
| 74 | * @brief Get an iterator referring to the first element in the container | ||
| 75 | * @param - | ||
| 76 | * @return iterator referring to the first element in the container. | ||
| 77 | * @globalvars none | ||
| 78 | * @exception none | ||
| 79 | * @pre none | ||
| 80 | * @post none | ||
| 81 | */ | ||
| 34 | iterator begin() | 82 | iterator begin() |
| 35 | { | 83 | { |
| 36 | return m_data; | 84 | return m_data; |
| 37 | } | 85 | } |
| 38 | 86 | ||
| 87 | /** | ||
| 88 | * @method begin | ||
| 89 | * @brief Get a constant iterator referring to the first element in the | ||
| 90 | * container | ||
| 91 | * @param - | ||
| 92 | * @return constant iterator referring to the first element in the container. | ||
| 93 | * @globalvars none | ||
| 94 | * @exception none | ||
| 95 | * @pre none | ||
| 96 | * @post none | ||
| 97 | */ | ||
| 39 | const_iterator begin() const | 98 | const_iterator begin() const |
| 40 | { | 99 | { |
| 41 | return m_data; | 100 | return m_data; |
| 42 | } | 101 | } |
| 43 | 102 | ||
| 103 | /** | ||
| 104 | * @method end | ||
| 105 | * @brief Get an iterator which is the past-the-end value for the container. | ||
| 106 | * @param - | ||
| 107 | * @return iterator which is the past-the-end value for the container | ||
| 108 | * @globalvars none | ||
| 109 | * @exception none | ||
| 110 | * @pre none | ||
| 111 | * @post none | ||
| 112 | */ | ||
| 44 | iterator end() | 113 | iterator end() |
| 45 | { | 114 | { |
| 46 | return m_data + N; | 115 | return m_data + N; |
| 47 | } | 116 | } |
| 48 | 117 | ||
| 118 | /** | ||
| 119 | * @method end | ||
| 120 | * @brief Get a constant iterator which is the past-the-end value for the | ||
| 121 | * container. | ||
| 122 | * @param - | ||
| 123 | * @return constant iterator which is the past-the-end value for the container | ||
| 124 | * @globalvars none | ||
| 125 | * @exception none | ||
| 126 | * @pre none | ||
| 127 | * @post none | ||
| 128 | */ | ||
| 49 | const_iterator end() const | 129 | const_iterator end() const |
| 50 | { | 130 | { |
| 51 | return m_data + N; | 131 | return m_data + N; |
| 52 | } | 132 | } |
| 53 | 133 | ||
| 134 | /** | ||
| 135 | * @method size | ||
| 136 | * @brief Get number of elements in the container | ||
| 137 | * @param - | ||
| 138 | * @return return N | ||
| 139 | * @globalvars none | ||
| 140 | * @exception none | ||
| 141 | * @pre none | ||
| 142 | * @post none | ||
| 143 | */ | ||
| 54 | size_type size() const | 144 | size_type size() const |
| 55 | { | 145 | { |
| 56 | return N; | 146 | return N; |
| 57 | } | 147 | } |
| 58 | 148 | ||
| 149 | /** | ||
| 150 | * @method max_size | ||
| 151 | * @brief Get maximal number of elements which fit into the container | ||
| 152 | * @param - | ||
| 153 | * @return return N | ||
| 154 | * @globalvars none | ||
| 155 | * @exception none | ||
| 156 | * @pre none | ||
| 157 | * @post none | ||
| 158 | */ | ||
| 59 | size_type max_size() const | 159 | size_type max_size() const |
| 60 | { | 160 | { |
| 61 | return N; | 161 | return N; |
| 62 | } | 162 | } |
| 63 | 163 | ||
| 164 | /** | ||
| 165 | * @method empty | ||
| 166 | * @brief Check if container is empty | ||
| 167 | * @param - | ||
| 168 | * @return always false | ||
| 169 | * @globalvars none | ||
| 170 | * @exception none | ||
| 171 | * @pre none | ||
| 172 | * @post none | ||
| 173 | */ | ||
| 64 | bool empty() const | 174 | bool empty() const |
| 65 | { | 175 | { |
| 66 | return false; /* size() == 0 */ | 176 | return false; /* size() == 0 */ |
| 67 | } | 177 | } |
| 68 | 178 | ||
| 179 | /** | ||
| 180 | * @method operator[] | ||
| 181 | * @brief operator [] | ||
| 182 | * @param n position of an element in the array | ||
| 183 | * @return reference to the element at position n in the container | ||
| 184 | * @globalvars none | ||
| 185 | * @exception none | ||
| 186 | * @pre none | ||
| 187 | * @post none | ||
| 188 | */ | ||
| 69 | reference operator[](size_type n) | 189 | reference operator[](size_type n) |
| 70 | { | 190 | { |
| 71 | return m_data[n]; | 191 | return m_data[n]; |
| 72 | } | 192 | } |
| 73 | 193 | ||
| 194 | /** | ||
| 195 | * @method operator[] | ||
| 196 | * @brief operator [] | ||
| 197 | * @param n position of an element in the array | ||
| 198 | * @return constant reference to the element at position n in the container | ||
| 199 | * @globalvars none | ||
| 200 | * @exception none | ||
| 201 | * @pre none | ||
| 202 | * @post none | ||
| 203 | */ | ||
| 74 | const_reference operator[](size_type n) const | 204 | const_reference operator[](size_type n) const |
| 75 | { | 205 | { |
| 76 | return m_data[n]; | 206 | return m_data[n]; |
| 77 | } | 207 | } |
| 78 | 208 | ||
| 209 | /** | ||
| 210 | * @method at | ||
| 211 | * @brief Get reference to the element at position n | ||
| 212 | * @param n position of an element in the array | ||
| 213 | * @return reference to the element at position n in the container. | ||
| 214 | * The difference between this member function and member operator | ||
| 215 | * function operator[] is that at signals if the requested | ||
| 216 | * position is out of range by throwing an out_of_range exception. | ||
| 217 | * @globalvars none | ||
| 218 | * @exception out_of_range | ||
| 219 | * @pre none | ||
| 220 | * @post none | ||
| 221 | */ | ||
| 79 | reference at(size_type n) | 222 | reference at(size_type n) |
| 80 | { | 223 | { |
| 81 | if (n >= N) | 224 | if (n >= N) |
| @@ -83,6 +226,19 @@ namespace Ti | |||
| 83 | return m_data[n]; | 226 | return m_data[n]; |
| 84 | } | 227 | } |
| 85 | 228 | ||
| 229 | /** | ||
| 230 | * @method at | ||
| 231 | * @brief Get constant reference to the element at position n | ||
| 232 | * @param n position of an element in the array | ||
| 233 | * @return constant reference to the element at position n in the container. | ||
| 234 | * The difference between this member function and member operator | ||
| 235 | * function operator[] is that at signals if the requested | ||
| 236 | * position is out of range by throwing an out_of_range exception. | ||
| 237 | * @globalvars none | ||
| 238 | * @exception out_of_range | ||
| 239 | * @pre none | ||
| 240 | * @post none | ||
| 241 | */ | ||
| 86 | const_reference at(size_type n) const | 242 | const_reference at(size_type n) const |
| 87 | { | 243 | { |
| 88 | if (n >= N) | 244 | if (n >= N) |
| @@ -90,22 +246,41 @@ namespace Ti | |||
| 90 | return m_data[n]; | 246 | return m_data[n]; |
| 91 | } | 247 | } |
| 92 | 248 | ||
| 249 | /** | ||
| 250 | * @method data | ||
| 251 | * @brief Check if container is empty | ||
| 252 | * @param - | ||
| 253 | * @return returns enclosed array | ||
| 254 | * @globalvars none | ||
| 255 | * @exception none | ||
| 256 | * @pre none | ||
| 257 | * @post none | ||
| 258 | */ | ||
| 93 | T* data() | 259 | T* data() |
| 94 | { | 260 | { |
| 95 | return m_data; | 261 | return m_data; |
| 96 | } | 262 | } |
| 97 | 263 | ||
| 98 | private: | 264 | private: |
| 265 | /** enclosed array */ | ||
| 99 | T m_data[N]; | 266 | T m_data[N]; |
| 100 | }; | 267 | }; |
| 101 | 268 | ||
| 102 | /* std::move returns lvalue as rvalue */ | 269 | /** |
| 270 | * @method make_array | ||
| 271 | * @brief Get an rvalue of an empty array | ||
| 272 | * @param none | ||
| 273 | * @return rvalue of an empty array | ||
| 274 | * @globalvars none | ||
| 275 | * @exception none | ||
| 276 | * @pre none | ||
| 277 | * @post none | ||
| 278 | */ | ||
| 103 | template<typename T, std::size_t N> | 279 | template<typename T, std::size_t N> |
| 104 | array<T, N>&& make_array() | 280 | array<T, N>&& make_array() |
| 105 | { | 281 | { |
| 106 | return std::move(array<T, N>()); | 282 | return std::move(array<T, N>()); |
| 107 | } | 283 | } |
| 108 | |||
| 109 | } // namespace | 284 | } // namespace |
| 110 | 285 | ||
| 111 | #endif | 286 | #endif |
diff --git a/ue5/mean_mark.hpp b/ue5/mean_mark.hpp index e07aedc..614a671 100644 --- a/ue5/mean_mark.hpp +++ b/ue5/mean_mark.hpp | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /** | ||
| 2 | * @module mean_mark | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Templates for mean_mark, mean_mark_student, remove_greater | ||
| 5 | * @date 13.06.2009 | ||
| 6 | */ | ||
| 7 | |||
| 1 | #ifndef MEAN_MARK_H | 8 | #ifndef MEAN_MARK_H |
| 2 | #define MEAN_MARK_H | 9 | #define MEAN_MARK_H |
| 3 | 10 | ||
| @@ -9,7 +16,18 @@ | |||
| 9 | 16 | ||
| 10 | namespace Ti | 17 | namespace Ti |
| 11 | { | 18 | { |
| 12 | 19 | /** | |
| 20 | * @method mean_mark | ||
| 21 | * @brief computes mean mark in the range [first,last) | ||
| 22 | * @param first forward iterator to the initial positions in a sequence | ||
| 23 | * @param last forward iterator to the final positions in a sequence | ||
| 24 | * @return computed mean mark | ||
| 25 | * @globalvars none | ||
| 26 | * @exception none | ||
| 27 | * @pre all objects in the sequence must have a method mark returning a type | ||
| 28 | * convertible to double | ||
| 29 | * @post none | ||
| 30 | */ | ||
| 13 | template <typename Iter> | 31 | template <typename Iter> |
| 14 | double mean_mark(Iter first, Iter last) | 32 | double mean_mark(Iter first, Iter last) |
| 15 | { | 33 | { |
| @@ -23,6 +41,19 @@ namespace Ti | |||
| 23 | return (count == 0) ? 0 : result / count; | 41 | return (count == 0) ? 0 : result / count; |
| 24 | } | 42 | } |
| 25 | 43 | ||
| 44 | /** | ||
| 45 | * @method mean_mark_student | ||
| 46 | * @brief computes mean mark of objects of type Student in the range [first,last) | ||
| 47 | * (using RTTI) | ||
| 48 | * @param first forward iterator to the initial positions in a sequence | ||
| 49 | * @param last forward iterator to the final positions in a sequence | ||
| 50 | * @return computed mean mark of objects of type Student | ||
| 51 | * @globalvars none | ||
| 52 | * @exception none | ||
| 53 | * @pre All objects in the sequence must have a method mark returning a type | ||
| 54 | * convertible to double. And type Stundent must exist | ||
| 55 | * @post none | ||
| 56 | */ | ||
| 26 | template <typename Iter> | 57 | template <typename Iter> |
| 27 | double mean_mark_student(Iter first, Iter last) | 58 | double mean_mark_student(Iter first, Iter last) |
| 28 | { | 59 | { |
| @@ -41,6 +72,23 @@ namespace Ti | |||
| 41 | return (count == 0) ? 0 : result / count; | 72 | return (count == 0) ? 0 : result / count; |
| 42 | } | 73 | } |
| 43 | 74 | ||
| 75 | /** | ||
| 76 | * @method remove_greater | ||
| 77 | * @brief Removes from the range [first,last) the elements with a mark greater | ||
| 78 | * than mark and returns an iterator to the new end of the range, | ||
| 79 | * which now includes only elements with a mark less than mark. | ||
| 80 | * @param first forward iterator to the initial positions in a sequence | ||
| 81 | * @param last forward iterator to the final positions in a sequence | ||
| 82 | * @param mark maximal value for mark to keep | ||
| 83 | * @return A forward iterator pointing to the new end of the sequence, | ||
| 84 | * which now includes all the elements with a mark less than mark. | ||
| 85 | * @globalvars none | ||
| 86 | * @exception none | ||
| 87 | * @pre All objects in the sequence must have a method mark returning a type | ||
| 88 | * convertible to double. | ||
| 89 | * @post This function does not alter the elements past the new end, | ||
| 90 | * which keep their old values and are still accessible. | ||
| 91 | */ | ||
| 44 | template <class ForwardIterator> | 92 | template <class ForwardIterator> |
| 45 | ForwardIterator remove_greater (ForwardIterator first, ForwardIterator last, int mark) | 93 | ForwardIterator remove_greater (ForwardIterator first, ForwardIterator last, int mark) |
| 46 | { | 94 | { |
| @@ -55,7 +103,6 @@ namespace Ti | |||
| 55 | } | 103 | } |
| 56 | return result; | 104 | return result; |
| 57 | } | 105 | } |
| 58 | |||
| 59 | } | 106 | } |
| 60 | 107 | ||
| 61 | #endif | 108 | #endif |
diff --git a/ue5/shared_ptr.hpp b/ue5/shared_ptr.hpp index 37989e3..7f502bd 100644 --- a/ue5/shared_ptr.hpp +++ b/ue5/shared_ptr.hpp | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /** | ||
| 2 | * @module shared_ptr | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Class template shared_ptr | ||
| 5 | * @date 13.06.2009 | ||
| 6 | */ | ||
| 7 | |||
| 1 | #ifndef SHARED_PTR_H | 8 | #ifndef SHARED_PTR_H |
| 2 | #define SHARED_PTR_H | 9 | #define SHARED_PTR_H |
| 3 | 10 | ||
| @@ -8,29 +15,85 @@ | |||
| 8 | 15 | ||
| 9 | namespace Ti | 16 | namespace Ti |
| 10 | { | 17 | { |
| 18 | /** | ||
| 19 | * @class shared_ptr | ||
| 20 | * | ||
| 21 | * The templates stores a pointer, usually obtained via new. | ||
| 22 | * shared_ptr implements semantics of shared ownership; the last remaining | ||
| 23 | * owner of the pointer is responsible for destroying the object, or | ||
| 24 | * otherwise releasing the resources associated with the stored pointer. | ||
| 25 | * A shared_ptr object is empty if it does not own a pointer. | ||
| 26 | */ | ||
| 11 | template <typename T> | 27 | template <typename T> |
| 12 | class shared_ptr | 28 | class shared_ptr |
| 13 | { | 29 | { |
| 14 | private: | 30 | private: |
| 31 | /** stored pointer */ | ||
| 15 | T* m_ptr; | 32 | T* m_ptr; |
| 33 | /** reference counter */ | ||
| 16 | unsigned long* m_count; | 34 | unsigned long* m_count; |
| 17 | 35 | ||
| 18 | public: | 36 | public: |
| 37 | /** | ||
| 38 | * @method shared_ptr | ||
| 39 | * @brief Default ctor | ||
| 40 | * @param - | ||
| 41 | * @return - | ||
| 42 | * @globalvars none | ||
| 43 | * @exception none | ||
| 44 | * @pre none | ||
| 45 | * @post get() == m_count == NULL | ||
| 46 | */ | ||
| 19 | shared_ptr() | 47 | shared_ptr() |
| 20 | : m_ptr(NULL), m_count(NULL) | 48 | : m_ptr(NULL), m_count(NULL) |
| 21 | {} | 49 | {} |
| 22 | 50 | ||
| 51 | /** | ||
| 52 | * @method get | ||
| 53 | * @brief Returns pointer of enclosed object | ||
| 54 | * @param - | ||
| 55 | * @return returns the stored pointer, null pointer if *this is empty. | ||
| 56 | * @globalvars none | ||
| 57 | * @exception none | ||
| 58 | * @pre none | ||
| 59 | * @post none | ||
| 60 | */ | ||
| 23 | T* get() const | 61 | T* get() const |
| 24 | { | 62 | { |
| 25 | return m_ptr; | 63 | return m_ptr; |
| 26 | } | 64 | } |
| 27 | 65 | ||
| 66 | /** | ||
| 67 | * @method shared_ptr | ||
| 68 | * @brief Copy ctor. If r is empty, constructs an empty shared_ptr | ||
| 69 | * object - otherwise, constructs a shared_ptr object that | ||
| 70 | * shares ownership with r. | ||
| 71 | * @param other instance to copy from | ||
| 72 | * @return - | ||
| 73 | * @globalvars none | ||
| 74 | * @exception none | ||
| 75 | * @pre none | ||
| 76 | * @post get() == other.get() && m_count == other.m_count | ||
| 77 | */ | ||
| 28 | shared_ptr(const shared_ptr<T>& other) | 78 | shared_ptr(const shared_ptr<T>& other) |
| 29 | : m_ptr(other.m_ptr), m_count(other.m_count) | 79 | : m_ptr(other.m_ptr), m_count(other.m_count) |
| 30 | { | 80 | { |
| 31 | add_ref(); | 81 | add_ref(); |
| 32 | } | 82 | } |
| 33 | 83 | ||
| 84 | /** | ||
| 85 | * @method shared_ptr | ||
| 86 | * @brief Copy ctor. If other is empty, constructs an empty shared_ptr | ||
| 87 | * object - otherwise, constructs a shared_ptr object that | ||
| 88 | * shares ownership with other. | ||
| 89 | * @param other instance to copy from | ||
| 90 | * @return - | ||
| 91 | * @globalvars none | ||
| 92 | * @exception none | ||
| 93 | * @pre The constructor shall not participate in the overload | ||
| 94 | * resolution unless O* is implicitly convertible to T*. | ||
| 95 | * @post get() == other.get() && m_count == other.m_count | ||
| 96 | */ | ||
| 34 | template <typename O> | 97 | template <typename O> |
| 35 | shared_ptr(const shared_ptr<O>& other) | 98 | shared_ptr(const shared_ptr<O>& other) |
| 36 | : m_ptr(other.m_ptr), m_count(other.m_count) | 99 | : m_ptr(other.m_ptr), m_count(other.m_count) |
| @@ -38,7 +101,18 @@ namespace Ti | |||
| 38 | add_ref(); | 101 | add_ref(); |
| 39 | } | 102 | } |
| 40 | 103 | ||
| 41 | /* used to construct shared_ptr with a sub-object of O */ | 104 | /** |
| 105 | * @method shared_ptr | ||
| 106 | * @brief Copy ctor. Constructs a shared_ptr instance that stores ptr | ||
| 107 | * and shares ownership with other. | ||
| 108 | * @param other instance to copy from | ||
| 109 | * @param ptr ptr to store | ||
| 110 | * @return - | ||
| 111 | * @globalvars none | ||
| 112 | * @exception none | ||
| 113 | * @pre none | ||
| 114 | * @post get() == p && m_count == other.m_count | ||
| 115 | */ | ||
| 42 | template <typename O> | 116 | template <typename O> |
| 43 | shared_ptr(const shared_ptr<O>& other, T* ptr) | 117 | shared_ptr(const shared_ptr<O>& other, T* ptr) |
| 44 | : m_ptr(ptr), m_count(other.m_count) | 118 | : m_ptr(ptr), m_count(other.m_count) |
| @@ -46,11 +120,33 @@ namespace Ti | |||
| 46 | add_ref(); | 120 | add_ref(); |
| 47 | } | 121 | } |
| 48 | 122 | ||
| 123 | /** | ||
| 124 | * @method shared_ptr | ||
| 125 | * @brief Copy ctor. Constructs a shared_ptr object that owns the pointer p. | ||
| 126 | * @param p pointer to store | ||
| 127 | * @return - | ||
| 128 | * @globalvars none | ||
| 129 | * @exception bad_alloc if memory couldn't be optained | ||
| 130 | * @pre p shall be convertible to T*. Y shall be a complete type. The | ||
| 131 | * expression delete p shall be well formed, shall have well | ||
| 132 | * defined behavior, and shall not throw exceptions. | ||
| 133 | * @post get() == p && m_count == 1 | ||
| 134 | */ | ||
| 49 | template <typename O> | 135 | template <typename O> |
| 50 | explicit shared_ptr(O* p) | 136 | explicit shared_ptr(O* p) |
| 51 | : m_ptr(p), m_count(new unsigned long(1)) | 137 | : m_ptr(p), m_count(new unsigned long(1)) |
| 52 | {} | 138 | {} |
| 53 | 139 | ||
| 140 | /** | ||
| 141 | * @method operator= | ||
| 142 | * @brief operator = | ||
| 143 | * @param other object to compare with | ||
| 144 | * @return always *this | ||
| 145 | * @globalvars none | ||
| 146 | * @exception none | ||
| 147 | * @pre none | ||
| 148 | * @post none | ||
| 149 | */ | ||
| 54 | shared_ptr& operator=(const shared_ptr<T>& other) | 150 | shared_ptr& operator=(const shared_ptr<T>& other) |
| 55 | { | 151 | { |
| 56 | if (*this == other) | 152 | if (*this == other) |
| @@ -62,6 +158,16 @@ namespace Ti | |||
| 62 | return *this; | 158 | return *this; |
| 63 | } | 159 | } |
| 64 | 160 | ||
| 161 | /** | ||
| 162 | * @method operator= | ||
| 163 | * @brief operator = | ||
| 164 | * @param other object to compare with | ||
| 165 | * @return always *this | ||
| 166 | * @globalvars none | ||
| 167 | * @exception none | ||
| 168 | * @pre none | ||
| 169 | * @post none | ||
| 170 | */ | ||
| 65 | template <typename O> | 171 | template <typename O> |
| 66 | shared_ptr& operator=(const shared_ptr<O>& other) | 172 | shared_ptr& operator=(const shared_ptr<O>& other) |
| 67 | { | 173 | { |
| @@ -74,39 +180,110 @@ namespace Ti | |||
| 74 | return *this; | 180 | return *this; |
| 75 | } | 181 | } |
| 76 | 182 | ||
| 183 | /** | ||
| 184 | * @method ~shared_ptr | ||
| 185 | * @brief Dtor | ||
| 186 | * @param - | ||
| 187 | * @return - | ||
| 188 | * @globalvars none | ||
| 189 | * @exception none | ||
| 190 | * @pre none | ||
| 191 | * @post none | ||
| 192 | */ | ||
| 77 | ~shared_ptr() | 193 | ~shared_ptr() |
| 78 | { | 194 | { |
| 79 | release(); | 195 | release(); |
| 80 | } | 196 | } |
| 81 | 197 | ||
| 198 | /** | ||
| 199 | * @method operator* | ||
| 200 | * @brief operator * | ||
| 201 | * @param - | ||
| 202 | * @return returns *get() | ||
| 203 | * @globalvars none | ||
| 204 | * @exception none | ||
| 205 | * @pre get() != NULL | ||
| 206 | * @post none | ||
| 207 | */ | ||
| 82 | T& operator*() const | 208 | T& operator*() const |
| 83 | { | 209 | { |
| 84 | return *m_ptr; | 210 | return *m_ptr; |
| 85 | } | 211 | } |
| 86 | 212 | ||
| 213 | /** | ||
| 214 | * @method operator-> | ||
| 215 | * @brief operator -> | ||
| 216 | * @param - | ||
| 217 | * @return returns get() | ||
| 218 | * @globalvars none | ||
| 219 | * @exception none | ||
| 220 | * @pre get() != NULL | ||
| 221 | * @post none | ||
| 222 | */ | ||
| 87 | T* operator->() const | 223 | T* operator->() const |
| 88 | { | 224 | { |
| 89 | return m_ptr; | 225 | return m_ptr; |
| 90 | } | 226 | } |
| 91 | 227 | ||
| 228 | /** | ||
| 229 | * @method swap | ||
| 230 | * @brief Exchanges the contents of *this and other. | ||
| 231 | * @param other instance to swap with | ||
| 232 | * @return - | ||
| 233 | * @globalvars none | ||
| 234 | * @exception none | ||
| 235 | * @pre none | ||
| 236 | * @post none | ||
| 237 | */ | ||
| 92 | void swap(shared_ptr<T>& other) | 238 | void swap(shared_ptr<T>& other) |
| 93 | { | 239 | { |
| 94 | std::swap(m_ptr, other.m_ptr); | 240 | std::swap(m_ptr, other.m_ptr); |
| 95 | std::swap(m_count, other.m_count); | 241 | std::swap(m_count, other.m_count); |
| 96 | } | 242 | } |
| 97 | 243 | ||
| 244 | /** | ||
| 245 | * @method reset | ||
| 246 | * @brief Equivalent to shared_ptr().swap(*this). | ||
| 247 | * @param - | ||
| 248 | * @return - | ||
| 249 | * @globalvars none | ||
| 250 | * @exception none | ||
| 251 | * @pre none | ||
| 252 | * @post none | ||
| 253 | */ | ||
| 98 | inline void reset() | 254 | inline void reset() |
| 99 | { | 255 | { |
| 100 | shared_ptr().swap(*this); | 256 | shared_ptr().swap(*this); |
| 101 | } | 257 | } |
| 102 | 258 | ||
| 103 | private: | 259 | private: |
| 260 | /** | ||
| 261 | * @method add_ref | ||
| 262 | * @brief Increase reference counter | ||
| 263 | * @param - | ||
| 264 | * @return - | ||
| 265 | * @globalvars none | ||
| 266 | * @exception none | ||
| 267 | * @pre m_count != NULL | ||
| 268 | * @post none | ||
| 269 | */ | ||
| 104 | void add_ref() | 270 | void add_ref() |
| 105 | { | 271 | { |
| 106 | if (m_count != NULL) | 272 | if (m_count != NULL) |
| 107 | ++(*m_count); | 273 | ++(*m_count); |
| 108 | } | 274 | } |
| 109 | 275 | ||
| 276 | /** | ||
| 277 | * @method release | ||
| 278 | * @brief Decrease reference counter | ||
| 279 | * if m_count == 0 free all resources | ||
| 280 | * @param - | ||
| 281 | * @return - | ||
| 282 | * @globalvars none | ||
| 283 | * @exception none | ||
| 284 | * @pre m_ptr != NULL && m_count != NULL | ||
| 285 | * @post none | ||
| 286 | */ | ||
| 110 | void release() | 287 | void release() |
| 111 | { | 288 | { |
| 112 | if (m_ptr != NULL) | 289 | if (m_ptr != NULL) |
| @@ -147,48 +324,139 @@ namespace Ti | |||
| 147 | friend shared_ptr<R> shared_dynamic_cast(const shared_ptr<F>& from); | 324 | friend shared_ptr<R> shared_dynamic_cast(const shared_ptr<F>& from); |
| 148 | }; | 325 | }; |
| 149 | 326 | ||
| 327 | /** | ||
| 328 | * @method make_shared | ||
| 329 | * @brief Creates an instance of shared_ptr containing a pointer to the | ||
| 330 | * also created instance of T(args...). | ||
| 331 | * @param args arguments for ctor of T. | ||
| 332 | * @return A shared_ptr instance that stores and owns the address of the | ||
| 333 | * newly constructed object of type T. | ||
| 334 | * @globalvars none | ||
| 335 | * @exception bad_alloc | ||
| 336 | * @pre none | ||
| 337 | * @post get() != 0 && use_count() == 1 | ||
| 338 | */ | ||
| 150 | template <typename T, typename ... Args> | 339 | template <typename T, typename ... Args> |
| 151 | shared_ptr<T> make_shared(Args ... args) | 340 | shared_ptr<T> make_shared(Args ... args) |
| 152 | { | 341 | { |
| 153 | return shared_ptr<T>(new T(args ...)); | 342 | return shared_ptr<T>(new T(args ...)); |
| 154 | } | 343 | } |
| 155 | 344 | ||
| 345 | /** | ||
| 346 | * @method operator== | ||
| 347 | * @brief operator == | ||
| 348 | * @param a object1 to compare | ||
| 349 | * @param b object2 to compare | ||
| 350 | * @return true if both objects contain the same pointer, false otherwise. | ||
| 351 | * @globalvars none | ||
| 352 | * @exception none | ||
| 353 | * @pre none | ||
| 354 | * @post none | ||
| 355 | */ | ||
| 156 | template<typename U1, typename U2> | 356 | template<typename U1, typename U2> |
| 157 | bool operator==(const shared_ptr<U1>& a, const shared_ptr<U2>& b) | 357 | bool operator==(const shared_ptr<U1>& a, const shared_ptr<U2>& b) |
| 158 | { | 358 | { |
| 159 | return a.get() == b.get(); | 359 | return a.get() == b.get(); |
| 160 | } | 360 | } |
| 161 | 361 | ||
| 362 | /** | ||
| 363 | * @method operator== | ||
| 364 | * @brief operator == | ||
| 365 | * @param a shared_ptr to compare | ||
| 366 | * @param b pointer to compare | ||
| 367 | * @return true if a contains pointer b, false otherwise. | ||
| 368 | * @globalvars none | ||
| 369 | * @exception none | ||
| 370 | * @pre none | ||
| 371 | * @post none | ||
| 372 | */ | ||
| 162 | template<typename U1, typename U2> | 373 | template<typename U1, typename U2> |
| 163 | bool operator==(const shared_ptr<U1>& a, const U2* b) | 374 | bool operator==(const shared_ptr<U1>& a, const U2* b) |
| 164 | { | 375 | { |
| 165 | return a.get() == b; | 376 | return a.get() == b; |
| 166 | } | 377 | } |
| 167 | 378 | ||
| 379 | /** | ||
| 380 | * @method operator== | ||
| 381 | * @brief operator == | ||
| 382 | * @param a pointer to compare | ||
| 383 | * @param b shared_ptr to compare | ||
| 384 | * @return true if b contains pointer a, false otherwise. | ||
| 385 | * @globalvars none | ||
| 386 | * @exception none | ||
| 387 | * @pre none | ||
| 388 | * @post none | ||
| 389 | */ | ||
| 168 | template<typename U1, typename U2> | 390 | template<typename U1, typename U2> |
| 169 | bool operator==(const U1* a, const shared_ptr<U2>& b) | 391 | bool operator==(const U1* a, const shared_ptr<U2>& b) |
| 170 | { | 392 | { |
| 171 | return a == b.get(); | 393 | return a == b.get(); |
| 172 | } | 394 | } |
| 173 | 395 | ||
| 396 | /** | ||
| 397 | * @method operator!= | ||
| 398 | * @brief operator != | ||
| 399 | * @param a shared_ptr to compare | ||
| 400 | * @param b pointer to compare | ||
| 401 | * @return true if a contains different pointer than b, false otherwise. | ||
| 402 | * @globalvars none | ||
| 403 | * @exception none | ||
| 404 | * @pre none | ||
| 405 | * @post none | ||
| 406 | */ | ||
| 174 | template<typename U1, typename U2> | 407 | template<typename U1, typename U2> |
| 175 | bool operator!=(const shared_ptr<U1>& a, const U2* b) | 408 | bool operator!=(const shared_ptr<U1>& a, const U2* b) |
| 176 | { | 409 | { |
| 177 | return a.get() != b; | 410 | return a.get() != b; |
| 178 | } | 411 | } |
| 179 | 412 | ||
| 413 | /** | ||
| 414 | * @method operator!= | ||
| 415 | * @brief operator != | ||
| 416 | * @param a pointer to compare | ||
| 417 | * @param b shared_ptr to compare | ||
| 418 | * @return true if b contains different pointer than a, false otherwise. | ||
| 419 | * @globalvars none | ||
| 420 | * @exception none | ||
| 421 | * @pre none | ||
| 422 | * @post none | ||
| 423 | */ | ||
| 180 | template<typename U1, typename U2> | 424 | template<typename U1, typename U2> |
| 181 | bool operator!=(const U1* a, const shared_ptr<U2>& b) | 425 | bool operator!=(const U1* a, const shared_ptr<U2>& b) |
| 182 | { | 426 | { |
| 183 | return a != b.get(); | 427 | return a != b.get(); |
| 184 | } | 428 | } |
| 185 | 429 | ||
| 430 | /** | ||
| 431 | * @method operator!= | ||
| 432 | * @brief operator != | ||
| 433 | * @param a object1 to compare | ||
| 434 | * @param b object2 to compare | ||
| 435 | * @return true if both objects contain different pointers, false otherwise. | ||
| 436 | * @globalvars none | ||
| 437 | * @exception none | ||
| 438 | * @pre none | ||
| 439 | * @post none | ||
| 440 | */ | ||
| 186 | template<typename U1, typename U2> | 441 | template<typename U1, typename U2> |
| 187 | bool operator!=(const shared_ptr<U1>& a, const shared_ptr<U2>& b) | 442 | bool operator!=(const shared_ptr<U1>& a, const shared_ptr<U2>& b) |
| 188 | { | 443 | { |
| 189 | return a.get() != b.get(); | 444 | return a.get() != b.get(); |
| 190 | } | 445 | } |
| 191 | 446 | ||
| 447 | /** | ||
| 448 | * @method shared_dynamic_cast | ||
| 449 | * @brief Converts pointer from F to T by using dynamic_cast. | ||
| 450 | * @param from object to cast | ||
| 451 | * @return When dynamic_cast<T*>(from.get()) returns a nonzero value, | ||
| 452 | * a shared_ptr<T> object that stores a copy of it and shares | ||
| 453 | * ownership with from - otherwise an empty shared_ptr<T> object. | ||
| 454 | * @globalvars none | ||
| 455 | * @exception none | ||
| 456 | * @pre The expression shared_dynamic_cast<T*>(from.get()) shall be well | ||
| 457 | * formed and shall have well defined behavior. | ||
| 458 | * @post w.get() == dynamic_cast<T*>(from.get()), where w is the return value. | ||
| 459 | */ | ||
| 192 | template <typename T, typename F> | 460 | template <typename T, typename F> |
| 193 | shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from) | 461 | shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from) |
| 194 | { | 462 | { |
| @@ -203,6 +471,18 @@ namespace Ti | |||
| 203 | namespace std | 471 | namespace std |
| 204 | { | 472 | { |
| 205 | using namespace Ti; | 473 | using namespace Ti; |
| 474 | |||
| 475 | /** | ||
| 476 | * @method swap | ||
| 477 | * @brief Exchanges the contents of t1 and t2. | ||
| 478 | * @param t1 instance#1 of shared_ptr | ||
| 479 | * @param t2 instance#2 of shared_ptr | ||
| 480 | * @return - | ||
| 481 | * @globalvars none | ||
| 482 | * @exception none | ||
| 483 | * @pre none | ||
| 484 | * @post none | ||
| 485 | */ | ||
| 206 | template <typename T> | 486 | template <typename T> |
| 207 | inline void swap(shared_ptr<T>& t1, shared_ptr<T>& t2) | 487 | inline void swap(shared_ptr<T>& t1, shared_ptr<T>& t2) |
| 208 | { | 488 | { |
