blob: 32a28a6c4b9ae0f979fd5a279f1e6ea7d2b743b1 [file] [log] [blame]
Brian Silverman355f11d2018-08-04 23:57:00 -07001////
2Copyright 2017 Peter Dimov
3Copyright 2013 Andrey Semashev
4
5Distributed under the Boost Software License, Version 1.0.
6
7See accompanying file LICENSE_1_0.txt or copy at
8http://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
19The `intrusive_ref_counter` class template implements a reference counter for
20a derived user's class that is intended to be used with `intrusive_ptr`. The
21base class has associated `intrusive_ptr_add_ref` and `intrusive_ptr_release`
22functions which modify the reference counter as needed and destroy the user's
23object when the counter drops to zero.
24
25The class template is parameterized on `Derived` and `CounterPolicy`
26parameters. 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
28correctly when there are no references to it left.
29
30The second parameter is a policy that defines the nature of the reference
31counter. The library provides two such policies: `thread_unsafe_counter` and
32`thread_safe_counter`. The former instructs the `intrusive_ref_counter` base
33class to use a counter only suitable for a single-threaded use. Pointers to a
34single object that uses this kind of reference counter must not be used in
35different threads. The latter policy makes the reference counter thread-safe,
36unless the target platform doesn't support threading. Since in modern systems
37support 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```
46namespace 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```
79intrusive_ref_counter() noexcept;
80```
81::
82```
83intrusive_ref_counter(const intrusive_ref_counter&) noexcept;
84```
85::
86Postconditions::: `use_count() == 0`.
87
88NOTE: The pointer to the constructed object is expected to be passed to
89`intrusive_ptr` constructor, assignment operator or `reset` method, which
90would increment the reference counter.
91
92### Destructor
93
94```
95~intrusive_ref_counter();
96```
97::
98Effects::: Destroys the counter object.
99
100NOTE: The destructor is protected so that the object can only be destroyed
101through the `Derived` class.
102
103### Assignment
104
105```
106intrusive_ref_counter& operator=(const intrusive_ref_counter& v) noexcept;
107```
108::
109Effects::: Does nothing, reference counter is not modified.
110
111### use_count
112
113```
114unsigned int use_count() const noexcept;
115```
116::
117Returns::: The current value of the reference counter.
118
119NOTE: The returned value may not be actual in multi-threaded applications.
120
121## Free Functions
122
123### intrusive_ptr_add_ref
124
125```
126template<class Derived, class CounterPolicy>
127 void intrusive_ptr_add_ref(
128 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;
129```
130::
131Effects::: Increments the reference counter.
132
133### intrusive_ptr_release
134
135```
136template<class Derived, class CounterPolicy>
137 void intrusive_ptr_release(
138 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;
139```
140::
141Effects::: Decrements the reference counter. If the reference counter reaches
1420, calls `delete static_cast<const Derived*>(p)`.