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