| 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 | 55810 | void ref() const { | |
| 80 | 55810 | ++nb_refs_; | |
| 81 | 55810 | } | |
| 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 | 55810 | void unref() const { | |
| 90 | 55810 | --nb_refs_; | |
| 91 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 55810 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
55810 | geo_debug_assert(nb_refs_ >= 0); |
| 92 |
2/2✓ Branch 0 taken 20109 times.
✓ Branch 1 taken 35701 times.
|
55810 | if(nb_refs_ == 0) { |
| 93 |
1/2✓ Branch 0 taken 20109 times.
✗ Branch 1 not taken.
|
20109 | delete this; |
| 94 | } | ||
| 95 | 55810 | } | |
| 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 | 56062 | static void ref(const Counted* counted) { | |
| 121 |
2/2✓ Branch 0 taken 55810 times.
✓ Branch 1 taken 252 times.
|
56062 | if(counted != nullptr) { |
| 122 | 55810 | counted->ref(); | |
| 123 | } | ||
| 124 | 56062 | } | |
| 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 | 65738 | static void unref(const Counted* counted) { | |
| 132 |
2/2✓ Branch 0 taken 55810 times.
✓ Branch 1 taken 9928 times.
|
65738 | if(counted != nullptr) { |
| 133 | 55810 | counted->unref(); | |
| 134 | } | ||
| 135 | 65738 | } | |
| 136 | |||
| 137 | protected: | ||
| 138 | /** | ||
| 139 | * \brief Creates a reference counted object | ||
| 140 | * \details This initializes the reference count to 0 (zero). | ||
| 141 | */ | ||
| 142 | 20149 | Counted() : | |
| 143 | 20149 | nb_refs_(0) { | |
| 144 | 20149 | } | |
| 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 |