Arcane  4.1.12.0
User 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
29/*!
30 * \brief Encapsulation of a pointer with a reference counter.
31 *
32 * This class encapsulates a pointer of a type that implements the methods
33 * of the abstract class ISharedReference (the template parameter does not
34 * need to derive from this class) and increments (_addRef()) or decrements
35 * (_removeRef()) the reference counter of the pointed element during
36 * successive assignments. This class does not perform any action based
37 * on the value of the reference counter; the eventual destruction of the object
38 * when the reference counter reaches zero is handled by the object itself.
39 */
40template <class T>
42: public PtrT<T>
43{
44 public:
45
46 //! Base class type
48
50
51 public:
52
53 //! Constructs an instance without a reference
54 AutoRefT() = default;
55
56 //! Constructs an instance referencing \a t
57 explicit AutoRefT(T* t)
58 : BaseClass()
59 {
60 _changeValue(t);
61 }
62
63 //! Constructs a reference referencing \a from
64 AutoRefT(const AutoRefT<T>& from)
65 : BaseClass()
66 {
67 _changeValue(from.m_value);
68 }
69
70 //! Copy operator
72 {
73 _changeValue(from.m_value);
74 return (*this);
75 }
76
77 //! Assigns the value \a new_value to the instance
78 AutoRefT<T>& operator=(T* new_value)
79 {
80 _changeValue(new_value);
81 return (*this);
82 }
83
84 //! Destructor. Decrements the reference counter of the pointed object
85 ~AutoRefT() { _removeRef(); }
86
87 private:
88
89 //! Adds a reference to the encapsulated object if not null
90 void _addRef()
91 {
92 if (m_value)
93 m_value->addRef();
94 }
95
96 //! Removes a reference to the encapsulated object if not null
97 void _removeRef()
98 {
99 if (m_value)
100 m_value->removeRef();
101 }
102
103 //! Changes the referenced object to \a new_value
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
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
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
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 --