1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
///******************************************************************************
//The MIT License(MIT)
//
//Embedded Template Library.
//https://github.com/ETLCPP/etl
//https://www.etlcpp.com
//
//Copyright(c) 2021 John Wellbelove
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//******************************************************************************/
#ifndef ETL_REFERENCE_COUNTED_OBJECT_INCLUDED
#define ETL_REFERENCE_COUNTED_OBJECT_INCLUDED
#include "platform.h"
#include "atomic.h"
#include "exception.h"
#include "error_handler.h"
#include "utility.h"
#include <stdint.h>
namespace etl
{
//***************************************************************************
/// Exceptions for reference counting
///\ingroup reference_counting
//***************************************************************************
class reference_counting_exception : public etl::exception
{
public:
reference_counting_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
/// Reference counter overrun exception
///\ingroup reference_counting
//***************************************************************************
class reference_count_overrun : public etl::reference_counting_exception
{
public:
reference_count_overrun(string_type file_name_, numeric_type line_number_)
: etl::reference_counting_exception(ETL_ERROR_TEXT("reference_counting:overrun", ETL_REFERENCE_COUNTED_OBJECT_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// The base of all reference counters.
//***************************************************************************
class ireference_counter
{
public:
virtual ~ireference_counter() {};
virtual void set_reference_count(int32_t value) = 0;
virtual void increment_reference_count() = 0;
ETL_NODISCARD virtual int32_t decrement_reference_count() = 0;
ETL_NODISCARD virtual int32_t get_reference_count() const = 0;
};
//***************************************************************************
/// A specific type of reference counter.
//***************************************************************************
template <typename TCounter>
class reference_counter : public ireference_counter
{
public:
//***************************************************************************
/// Constructor.
//***************************************************************************
reference_counter()
: reference_count(0)
{
}
//***************************************************************************
/// Set the reference count.
//***************************************************************************
virtual void set_reference_count(int32_t value) ETL_OVERRIDE
{
reference_count = value;
}
//***************************************************************************
/// Increment the reference count.
//***************************************************************************
virtual void increment_reference_count() ETL_OVERRIDE
{
++reference_count;
}
//***************************************************************************
/// Decrement the reference count.
//***************************************************************************
ETL_NODISCARD virtual int32_t decrement_reference_count() ETL_OVERRIDE
{
ETL_ASSERT(reference_count > 0, ETL_ERROR(reference_count_overrun));
return int32_t(--reference_count);
}
//***************************************************************************
/// Get the current reference count.
//***************************************************************************
ETL_NODISCARD virtual int32_t get_reference_count() const ETL_OVERRIDE
{
return int32_t(reference_count);
}
private:
TCounter reference_count; // The reference count object.
};
//***************************************************************************
/// A specialisation for a counter type of void.
//***************************************************************************
template <>
class reference_counter<void> : public ireference_counter
{
public:
//***************************************************************************
/// Constructor.
//***************************************************************************
reference_counter()
{
// Do nothing.
}
//***************************************************************************
/// Set the reference count.
//***************************************************************************
virtual void set_reference_count(int32_t /*value*/) ETL_OVERRIDE
{
// Do nothing.
}
//***************************************************************************
/// Increment the reference count.
//***************************************************************************
virtual void increment_reference_count() ETL_OVERRIDE
{
// Do nothing.
}
//***************************************************************************
/// Decrement the reference count.
//***************************************************************************
ETL_NODISCARD virtual int32_t decrement_reference_count() ETL_OVERRIDE
{
return 1;
}
//***************************************************************************
/// Get the current reference count.
//***************************************************************************
ETL_NODISCARD virtual int32_t get_reference_count() const ETL_OVERRIDE
{
return 1;
}
};
//***************************************************************************
/// Base for all reference counted objects.
//***************************************************************************
class ireference_counted_object
{
public:
virtual ~ireference_counted_object() {}
ETL_NODISCARD virtual ireference_counter& get_reference_counter() = 0;
ETL_NODISCARD virtual const ireference_counter& get_reference_counter() const = 0;
};
//***************************************************************************
/// Class for creating reference counted objects.
/// \tparam TObject The type to be reference counted.
/// \tparam TCounter The type to use as the counter.
//***************************************************************************
template <typename TObject, typename TCounter>
class reference_counted_object : public etl::ireference_counted_object
{
public:
typedef TObject value_type;
typedef TCounter counter_type;
//***************************************************************************
/// Constructor.
//***************************************************************************
reference_counted_object()
{
}
//***************************************************************************
/// Constructor.
//***************************************************************************
reference_counted_object(const TObject& object_)
: object(object_)
{
}
#if ETL_USING_CPP11
//***************************************************************************
/// Constructor.
//***************************************************************************
template <typename... TArgs>
reference_counted_object(TArgs&&... args)
: object(etl::forward<TArgs>(args)...)
{
}
#endif
//***************************************************************************
/// Get a reference to the counted object.
//***************************************************************************
ETL_NODISCARD value_type& get_object()
{
return object;
}
//***************************************************************************
/// Get a const reference to the counted object.
//***************************************************************************
ETL_NODISCARD const value_type& get_object() const
{
return object;
}
//***************************************************************************
/// Get a reference to the reference counter.
//***************************************************************************
ETL_NODISCARD virtual ireference_counter& get_reference_counter() ETL_OVERRIDE
{
return reference_counter;
}
//***************************************************************************
/// Get a const reference to the reference counter.
//***************************************************************************
ETL_NODISCARD virtual const ireference_counter& get_reference_counter() const ETL_OVERRIDE
{
return reference_counter;
}
private:
// This class must not be copy constructed or assigned.
reference_counted_object(const reference_counted_object&) ETL_DELETE;
reference_counted_object& operator =(const reference_counted_object&) ETL_DELETE;
TObject object; ///< The object being reference counted.
etl::reference_counter<TCounter> reference_counter; ///< The reference counter.
};
#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
//***************************************************************************
/// Class for creating reference counted objects using an atomic counter.
/// \tparam TObject The type to be reference counted.
//***************************************************************************
template <typename TObject>
using atomic_counted_object = etl::reference_counted_object<TObject, etl::atomic_int32_t>;
#endif
}
#endif
|