TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Objet_U_ptr.cpp
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#include <Synonyme_info.h>
17#include <Objet_U_ptr.h>
18#include <Memoire.h>
19#include <Nom.h>
20
21Implemente_base_sans_constructeur_ni_destructeur(Objet_U_ptr,"Objet_U_ptr",Objet_U);
22
24{
25 const Objet_U * objet = get_Objet_U_ptr();
26 if (objet)
27 {
28 os << objet->le_type() << finl;
29 os << (*objet);
30 }
31 else os << "vide" << finl;
32
33 return os;
34}
35
37{
38 detach(); // Clear the existing object
39 static Nom nom_type; // static to avoid creating an Objet_U on every call
40 is >> nom_type;
41 Objet_U * objet = nullptr;
42 if (nom_type != "vide")
43 {
44 objet = typer(nom_type);
45 if (! objet) Process::exit();
46 }
47
48 set_Objet_U_ptr(objet);
49 if (objet) is >> (*objet); // Read the object
50
51 return is;
52}
53
54/*! @brief Destructor.
55 *
56 * It does not destroy the referenced object.
57 *
58 */
60{
61 cle_ = -2; // Paranoia: invalidate the pointer
62}
63
64/*! @brief Builds a null pointer (key set to -1).
65 *
66 */
67Objet_U_ptr::Objet_U_ptr() : cle_(-1), ptr_object_id_(-1) { }
68
70{
71 Objet_U * ptr = get_Objet_U_ptr();
72 if (ptr) delete ptr;
73 set_Objet_U_ptr(nullptr);
74}
75
77{
79 assert(ptr != nullptr);
80 int resu = ptr->associer_(objet);
81 return resu;
82}
83
84
85/*! @brief Checks if the pointer is valid.
86 *
87 * The pointer is valid if cle_==-1
88 * or if la_memoire().objet_u(cle_) has the same object_id_ as
89 * the one registered in ptr_object_id_.
90 * If the pointer is not valid, the program stops.
91 * Returns the address of the pointed object (of type Objet_U).
92 *
93 */
95{
96 if (cle_ != -1 || ptr_object_id_ != -1)
97 {
98 Objet_U& objet = Memoire::Instance().objet_u(cle_);
99 Objet_U * addr = &objet;
100 const int id = objet.get_object_id();
101 if (id == ptr_object_id_)
102 return addr;
103 else
104 {
105 Cerr << "(PE" << me() << ") ";
106 Cerr << "Error in Objet_U_ptr::get_Objet_U_ptr_check() : id != ptr_object_id_\n";
107 Cerr << " Pointer type " << le_type();
108 Cerr << "\n cle_ = " << cle_;
109 Cerr << "\n ptr_object_id_ = " << ptr_object_id_;
110 Cerr << "\n id = " << id;
111 std::cerr << "\n &la_memoire().objet_u(cle_) = " << addr << std::endl;
112 // If it crashes at this point, it means the referenced object
113 // has been destroyed but the reference is still in use.
114 exit();
115 }
116 }
117 return 0;
118}
119
120/*! @brief Verifies that the object pointed to by ptr is of an acceptable type for the pointer (via get_info_ptr).
121 *
122 */
124{
125 if (ptr == nullptr) return 1; // The null pointer is valid
126
127 const Objet_U& objet = *ptr;
128 // Check that the object is of the correct type:
129 // type accepted by the pointer:
130 const Type_info& type_info_ptr = get_info_ptr();
131 // type of the object:
132 const Type_info& type_info_obj = *(objet.get_info());
133 // Can we cast type_info_obj to type_info_ptr?
134 if (! type_info_ptr.can_cast(&type_info_obj))
135 {
136 Cerr << "(PE" << me() << ") ";
137 Cerr << "Error in Objet_U_ptr::get_Objet_U_ptr_check() : Type error\n";
138 Cerr << " Pointer type " << le_type();
139 Cerr << "\n cle_ = " << cle_;
140 Cerr << "\n ptr_object_id_ = " << ptr_object_id_;
141 std::cerr << "\n &memoire.objet_u(cle_) = " << ptr;
142 Cerr << "\n Type accepted by the pointer : " << type_info_ptr.name();
143 Cerr << "\n Object type in reference : " << type_info_obj.name();
145 }
146 return 1;
147}
148
149
150
151/*! @brief Updates keys when Objet_U objects have been renumbered.
152 *
153 * @param (const int* const new_ones) array of new numbering
154 * @return (int) the new key of the pointer
155 */
156int Objet_U_ptr::change_num(const int* const new_ones)
157{
158 Objet_U::change_num(new_ones);
159
160 // Do not call a function that does "verifie"
161 // because the memory is currently being modified.
162 if (cle_ > -1)
163 cle_ = new_ones[cle_];
164 return cle_;
165}
166
167/*! @brief Duplicates the Objet_U obj and then changes the pointer to this copy.
168 *
169 * @param (const Objet_U& obj) reference to the Objet_U to copy
170 */
172{
173 int cle = obj.duplique();
174 Memoire& memoire = Memoire::Instance();
175 Objet_U& objet = memoire.objet_u(cle);
176 set_Objet_U_ptr(& objet);
177}
178
179/*! @brief Returns a pointer to the associated Objet_U. WARNING: the address may be null (if the pointer is null).
180 *
181 */
183{
184 const Objet_U * objet;
185 if (cle_ < 0)
186 {
187 objet = nullptr;
188 }
189 else
190 {
191 Memoire& memoire = Memoire::Instance();
192 objet = & memoire.objet_u(cle_);
193 }
194 assert(objet == get_Objet_U_ptr_check());
195 return (Objet_U*) objet;
196}
197
198/*! @brief Makes *this point to the object *ptr. The address may be null (null pointer).
199 *
200 */
202{
203 if (ptr != nullptr)
204 {
205 cle_ = ptr->numero();
206 ptr_object_id_ = ptr->get_object_id();
207 }
208 else
209 {
210 cle_ = -1;
211 ptr_object_id_ = -1;
212 }
213 // It is sufficient to check the type here: if the type is correct here and object_id_ does not change afterwards, then the type remains correct.
214 assert(check_Objet_U_ptr_type(ptr));
215 assert(get_Objet_U_ptr_check() == ptr);
216}
217
218/*! @brief Tries to create an instance of type "type".
219 *
220 * If type is not a type or type is not instantiable => stop.
221 * If type is not a subtype of the pointer type => return 0.
222 * If ok, returns the address of the created object.
223 */
224Objet_U * Objet_U_ptr::typer(const char * type)
225{
226 const Type_info * type_info = Type_info::type_info_from_name(type); // Type_info of the requested type
227 const Type_info& type_base = get_info_ptr(); // Base type of the OWN_PTR
228
229 if ( get_Objet_U_ptr()) detach();
230
231 Objet_U * instance = nullptr;
232
233 if (type_info == 0)
234 {
236
237 if (syn_info!=0) return typer(syn_info->org_name_());
238 else
239 {
240 Cerr << "Error in Deriv_::typer_(const char* const type)" << finl << type << " is not a type." << finl;
241 Cerr << "Type required : derived from " << type_base.name() << finl << finl;
242 Cerr << type << " is not a recognized keyword." << finl << "Check your data set." << finl;
243 Nom nompb = type;
244 if (nompb.find("TURBULENT") != -1 || nompb.find("TURBULENCE") != -1)
245 Cerr << finl << "*** NOTE :: Since TRUST V1.8.0, turbulence models are in TrioCFD and not anymore in TRUST.\nTry using TrioCFD executable or contact TRUST support." << finl;
247 }
248 }
249 if (! type_info->instanciable())
250 {
251 Cerr << "Error in Deriv_::typer_(const char* const type).\n" << type << " is not instanciable." << finl;
253 }
254
255 if (! type_info->has_base(&type_base))
256 Cerr << "Error in Deriv_::typer_(const char* const type).\n " << type << " is not a subtype of " << type_base.name() << finl;
257 else
258 instance = type_info->instance(); // Creates an instance of the type described in type_info
259
260 set_Objet_U_ptr(instance);
261 return instance;
262}
263
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
La memoire de Trio-U.
Definition Memoire.h:30
Objet_U & objet_u(int)
Returns a reference to the Objet_U at index num in memory.
Definition Memoire.cpp:206
static Memoire & Instance()
Returns a pointer to the memory instance. Creates a new memory object if no instance has been created...
Definition Memoire.cpp:32
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
virtual int find(const char *const n) const
Definition Nom.cpp:309
Pointer to an Objet_U.
Definition Objet_U_ptr.h:31
int associer_(Objet_U &objet) override
Associates the Objet_U with another Objet_U. Virtual method to override.
int change_num(const int *const) override
Updates keys when Objet_U objects have been renumbered.
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.
int check_Objet_U_ptr_type(const Objet_U *ptr) const
Verifies that the object pointed to by ptr is of an acceptable type for the pointer (via get_info_ptr...
~Objet_U_ptr() override
Destructor.
Objet_U_ptr()
Builds a null pointer (key set to -1).
Objet_U * typer(const char *nom_type)
Tries to create an instance of type "type".
virtual Objet_U * get_Objet_U_ptr() const
Returns a pointer to the associated Objet_U. WARNING: the address may be null (if the pointer is null...
virtual const Type_info & get_info_ptr() const =0
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
virtual int duplique() const =0
virtual int associer_(Objet_U &)
Associates the Objet_U with another Objet_U. Virtual method to override.
Definition Objet_U.cpp:200
int numero() const
Returns the index of the object in Memoire::data.
Definition Objet_U.cpp:264
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual int change_num(const int *const)
Changes the internal number of the Objet_U.
Definition Objet_U.cpp:180
const char * le_type() const
Returns the type name of the Objet_U.
Definition Objet_U.cpp:190
Objet_U()
Default constructor: assigns a unique identifier to the object (object_id_) and registers the object ...
Definition Objet_U.cpp:54
int get_object_id() const
Returns the unique identifier of the object (object_id_).
Definition Objet_U.cpp:95
virtual const Type_info * get_info() const
Returns type information for the Objet_U.
Definition Objet_U.cpp:126
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static int me()
Returns the rank of the local processor in the current communication group. See Comm_Group::rank() an...
Definition Process.cpp:122
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
Models synonym information for Objet_U objects.
static const Synonyme_info * synonyme_info_from_name(const char *synonyme_name)
Static method that returns a pointer to the Synonyme_info whose name is "synonyme_name".
const char * org_name_() const
Models type information for Objet_U objects.
Definition Type_info.h:30
int instanciable() const
Returns 1 if the associated type is instantiable (cree_instance is non-null), 0 otherwise.
Objet_U * instance() const
Creates an instance of the class associated with the type_info.
int has_base(const Type_info *p, int direct=0) const
Tests whether a type belongs to the base types of the considered type. If direct == 0,...
int can_cast(const Type_info *p) const
Traverses the type hierarchy. Returns 1 if p points to a subtype of the current type.
Definition Type_info.h:100
static const Type_info * type_info_from_name(const char *type_name)
Static method that returns a pointer to the Type_info whose name is "type_name".
const Nom & name() const
Definition Type_info.h:36