Brian Silverman | 355f11d | 2018-08-04 23:57:00 -0700 | [diff] [blame^] | 1 | //// |
| 2 | Copyright 2017 Peter Dimov |
| 3 | Copyright 2013 Andrey Semashev |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. |
| 6 | |
| 7 | See accompanying file LICENSE_1_0.txt or copy at |
| 8 | http://www.boost.org/LICENSE_1_0.txt |
| 9 | //// |
| 10 | |
| 11 | [#intrusive_ref_counter] |
| 12 | # intrusive_ref_counter |
| 13 | :toc: |
| 14 | :toc-title: |
| 15 | :idprefix: intrusive_ref_counter_ |
| 16 | |
| 17 | ## Description |
| 18 | |
| 19 | The `intrusive_ref_counter` class template implements a reference counter for |
| 20 | a derived user's class that is intended to be used with `intrusive_ptr`. The |
| 21 | base class has associated `intrusive_ptr_add_ref` and `intrusive_ptr_release` |
| 22 | functions which modify the reference counter as needed and destroy the user's |
| 23 | object when the counter drops to zero. |
| 24 | |
| 25 | The class template is parameterized on `Derived` and `CounterPolicy` |
| 26 | parameters. The first parameter is the user's class that derives from |
| 27 | `intrusive_ref_counter`. This type is needed in order to destroy the object |
| 28 | correctly when there are no references to it left. |
| 29 | |
| 30 | The second parameter is a policy that defines the nature of the reference |
| 31 | counter. The library provides two such policies: `thread_unsafe_counter` and |
| 32 | `thread_safe_counter`. The former instructs the `intrusive_ref_counter` base |
| 33 | class to use a counter only suitable for a single-threaded use. Pointers to a |
| 34 | single object that uses this kind of reference counter must not be used in |
| 35 | different threads. The latter policy makes the reference counter thread-safe, |
| 36 | unless the target platform doesn't support threading. Since in modern systems |
| 37 | support for threading is common, the default counter policy is |
| 38 | `thread_safe_counter`. |
| 39 | |
| 40 | ## Synopsis |
| 41 | |
| 42 | `intrusive_ref_counter` is defined in |
| 43 | `<boost/smart_ptr/intrusive_ref_counter.hpp>`. |
| 44 | |
| 45 | ``` |
| 46 | namespace boost { |
| 47 | struct thread_unsafe_counter; |
| 48 | struct thread_safe_counter; |
| 49 | |
| 50 | template<class Derived, class CounterPolicy = thread_safe_counter> |
| 51 | class intrusive_ref_counter { |
| 52 | public: |
| 53 | intrusive_ref_counter() noexcept; |
| 54 | intrusive_ref_counter(const intrusive_ref_counter& v) noexcept; |
| 55 | |
| 56 | intrusive_ref_counter& operator=(const intrusive_ref_counter& v) noexcept; |
| 57 | |
| 58 | unsigned int use_count() const noexcept; |
| 59 | |
| 60 | protected: |
| 61 | ~intrusive_ref_counter() = default; |
| 62 | }; |
| 63 | |
| 64 | template<class Derived, class CounterPolicy> |
| 65 | void intrusive_ptr_add_ref( |
| 66 | const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept; |
| 67 | |
| 68 | template<class Derived, class CounterPolicy> |
| 69 | void intrusive_ptr_release( |
| 70 | const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept; |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | ## Members |
| 75 | |
| 76 | ### Constructors |
| 77 | |
| 78 | ``` |
| 79 | intrusive_ref_counter() noexcept; |
| 80 | ``` |
| 81 | :: |
| 82 | ``` |
| 83 | intrusive_ref_counter(const intrusive_ref_counter&) noexcept; |
| 84 | ``` |
| 85 | :: |
| 86 | Postconditions::: `use_count() == 0`. |
| 87 | |
| 88 | NOTE: The pointer to the constructed object is expected to be passed to |
| 89 | `intrusive_ptr` constructor, assignment operator or `reset` method, which |
| 90 | would increment the reference counter. |
| 91 | |
| 92 | ### Destructor |
| 93 | |
| 94 | ``` |
| 95 | ~intrusive_ref_counter(); |
| 96 | ``` |
| 97 | :: |
| 98 | Effects::: Destroys the counter object. |
| 99 | |
| 100 | NOTE: The destructor is protected so that the object can only be destroyed |
| 101 | through the `Derived` class. |
| 102 | |
| 103 | ### Assignment |
| 104 | |
| 105 | ``` |
| 106 | intrusive_ref_counter& operator=(const intrusive_ref_counter& v) noexcept; |
| 107 | ``` |
| 108 | :: |
| 109 | Effects::: Does nothing, reference counter is not modified. |
| 110 | |
| 111 | ### use_count |
| 112 | |
| 113 | ``` |
| 114 | unsigned int use_count() const noexcept; |
| 115 | ``` |
| 116 | :: |
| 117 | Returns::: The current value of the reference counter. |
| 118 | |
| 119 | NOTE: The returned value may not be actual in multi-threaded applications. |
| 120 | |
| 121 | ## Free Functions |
| 122 | |
| 123 | ### intrusive_ptr_add_ref |
| 124 | |
| 125 | ``` |
| 126 | template<class Derived, class CounterPolicy> |
| 127 | void intrusive_ptr_add_ref( |
| 128 | const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept; |
| 129 | ``` |
| 130 | :: |
| 131 | Effects::: Increments the reference counter. |
| 132 | |
| 133 | ### intrusive_ptr_release |
| 134 | |
| 135 | ``` |
| 136 | template<class Derived, class CounterPolicy> |
| 137 | void intrusive_ptr_release( |
| 138 | const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept; |
| 139 | ``` |
| 140 | :: |
| 141 | Effects::: Decrements the reference counter. If the reference counter reaches |
| 142 | 0, calls `delete static_cast<const Derived*>(p)`. |