Arcane  4.1.12.0
Developer 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
44template <class T>
45class PtrT
46{
47 protected:
48
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
64 PtrT<T>& operator=(T* new_value)
65 {
66 m_value = new_value;
67 return (*this);
68 }
69
71 PtrT(const PtrT<T>& from)
72 : m_value(from.m_value)
73 {}
74
76 template <typename T2>
77 PtrT(const PtrT<T2>& from)
78 : m_value(from.m_value)
79 {}
80
81 public:
82
84 PtrT() = default;
85
87 explicit PtrT(T* t)
88 : m_value(t)
89 {}
90
91 virtual ~PtrT() = default;
92
93 public:
94 public:
95
97 inline T* operator->() const
98 {
99#ifdef ARCANE_CHECK
100 if (!m_value)
102#endif
103 return m_value;
104 }
105
107 inline T& operator*() const
108 {
109#ifdef ARCANE_CHECK
110 if (!m_value)
112#endif
113 return *m_value;
114 }
115
122 T* get() const { return m_value; }
123
124 bool isNull() const { return !m_value; }
125
126 protected:
127
128 T* m_value = nullptr;
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
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
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.