Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ReferenceCounter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ReferenceCounter.h (C) 2000-2025 */
9/* */
10/* Encapsulation of a pointer with a reference counter. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_REFERENCECOUNTER_H
13#define ARCCORE_BASE_REFERENCECOUNTER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/CheckedPointer.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*!
30 * \brief Encapsulation of a pointer with a reference counter.
31 *
32 * This class holds a pointer of a type that must implement
33 * the following methods:
34 * - addReference() to add a reference
35 * - removeReference() to remove a reference.
36 *
37 * Unlike std::shared_ptr, the reference counter is managed
38 * internally by the type *T*.
39 * This class performs no action based on the reference counter value.
40 * the eventual destruction of the object when the reference counter reaches
41 * zero is handled by the object itself.
42 */
43template <class T>
45: public CheckedPointer<T>
46{
47 public:
48
49 //! Type of the base class
51
53
54 public:
55
56 //! Constructs an instance without a reference
58 : BaseClass(nullptr)
59 {}
60 //! Constructs an instance referencing \a t
61 explicit ReferenceCounter(T* t)
62 : BaseClass(nullptr)
63 {
64 _changeValue(t);
65 }
66 //! Constructs a reference referencing \a from
68 : BaseClass(nullptr)
69 {
70 _changeValue(from.m_value);
71 }
72
73 //! Copy operator
75 {
76 _changeValue(from.m_value);
77 return (*this);
78 }
79
80 //! Assigns the value \a new_value to the instance
82 {
83 _changeValue(new_value);
84 return (*this);
85 }
86
87 //! Destructor. Decrements the reference counter of the pointed object
88 ~ReferenceCounter() { _removeRef(); }
89
90 private:
91
92 //! Removes a reference to the encapsulated object if not null
93 void _removeRef()
94 {
95 if (m_value)
96 ReferenceCounterAccessor<T>::removeReference(m_value);
97 }
98 //! Changes the referenced object to \a new_value
99 void _changeValue(T* new_value)
100 {
101 if (m_value == new_value)
102 return;
103 // Always add first in case the new value
104 // and the old value are from the same instance.
105 if (new_value)
106 ReferenceCounterAccessor<T>::addReference(new_value);
107 _removeRef();
108 m_value = new_value;
109 }
110};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115} // namespace Arcane
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120#endif
T * m_value
Pointer to the referenced object.
CheckedPointer(const CheckedPointer< T > &from)
Constructs a reference referring to from.
ReferenceCounter(const ReferenceCounter< T > &from)
Constructs a reference referencing from.
CheckedPointer< IModuleFactoryInfo > BaseClass
ReferenceCounter(T *t)
Constructs an instance referencing t.
~ReferenceCounter()
Destructor. Decrements the reference counter of the pointed object.
T * m_value
Pointer to the referenced object.
ReferenceCounter()
Constructs an instance without a reference.
ReferenceCounter< T > & operator=(T *new_value)
Assigns the value new_value to the instance.
ReferenceCounter< T > & operator=(const ReferenceCounter< T > &from)
Copy operator.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --