| 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 645 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 473 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 473 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 473 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 473 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 473 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 473 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 473 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 473 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 473 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 473 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 473 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 473 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 473 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 178 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 509 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 1952 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 640 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1911 times.
✗ Branch 56 not taken.
|
31395 | ++nb_refs_; |
| 81 | 40944 | } | |
| 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 | 47790 | --nb_refs_; | |
| 91 | geo_debug_assert(nb_refs_ >= 0); | ||
| 92 |
34/88✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 760 times.
✓ Branch 3 taken 5429 times.
✓ Branch 4 taken 146 times.
✓ Branch 5 taken 9224 times.
✓ Branch 6 taken 12363 times.
✓ Branch 7 taken 144 times.
✓ Branch 8 taken 1013 times.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1528 times.
✓ Branch 12 taken 45 times.
✓ Branch 13 taken 473 times.
✓ Branch 14 taken 298 times.
✓ Branch 15 taken 486 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 473 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 4800 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 473 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 766 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 473 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 251 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✓ Branch 35 taken 251 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 473 times.
✗ Branch 44 not taken.
✓ Branch 45 taken 473 times.
✗ Branch 46 not taken.
✓ Branch 47 taken 473 times.
✗ Branch 48 not taken.
✓ Branch 49 taken 473 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 473 times.
✗ Branch 60 not taken.
✓ Branch 61 taken 473 times.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✓ Branch 67 taken 178 times.
✗ Branch 68 not taken.
✓ Branch 69 taken 509 times.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
✓ Branch 75 taken 1952 times.
✗ Branch 76 not taken.
✓ Branch 77 taken 640 times.
✗ Branch 78 not taken.
✓ Branch 79 taken 1911 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 251 times.
✗ Branch 87 not taken.
|
47790 | if(nb_refs_ == 0) { |
| 93 | 14971 | 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 15952 times.
✓ Branch 1 taken 251 times.
✓ Branch 2 taken 14010 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4473 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 504 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1085 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 49 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4327 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 293 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 251 times.
✗ Branch 21 not taken.
|
41196 | 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 94 times.
✓ Branch 1 taken 1701 times.
✓ Branch 2 taken 6189 times.
✓ Branch 3 taken 5034 times.
✓ Branch 4 taken 9370 times.
✓ Branch 5 taken 2590 times.
✓ Branch 6 taken 12353 times.
✓ Branch 7 taken 303 times.
✓ Branch 8 taken 1047 times.
✓ Branch 9 taken 266 times.
✓ Branch 10 taken 1311 times.
✓ Branch 11 taken 27 times.
✓ Branch 12 taken 11 times.
✓ Branch 13 taken 7 times.
✓ Branch 14 taken 299 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 4327 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 293 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 251 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✓ Branch 34 taken 251 times.
✗ Branch 35 not taken.
|
45724 | 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 144 times.
✗ Branch 2 not taken.
|
20149 | Counted() : |
| 143 |
15/24✓ Branch 1 taken 2915 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 251 times.
✓ Branch 4 taken 716 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 251 times.
✓ Branch 7 taken 1911 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 251 times.
✓ Branch 10 taken 692 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 251 times.
✓ Branch 13 taken 6622 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 502 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 502 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 251 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 251 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 251 times.
✗ Branch 28 not taken.
|
17993 | 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 |