Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AutoRef.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/* AutoRef.h (C) 2000-2025 */
9/* */
10/* Encapsulation of a pointer with a reference counter. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_AUTOREF_H
13#define ARCANE_UTILS_AUTOREF_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Ptr.h"
18#include "arccore/base/AutoRef2.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
40template <class T>
42: public PtrT<T>
43{
44 public:
45
48
50
51 public:
52
54 AutoRefT() = default;
55
57 explicit AutoRefT(T* t)
58 : BaseClass()
59 {
60 _changeValue(t);
61 }
62
64 AutoRefT(const AutoRefT<T>& from)
65 : BaseClass()
66 {
68 }
69
72 {
74 return (*this);
75 }
76
78 AutoRefT<T>& operator=(T* new_value)
79 {
80 _changeValue(new_value);
81 return (*this);
82 }
83
86
87 private:
88
90 void _addRef()
91 {
92 if (m_value)
93 m_value->addRef();
94 }
95
98 {
99 if (m_value)
100 m_value->removeRef();
101 }
102
104 void _changeValue(T* new_value)
105 {
106 if (m_value == new_value)
107 return;
108 _removeRef();
109 m_value = new_value;
110 _addRef();
111 }
112};
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117} // namespace Arcane
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
121
122#endif
T * m_value
Pointer to the referenced object.
Definition Ptr.h:128
AutoRefT()=default
Constructs an instance without a reference.
PtrT< T > BaseClass
Base class type.
Definition AutoRef.h:47
~AutoRefT()
Destructor. Decrements the reference counter of the pointed object.
Definition AutoRef.h:85
void _addRef()
Adds a reference to the encapsulated object if not null.
Definition AutoRef.h:90
AutoRefT(T *t)
Constructs an instance referencing t.
Definition AutoRef.h:57
AutoRefT(const AutoRefT< T > &from)
Constructs a reference referencing from.
Definition AutoRef.h:64
void _changeValue(T *new_value)
Changes the referenced object to new_value.
Definition AutoRef.h:104
AutoRefT< T > & operator=(T *new_value)
Assigns the value new_value to the instance.
Definition AutoRef.h:78
AutoRefT< T > & operator=(const AutoRefT< T > &from)
Copy operator.
Definition AutoRef.h:71
void _removeRef()
Removes a reference to the encapsulated object if not null.
Definition AutoRef.h:97
T * m_value
Pointer to the referenced object.
Definition Ptr.h:128
PtrT(const PtrT< T > &from)
Constructs a reference referring to from.
Definition Ptr.h:71
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --