Arcane  4.1.12.0
Developer 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
43template <class T>
45{
46 protected:
47
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
63 const CheckedPointer<T>& operator=(T* new_value)
64 {
65 m_value = new_value;
66 return (*this);
67 }
68
71 : m_value(from.m_value)
72 {}
73
75 template <typename T2>
77 : m_value(from.m_value)
78 {}
79
80 public:
81
84 : m_value(nullptr)
85 {}
86
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
99 inline T* operator->() const
100 {
101#ifdef ARCCORE_CHECK
102 if (!m_value)
104#endif
105 return m_value;
106 }
107
109 inline T& operator*() const
110 {
111#ifdef ARCCORE_CHECK
112 if (!m_value)
114#endif
115 return *m_value;
116 }
117
124 inline T* get() const
125 {
126 return m_value;
127 }
128
129 inline bool isNull() const
130 {
131 return (!m_value);
132 }
133
140 template <typename T2> friend bool
142 {
143 return v1.get() == v2.get();
144 }
145
152 template <typename T2> friend bool
154 {
155 return v1.get() != v2.get();
156 }
157
158 protected:
159
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.