Arcane  4.1.12.0
Developer 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
43template <class T>
45: public CheckedPointer<T>
46{
47 public:
48
51
53
54 public:
55
58 : BaseClass(nullptr)
59 {}
60
61 explicit ReferenceCounter(T* t)
62 : BaseClass(nullptr)
63 {
64 _changeValue(t);
65 }
66
68 : BaseClass(nullptr)
69 {
71 }
72
75 {
77 return (*this);
78 }
79
82 {
83 _changeValue(new_value);
84 return (*this);
85 }
86
89
90 private:
91
94 {
95 if (m_value)
96 ReferenceCounterAccessor<T>::removeReference(m_value);
97 }
98
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.
void _removeRef()
Removes a reference to the encapsulated object if not null.
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.
void _changeValue(T *new_value)
Changes the referenced object to new_value.
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 --