Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AutoRef2.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/* AutoRef2.h (C) 2000-2026 */
9/* */
10/* Encapsulation of a pointer with a reference counter. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_AUTOREF2_H
13#define ARCCORE_BASE_AUTOREF2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
39template <class T>
41{
42 public:
43
44 using ThatClass = AutoRef2<T>;
45
46 public:
47
49 AutoRef2() = default;
51 explicit AutoRef2(T* t)
52 {
53 _changeValue(t);
54 }
55
56 AutoRef2(const ThatClass& from)
57 {
59 }
60
61 AutoRef2(ThatClass&& from) noexcept
62 : m_value(from.m_value)
63 {
64 from.m_value = nullptr;
65 }
66
68 ThatClass& operator=(const ThatClass& from)
69 {
71 return (*this);
72 }
73
74 ThatClass& operator=(ThatClass&& from) noexcept
75 {
76 _removeRef();
77 m_value = from.m_value;
78 from.m_value = nullptr;
79 return (*this);
80 }
81
83 ThatClass& operator=(T* new_value)
84 {
85 _changeValue(new_value);
86 return (*this);
87 }
88
91
93 T* operator->() const
94 {
95 ARCCORE_CHECK_PTR(m_value);
96 return m_value;
97 }
98
100 inline T& operator*() const
101 {
102 ARCCORE_CHECK_PTR(m_value);
103 return *m_value;
104 }
105
107 T* get() const { return m_value; }
108
109 bool isNull() const { return !m_value; }
110 operator bool() const { return m_value; }
111
112 friend bool operator==(const ThatClass& a, const ThatClass& b)
113 {
114 return a.get() == b.get();
115 }
116 friend bool operator!=(const ThatClass& a, const ThatClass& b)
117 {
118 return a.get() != b.get();
119 }
120
121 private:
122
124 void _addRef()
125 {
126 if (m_value)
127 m_value->addRef();
128 }
129
130 void _removeRef() noexcept
131 {
132 if (m_value)
133 m_value->removeRef();
134 }
135
136 void _changeValue(T* new_value)
137 {
138 if (m_value == new_value)
139 return;
140 _removeRef();
141 m_value = new_value;
142 _addRef();
143 }
144
145 private:
146
147 T* m_value = nullptr;
148};
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153} // namespace Arcane
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158#endif
Definitions and globals of Arccore.
ThatClass & operator=(ThatClass &&from) noexcept
Move operator.
Definition AutoRef2.h:74
AutoRef2(const ThatClass &from)
Constructs a reference referencing from.
Definition AutoRef2.h:56
AutoRef2()=default
Constructs an instance without a reference.
void _addRef()
Adds a reference to the encapsulated object if not null.
Definition AutoRef2.h:124
AutoRef2(ThatClass &&from) noexcept
Constructs a reference referencing from.
Definition AutoRef2.h:61
void _changeValue(T *new_value)
Changes the referenced object to new_value.
Definition AutoRef2.h:136
~AutoRef2()
Destructor. Decrements the reference counter of the pointed object.
Definition AutoRef2.h:90
T * m_value
Pointer to the referenced object.
Definition AutoRef2.h:147
ThatClass & operator=(const ThatClass &from)
Copy operator.
Definition AutoRef2.h:68
T * get() const
Returns the object referenced by the instance.
Definition AutoRef2.h:107
T & operator*() const
Returns the object referenced by the instance.
Definition AutoRef2.h:100
void _removeRef() noexcept
Removes a reference to the encapsulated object if not null.
Definition AutoRef2.h:130
ThatClass & operator=(T *new_value)
Assigns the value new_value to the instance.
Definition AutoRef2.h:83
T * operator->() const
Returns the object referenced by the instance.
Definition AutoRef2.h:93
AutoRef2(T *t)
Constructs an instance referencing t.
Definition AutoRef2.h:51
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --