TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
TRUST_Ref.h
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef TRUST_Ref_included
17#define TRUST_Ref_included
18
19#include <type_traits>
20#include <assert.h>
21
22class Objet_U;
23class Nom;
24
25/*! @brief Template reference class for any class _CLASSE_ deriving from Objet_U.
26 *
27 * TRUST_Ref<_CLASSE_> is a small template class containing a pointer to an instance of _CLASSE_.
28 *
29 * Definition of the REF:
30 *
31 * #include <TRUST_Ref.h>
32 * #include <_CLASSE_.h>
33 *
34 * Creating an object of type OBS_PTR(_CLASSE_):
35 *
36 * OBS_PTR(_CLASSE_) la_ref_;
37 *
38 * or equivalently
39 *
40 * TRUST_Ref<_CLASSE_>
41 *
42 */
43
44// MACRO to replace OBS_PTR(_TYPE_) by TRUST_Ref<_TYPE_*> & keep previous syntax for some developers
45#define REF(_TYPE_) \
46 static_assert(false, "The old REF MACRO is now deprecated. Please use OBS_PTR instead.")
47
48#define OBS_PTR(_TYPE_) TRUST_Ref<_TYPE_*>
49
50template<typename _CLASSE_>
51class TRUST_Ref final
52{
53 /*
54 * Elie & Adrien :
55 * Define the underlying (non-pointer) type
56 * For example if _CLASSE_ = 'Probleme_base*', value_type is 'Probleme_base' : https://en.cppreference.com/w/cpp/types/remove_pointer
57 */
58 using value_type = typename std::remove_pointer<_CLASSE_>::type;
59
60private:
61 value_type * p_ = nullptr;
62
63public:
64 static constexpr bool HAS_POINTER = true;
65
66 ~TRUST_Ref() = default;
67 TRUST_Ref() = default;
68 TRUST_Ref(const value_type& t) : p_((value_type*)&t) { }
69 TRUST_Ref(const TRUST_Ref& t) : p_(t.p_) { }
70
71 const TRUST_Ref& operator=(const value_type& t)
72 {
73 p_ = const_cast<value_type *>(&t);
74 return *this;
75 }
76
77 const TRUST_Ref& operator=(const TRUST_Ref& t)
78 {
79 p_ = t.p_;
80 return *this;
81 }
82
83 // not deleted because of issues in TRUST_List iterators, e.g. LIST(REF ...)
84 operator const value_type& () const { return valeur(); }
85 operator value_type& () { return valeur(); }
86
87 inline const value_type& valeur() const { assert(p_ != nullptr); return *p_; }
88 inline value_type& valeur() { assert(p_ != nullptr); return *p_; }
89 inline const value_type* operator ->() const { assert(p_ != nullptr); return p_; }
90 inline value_type* operator ->() { assert(p_ != nullptr); return p_; }
91
92 explicit operator bool() const noexcept { return p_ != nullptr; }
93
94 const Nom& le_nom() const = delete;
95 void reset() { p_ = nullptr; }
96};
97
98// The result of == is true if r1 and r2 point to the same object (same key), or if both references are null.
99template<typename _CLASSE_>
100inline int operator ==(const TRUST_Ref<_CLASSE_>& r1, const TRUST_Ref<_CLASSE_>& r2)
101{
102 if (!r1 && !r2) return 1;
103 if (r1->numero() == r2->numero()) return 1;
104 return 0;
105}
106
107/* ======================================================= *
108 * ======================================================= *
109 * ======================================================= */
110
111/*! @brief Class TRUST_Ref_Objet_U.
112 *
113 * This class is almost identical to TRUST_Ref<>, except that it does not contain implicit conversion operators.
114 *
115 */
117{
118private:
119 Objet_U * p_ = nullptr;
120
121public:
122 static constexpr bool HAS_POINTER = true;
123
126 TRUST_Ref_Objet_U(const Objet_U& t);
128
129 const TRUST_Ref_Objet_U& operator=(const Objet_U& t);
131
132 explicit operator bool() const noexcept { return p_ != nullptr; }
133
134 inline const Objet_U& valeur() const { assert(p_ != nullptr); return *p_; }
135 inline Objet_U& valeur() { assert(p_ != nullptr); return *p_; }
136 inline const Objet_U* operator ->() const { assert(p_ != nullptr); return p_; }
137 inline Objet_U* operator ->() { assert(p_ != nullptr); return p_; }
138};
139
140int operator ==(const TRUST_Ref_Objet_U& r1, const TRUST_Ref_Objet_U& r2);
141
142using RefObjU = TRUST_Ref_Objet_U;
143
144#endif /* TRUST_Ref_included */
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
Class TRUST_Ref_Objet_U.
Definition TRUST_Ref.h:117
static constexpr bool HAS_POINTER
Definition TRUST_Ref.h:122
const Objet_U * operator->() const
Definition TRUST_Ref.h:136
const TRUST_Ref_Objet_U & operator=(const Objet_U &t)
Definition TRUST_Ref.cpp:27
const Objet_U & valeur() const
Definition TRUST_Ref.h:134
Objet_U & valeur()
Definition TRUST_Ref.h:135
const value_type & valeur() const
Definition TRUST_Ref.h:87
TRUST_Ref()=default
const TRUST_Ref & operator=(const value_type &t)
Definition TRUST_Ref.h:71
TRUST_Ref(const TRUST_Ref &t)
Definition TRUST_Ref.h:69
void reset()
Definition TRUST_Ref.h:95
const TRUST_Ref & operator=(const TRUST_Ref &t)
Definition TRUST_Ref.h:77
static constexpr bool HAS_POINTER
Definition TRUST_Ref.h:64
~TRUST_Ref()=default
value_type & valeur()
Definition TRUST_Ref.h:88
TRUST_Ref(const value_type &t)
Definition TRUST_Ref.h:68
const value_type * operator->() const
Definition TRUST_Ref.h:89
const Nom & le_nom() const =delete