TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
TRUST_Deriv.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_Deriv_included
17#define TRUST_Deriv_included
18
19#include <Objet_U_ptr.h>
20#include <Nom.h>
21
22/*! @brief Template ownership pointer class TRUST_Deriv.
23 *
24 * Given a class Type_base deriving from Objet_U, TRUST_Deriv<Type_base> is a small template class
25 * containing a pointer to an instance of Type_base, or any class deriving from Type_base,
26 * created dynamically at runtime.
27 *
28 * Definition of the DERIV:
29 *
30 * #include <TRUST_Deriv.h>
31 * #include <Type_base.h>
32 * TRUST_Deriv<Type_base>
33 *
34 * Creating an object of type OWN_PTR(Type_base):
35 * OWN_PTR(Type_base) deriv_type; // deriv_type is still a null pointer
36 *
37 * Assuming class Type_Derive derives from Type_base and is instantiable:
38 * class Type_Derive : Type_base
39 * {
40 * Declare_instanciable(Type_Derive);;
41 * ...
42 * };
43 *
44 * Creating an instance of Type_Derive:
45 * deriv_type.typer("Type_Derive");
46 *
47 * Accessing the Type_Derive instance:
48 * Type_Derive & objet_derive = deriv_type.valeur();
49 * const Type_Derive & objet_derive = deriv_type.valeur();
50 *
51 * The Type_Derive instance is destroyed when typer() is called again,
52 * or when the deriv_type object itself is destroyed.
53 *
54 */
55
56// MACRO to replace OWN_PTR(THECLASS) by Deriv_THECLASS & keep previous syntax for some developers
57#define DERIV(_TYPE_) \
58 static_assert(false, "The old DERIV MACRO is now deprecated. Please use OWN_PTR instead.")
59
60#define OWN_PTR(_TYPE_) TRUST_Deriv<_TYPE_>
61
62template<typename _CLASSE_>
64{
65protected:
66 unsigned taille_memoire() const override { throw; }
67
68 int duplique() const override
69 {
70 TRUST_Deriv *xxx = new TRUST_Deriv(*this);
71#ifndef LATATOOLS
72 if (!xxx) Process::exit("Not enough memory !");
73#endif
74 return xxx->numero();
75 }
76
77 Sortie& printOn(Sortie& os) const override { return Objet_U_ptr::printOn(os); }
78
79 Entree& readOn(Entree& is) override { return Objet_U_ptr::readOn(is); }
80
81 void set_Objet_U_ptr(Objet_U *objet) override
82 {
84 /* Note: this type conversion is non-trivial if _TYPE_ comes from multiple inheritance. */
85 if (objet) pointeur_ = (_CLASSE_*) objet;
86 else pointeur_ = nullptr;
87 }
88
89private:
90 _CLASSE_ *pointeur_ = nullptr;
91
92 int reprendre(Entree&) override { return -100; /* NOT POSSIBLE */ }
93 int sauvegarder(Sortie&) const override { return -100; /* NOT POSSIBLE */ }
94
95public:
97 TRUST_Deriv() : Objet_U_ptr(), pointeur_(nullptr) { }
99 {
100 if (t) recopie(t.valeur());
101 }
102
103 TRUST_Deriv(const _CLASSE_& t) : TRUST_Deriv()
104 {
105 recopie(t);
106 }
107
108 inline const _CLASSE_& valeur() const
109 {
110 assert(pointeur_ != nullptr);
111#ifndef TRUST_USE_GPU
112 assert(get_Objet_U_ptr_check() || 1);
113#endif
114 return *pointeur_;
115 }
116
117 inline _CLASSE_& valeur()
118 {
119 assert(pointeur_ != nullptr);
120#ifndef TRUST_USE_GPU
121 assert(get_Objet_U_ptr_check() || 1);
122#endif
123 return *pointeur_;
124 }
125
126 inline const _CLASSE_* operator ->() const
127 {
128 assert(pointeur_ != nullptr);
129 assert(get_Objet_U_ptr_check() || 1);
130 return pointeur_;
131 }
132
133 inline _CLASSE_* operator ->()
134 {
135 assert(pointeur_ != nullptr);
136 assert(get_Objet_U_ptr_check() || 1);
137 return pointeur_;
138 }
139
140 const TRUST_Deriv& operator=(const _CLASSE_& t)
141 {
142 if (pointeur_ != (&t))
143 {
144 detach();
145 recopie(t);
146 }
147 return *this;
148 }
149
151 {
152 if (pointeur_ != t.pointeur_)
153 {
154 detach();
155 if (t) recopie(t.valeur());
156 }
157 return *this;
158 }
159
160 operator const _CLASSE_& () const { return valeur(); }
161 operator _CLASSE_& () { return valeur(); }
162
163 const Type_info& get_info_ptr() const override
164 {
165 const Type_info * type_info = _CLASSE_::info();
166 return *type_info; /* base type accepted by the ref */
167 }
168
169 Entree& typer_lire_simple(Entree& is, const char* msg = "??")
170 {
171 return typer_lire(is, "??", msg);
172 }
173
174 Entree& typer_lire(Entree& is, const char* b = "??", const char* msg = "??")
175 {
176 Nom type, base;
177
178 if (strcmp(msg, "??") != 0)
179 Cerr << msg << " ";
180
181 if (strcmp(b, "??") != 0)
182 base = b;
183
184 is >> type; // Read the type
185
186 if (base != "??")
187 type = base + type;
188
189 typer(type); // Instantiate the type
190
191 if (strcmp(msg, "??") != 0)
192 Cerr << valeur().que_suis_je() << finl;
193
194 is >> valeur(); // Read the class instance
195
196 return is;
197 }
198};
199
200/* ======================================================= *
201 * ======================================================= *
202 * ======================================================= */
203
204/*! @brief Class TRUST_Deriv_Objet_U is almost identical to TRUST_Deriv<Objet_U>,
205 * except that it does not contain the implicit conversion operators from OWN_PTR(Objet_U) to Objet_U.
206 *
207 * It provides 3 additional methods:
208 *
209 * - deplace(TRUST_Deriv_Objet_U& )
210 * - reprendre
211 * - sauvegarder
212 *
213 * Usage:
214 * - TRUST_Deriv_Objet_U
215 * or
216 * - DerObjU
217 */
219{
220 Declare_instanciable_sans_constructeur(TRUST_Deriv_Objet_U);
221protected:
222 void set_Objet_U_ptr(Objet_U *objet) override;
223
224private:
225 Objet_U *pointeur_;
226
227public:
231 const Type_info& get_info_ptr() const override;
232 const TRUST_Deriv_Objet_U& operator=(const Objet_U& t);
234#ifndef LATATOOLS
235 int reprendre(Entree& is) override;
236 int sauvegarder(Sortie& os) const override;
237#endif
238 void deplace(TRUST_Deriv_Objet_U& deriv_obj);
239
240 inline const Objet_U& valeur() const
241 {
242 assert(pointeur_ != nullptr);
243 assert(get_Objet_U_ptr_check() || 1);
244 return *pointeur_;
245 }
246
247 inline Objet_U& valeur()
248 {
249 assert(pointeur_ != nullptr);
250 assert(get_Objet_U_ptr_check() || 1);
251 return *pointeur_;
252 }
253
254 inline Objet_U* operator ->() const
255 {
256 assert(pointeur_ != nullptr);
257 assert(get_Objet_U_ptr_check() || 1);
258 return pointeur_;
259 }
260};
261
262using DerObjU = TRUST_Deriv_Objet_U;
263
264#endif /* TRUST_Deriv_included */
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
Objet_U * get_Objet_U_ptr_check() const
Checks if the pointer is valid.
virtual void set_Objet_U_ptr(Objet_U *)
Makes *this point to the object *ptr. The address may be null (null pointer).
void recopie(const Objet_U &)
Duplicates the Objet_U obj and then changes the pointer to this copy.
Objet_U_ptr(const Objet_U_ptr &)=delete
Objet_U * typer(const char *nom_type)
Tries to create an instance of type "type".
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
friend class Entree
Definition Objet_U.h:71
int numero() const
Returns the index of the object in Memoire::data.
Definition Objet_U.cpp:264
friend class Sortie
Definition Objet_U.h:70
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
Objet_U()
Default constructor: assigns a unique identifier to the object (object_id_) and registers the object ...
Definition Objet_U.cpp:54
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
Base class for output streams.
Definition Sortie.h:52
Class TRUST_Deriv_Objet_U is almost identical to TRUST_Deriv<Objet_U>, except that it does not contai...
int sauvegarder(Sortie &os) const override
Saves an Objet_U to an output stream. Virtual method to override.
int reprendre(Entree &is) override
Restores an Objet_U from an input stream. Virtual method to override.
const Type_info & get_info_ptr() const override
Objet_U * operator->() const
const Objet_U & valeur() const
const TRUST_Deriv_Objet_U & operator=(const Objet_U &t)
void deplace(TRUST_Deriv_Objet_U &deriv_obj)
void set_Objet_U_ptr(Objet_U *objet) override
Makes *this point to the object *ptr. The address may be null (null pointer).
int duplique() const override
Definition TRUST_Deriv.h:68
const _CLASSE_ & valeur() const
Sortie & printOn(Sortie &os) const override
Writes the object to an output stream. Virtual method to override.
Definition TRUST_Deriv.h:77
TRUST_Deriv(const TRUST_Deriv &t)
Definition TRUST_Deriv.h:98
_CLASSE_ & valeur()
const _CLASSE_ * operator->() const
const TRUST_Deriv & operator=(const _CLASSE_ &t)
Entree & typer_lire_simple(Entree &is, const char *msg="??")
Entree & typer_lire(Entree &is, const char *b="??", const char *msg="??")
unsigned taille_memoire() const override
Definition TRUST_Deriv.h:66
const TRUST_Deriv & operator=(const TRUST_Deriv &t)
void set_Objet_U_ptr(Objet_U *objet) override
Makes *this point to the object *ptr. The address may be null (null pointer).
Definition TRUST_Deriv.h:81
Entree & readOn(Entree &is) override
Reads an Objet_U from an input stream. Virtual method to override.
Definition TRUST_Deriv.h:79
const Type_info & get_info_ptr() const override
TRUST_Deriv(const _CLASSE_ &t)
Models type information for Objet_U objects.
Definition Type_info.h:30