Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Ptr.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/* Ptr.h (C) 2000-2024 */
9/* */
10/* Classes encapsulating various pointers. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_PTR_H
13#define ARCANE_UTILS_PTR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28/*!
29 * \ingroup Core
30 * \brief Encapsulation of a pointer.
31 *
32 * This class does nothing special other than encapsulating a
33 * pointer of any type. It serves as a base class for other
34 * classes that provide more advanced features like AutoRefT.
35 *
36 * To avoid accidental copies, the copy constructor and
37 * copy operators are protected.
38 *
39 * In debug mode, checks that we do not access a null pointer.
40 *
41 * The template parameter does not need to be defined. This class can therefore
42 * be instantiated for an opaque type.
43 */
44template <class T>
45class PtrT
46{
47 protected:
48
49 //! Copy operator
51 {
52 m_value = from.m_value;
53 return (*this);
54 }
55
56 template <typename T2>
57 PtrT<T>& operator=(const PtrT<T2>& from)
58 {
59 m_value = from.get();
60 return (*this);
61 }
62
63 //! Assigns the value \a new_value to the instance
64 PtrT<T>& operator=(T* new_value)
65 {
66 m_value = new_value;
67 return (*this);
68 }
69
70 //! Constructs a reference referring to \a from
71 PtrT(const PtrT<T>& from)
72 : m_value(from.m_value)
73 {}
74
75 //! Constructs a reference referring to \a from
76 template <typename T2>
77 PtrT(const PtrT<T2>& from)
78 : m_value(from.m_value)
79 {}
80
81 public:
82
83 //! Constructs an instance without a reference
84 PtrT() = default;
85
86 //! Constructs an instance referring to \a t
87 explicit PtrT(T* t)
88 : m_value(t)
89 {}
90
91 virtual ~PtrT() = default;
92
93 public:
94 public:
95
96 //! Returns the object referenced by the instance
97 inline T* operator->() const
98 {
99#ifdef ARCANE_CHECK
100 if (!m_value)
102#endif
103 return m_value;
104 }
105
106 //! Returns the object referenced by the instance
107 inline T& operator*() const
108 {
109#ifdef ARCANE_CHECK
110 if (!m_value)
112#endif
113 return *m_value;
114 }
115
116 /*!
117 * \brief Returns the object referenced by the instance
118 *
119 * \warning In general, caution must be exercised when using this
120 * function and the returned pointer should not be retained.
121 */
122 T* get() const { return m_value; }
123
124 bool isNull() const { return !m_value; }
125
126 protected:
127
128 T* m_value = nullptr; //!< Pointer to the referenced object
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133/*!
134 * \brief Compares the objects referenced by \a v1 and \a v2
135 *
136 * The comparison is done pointer by pointer.
137 * \retval true if they are equal
138 * \retval false otherwise
139 */
140template <typename T1, typename T2> inline bool
141operator==(const PtrT<T1>& v1, const PtrT<T2>& v2)
142{
143 return v1.get() == v2.get();
144}
145
146/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
148
149/*!
150 * \brief Compares the objects referenced by \a v1 and \a v2
151 * The comparison is done pointer by pointer.
152 * \retval false if they are equal
153 * \retval true otherwise
154 */
155template <typename T1, typename T2> inline bool
156operator!=(const PtrT<T1>& v1, const PtrT<T2>& v2)
157{
158 return v1.get() != v2.get();
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164} // namespace Arcane
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169#endif
Arcane configuration file.
Encapsulation of a pointer.
Definition Ptr.h:46
T * m_value
Pointer to the referenced object.
Definition Ptr.h:128
T * operator->() const
Returns the object referenced by the instance.
Definition Ptr.h:97
PtrT< T > & operator=(const PtrT< T > &from)
Copy operator.
Definition Ptr.h:50
PtrT(const PtrT< T > &from)
Constructs a reference referring to from.
Definition Ptr.h:71
T * get() const
Returns the object referenced by the instance.
Definition Ptr.h:122
PtrT()=default
Constructs an instance without a reference.
PtrT(const PtrT< T2 > &from)
Constructs a reference referring to from.
Definition Ptr.h:77
PtrT< T > & operator=(T *new_value)
Assigns the value new_value to the instance.
Definition Ptr.h:64
T & operator*() const
Returns the object referenced by the instance.
Definition Ptr.h:107
PtrT(T *t)
Constructs an instance referring to t.
Definition Ptr.h:87
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
void arcaneNullPointerError()
Signals the use of a null pointer.