GCC Code Coverage Report


Directory: ./
File: lib/geogram/basic/counted.h
Date: 2026-09-07 02:25:23
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 0 0 -%
Branches: 101 215 47.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef GEOGRAM_BASIC_COUNTED
41 #define GEOGRAM_BASIC_COUNTED
42
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/smart_pointer.h>
45 #include <geogram/basic/assert.h>
46
47 /**
48 * \file geogram/basic/counted.h
49 * \brief Base class of reference-counted objects,
50 * to be used with smart pointers
51 */
52
53 namespace GEO {
54
55 /**
56 * \brief Base class for reference-counted objects
57 * \details Reference counted objects implement shared ownership with a
58 * simple mechanism: objects willing to share ownership on a Counted object
59 * must call ref() on this object and call unref() when they no longer
60 * need it. The object is destroyed when no more objects hold a reference
61 * on it (when the last holder calls unref()).
62 *
63 * Objects can benefit reference counted sharing simply by deriving from
64 * Counted and implementing the virtual destructor.
65 *
66 * Reference acquisition and release can be done manually by explicitly
67 * calling ref() or unref() on the reference counted objects, or can be
68 * done automatically by using SmartPointer<T>.
69 * \see SmartPointer
70 */
71 class GEOGRAM_API Counted {
72 public:
73 /**
74 * \brief Increments the reference count
75 * \details This function must be called to share ownership on this
76 * object. Calling ref() will prevent this object from being deleted
77 * when someone else releases ownership.
78 */
79 void ref() const {
80
19/38
✓ Branch 1 taken 639 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 483 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 483 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 483 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 483 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 483 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 483 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 483 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 483 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 483 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 483 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 483 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 483 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 483 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 177 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 506 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 2008 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 652 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1947 times.
✗ Branch 56 not taken.
31367 ++nb_refs_;
81 41087 }
82
83 /**
84 * \brief Decrements the reference count
85 * \details This function must be called to release ownership on this
86 * object when it's no longer needed. Whwen the reference count
87 * reaches the value of 0 (zero), the object is simply deleted.
88 */
89 void unref() const {
90 48203 --nb_refs_;
91 geo_debug_assert(nb_refs_ >= 0);
92
34/88
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 756 times.
✓ Branch 3 taken 5345 times.
✓ Branch 4 taken 129 times.
✓ Branch 5 taken 9397 times.
✓ Branch 6 taken 12588 times.
✓ Branch 7 taken 128 times.
✓ Branch 8 taken 1005 times.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1534 times.
✓ Branch 12 taken 45 times.
✓ Branch 13 taken 483 times.
✓ Branch 14 taken 296 times.
✓ Branch 15 taken 502 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 483 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 4724 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 483 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 774 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 483 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 249 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✓ Branch 35 taken 249 times.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 483 times.
✗ Branch 44 not taken.
✓ Branch 45 taken 483 times.
✗ Branch 46 not taken.
✓ Branch 47 taken 483 times.
✗ Branch 48 not taken.
✓ Branch 49 taken 483 times.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✓ Branch 59 taken 483 times.
✗ Branch 60 not taken.
✓ Branch 61 taken 483 times.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✓ Branch 67 taken 177 times.
✗ Branch 68 not taken.
✓ Branch 69 taken 506 times.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
✓ Branch 75 taken 2008 times.
✗ Branch 76 not taken.
✓ Branch 77 taken 652 times.
✗ Branch 78 not taken.
✓ Branch 79 taken 1947 times.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✗ Branch 84 not taken.
✗ Branch 85 not taken.
✓ Branch 86 taken 249 times.
✗ Branch 87 not taken.
48203 if(nb_refs_ == 0) {
93 15161 delete this;
94 }
95 }
96
97 /**
98 * \brief Check if the object is shared
99 * \details An object is considered as shared if at least 2 client
100 * objects have called ref() on this object.
101 * \return \c true if the object is shared, \c false otherwise
102 */
103 bool is_shared() const {
104 return nb_refs_ > 1;
105 }
106
107 /**
108 * \brief Gets the number of references that point to this object.
109 * \return the number of references.
110 */
111 int nb_refs() const {
112 return nb_refs_;
113 }
114
115 /**
116 * \brief Increments the reference count
117 * \details This calls ref() on object \p counted if it is not null.
118 * \param[in] counted reference object to reference.
119 */
120 static void ref(const Counted* counted) {
121
11/22
✓ Branch 0 taken 16026 times.
✓ Branch 1 taken 249 times.
✓ Branch 2 taken 14117 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4527 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 500 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1081 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 55 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4241 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 291 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 249 times.
✗ Branch 21 not taken.
41341 if(counted != nullptr) {
122 counted->ref();
123 }
124 }
125
126 /**
127 * \brief Decrements the reference count
128 * \details This calls unref() on object \p counted if it is not null.
129 * \param[in] counted reference object to dereference.
130 */
131 static void unref(const Counted* counted) {
132
19/36
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1684 times.
✓ Branch 2 taken 6101 times.
✓ Branch 3 taken 4993 times.
✓ Branch 4 taken 9526 times.
✓ Branch 5 taken 2558 times.
✓ Branch 6 taken 12588 times.
✓ Branch 7 taken 301 times.
✓ Branch 8 taken 1039 times.
✓ Branch 9 taken 264 times.
✓ Branch 10 taken 1305 times.
✓ Branch 11 taken 27 times.
✓ Branch 12 taken 11 times.
✓ Branch 13 taken 7 times.
✓ Branch 14 taken 303 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 4241 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 291 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✓ Branch 30 taken 249 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✓ Branch 34 taken 249 times.
✗ Branch 35 not taken.
45829 if(counted != nullptr) {
133 counted->unref();
134 }
135 }
136
137 protected:
138 /**
139 * \brief Creates a reference counted object
140 * \details This initializes the reference count to 0 (zero).
141 */
142
1/2
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
20293 Counted() :
143
17/29
✗ Branch 0 not taken.
✓ Branch 1 taken 2959 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 249 times.
✓ Branch 4 taken 727 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 249 times.
✓ Branch 7 taken 1947 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 249 times.
✓ Branch 10 taken 688 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 249 times.
✓ Branch 13 taken 6762 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 249 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 249 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 249 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 249 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 249 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 249 times.
✗ Branch 31 not taken.
✓ Branch 33 taken 249 times.
✗ Branch 34 not taken.
18178 nb_refs_(0) {
144 }
145
146 /**
147 * \brief Destroys a reference counted object
148 * \details The destructor is never called directly but indirectly
149 * through unref(). If the reference counter is not null when the
150 * destructor is called the program dies with an assertion failure.
151 */
152 virtual ~Counted();
153
154 private:
155 /** Forbid copy constructor */
156 Counted(const Counted&);
157 /** Forbid assignment operator */
158 Counted& operator= (const Counted&);
159
160 mutable int nb_refs_;
161 };
162 }
163
164 #endif
165