TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
TRUST_List.h
1/****************************************************************************
2* Copyright (c) 2024, 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_List_included
17#define TRUST_List_included
18
19#include <type_traits>
20#include <algorithm> // for std::find
21#include <Motcle.h>
22#include <list> // pour stl list
23
24#define LIST(_TYPE_) TRUST_List<_TYPE_>
25
26/*! @brief classe TRUST_List
27 *
28 * - La classe template TRUST_List est utilisable pour n'importe quelle classe
29 * Utilisation (par exemple):
30 *
31 * - TRUST_List<Milieu_base>
32 */
33template<typename _CLASSE_>
34class TRUST_List : public Objet_U
35{
36 using value_type = _CLASSE_;
37 using STLList = std::list<_CLASSE_>;
38 using Iterator = typename std::list<_CLASSE_>::iterator;
39 using CIterator = typename std::list<_CLASSE_>::const_iterator;
40
41protected:
42 unsigned taille_memoire() const override { throw; }
43 int duplique() const override { throw; }
44
45 Sortie& printOn(Sortie& os) const override
46 {
47 Nom accouverte = "{", accfermee = "}", virgule = ",", blanc = " ";
48 if (size() == 0)
49 os << "vide" << finl;
50 else
51 {
52 os << accouverte << finl;
53 for (auto &itr : list_)
54 {
55 os << itr;
56 if (&itr != &list_.back()) os << blanc << virgule << finl;
57 }
58 os << finl << accfermee << finl;
59 }
60 return os;
61 }
62
63 Entree& readOn(Entree& is) override
64 {
65 Nom accouverte = "{", accfermee = "}", virgule = ",";
66 Motcle nom_;
67 is >> nom_;
68 if (nom_ == (const char*) "vide") return is;
69
70 if (nom_ != accouverte) Cerr << "Error while reading a list. One expected an opened bracket { to start." << finl, Process::exit();
71 _CLASSE_ t;
72
73 while (1)
74 {
75 is >> add(t);
76 is >> nom_;
77 if (nom_ == accfermee) return is;
78 if (nom_ != virgule) Cerr << nom_ << " one expected a ',' or a '}'" << finl, Process::exit();
79 }
80 }
81
82private:
83 STLList list_;
84
85public:
86 // Iterators to enable iterating over the elements of TRUST_List
87 Iterator begin() { return list_.begin(); }
88 Iterator end() { return list_.end(); }
89 const CIterator begin() const { return list_.begin(); }
90 const CIterator end() const { return list_.end(); }
91
92 const std::list<_CLASSE_>& get_stl_list() const { return list_; }
93 std::list<_CLASSE_>& get_stl_list() { return list_; }
94
95 ~TRUST_List() { list_.clear(); }
96 TRUST_List() : list_() { }
97 TRUST_List(const _CLASSE_& t) : list_(t) { }
98 TRUST_List(const TRUST_List& t) : Objet_U(t) { list_ = t.list_; }
99
100 int size() const { return (int)list_.size(); }
101 void vide() { list_.clear(); }
102 const _CLASSE_& front() const { return list_.front(); }
103 _CLASSE_& front() { return list_.front(); }
104 const _CLASSE_& dernier() const { return list_.back(); }
105 _CLASSE_& dernier() { return list_.back(); }
106 int est_vide() const { return list_.empty(); }
107 void suppr(const _CLASSE_& t) { list_.remove(t); }
108
109 /* Add list to list */
110 TRUST_List& add(const TRUST_List& a_list)
111 {
112 list_.insert(list_.end(), a_list.get_stl_list().begin(), a_list.get_stl_list().end());
113 return *this;
114 }
115
116 /* Add element to list */
117 _CLASSE_& add(const _CLASSE_ &t)
118 {
119 list_.push_back(t);
120 return static_cast<_CLASSE_&>(list_.back()); /* attention pour retourner une reference de t dans la liste !!!! */
121 }
122
123 /* Add element to list if it is not already inside */
124 _CLASSE_& add_if_not(const _CLASSE_& t)
125 {
126 auto it = std::find(list_.begin(), list_.end(), t); // Search for the element in the list
127
128 // If the element is not found, add it to the list
129 if (it == list_.end())
130 {
131 list_.push_back(t);
132 it = std::prev(list_.end());
133 }
134
135 return *it;
136 }
137
138 // XXX : Elie Saikali
139 // j'ai tente de supprimer tout ce bordel mais ... bon courage je te laisse faire
140 _CLASSE_& operator[](int i)
141 {
142 assert (size() > 0);
143 int ind = 0;
144 for (auto& itr : list_)
145 {
146 if (ind == i) return itr;
147 ind++;
148 }
149 Process::exit("TRUST_List : overflow of list ");
150 throw;
151 }
152
153 const _CLASSE_& operator[](int i) const
154 {
155 assert (size() > 0);
156 int ind = 0;
157 for (auto& itr : list_)
158 {
159 if (ind == i) return itr;
160 ind++;
161 }
162 Process::exit("TRUST_List : overflow of list ");
163 throw;
164 }
165
166 _CLASSE_& operator[](const Nom& nom) { assert (size() > 0); return operator_<_CLASSE_>(nom); }
167 const _CLASSE_& operator[](const Nom& nom) const { assert (size() > 0); return operator_<_CLASSE_>(nom); }
168
169 _CLASSE_& operator()(int i) { return operator[](i); }
170 const _CLASSE_& operator()(int i) const { return operator[](i); }
171 _CLASSE_& operator()(const Nom& n) { return operator[](n); }
172 const _CLASSE_& operator()(const Nom& n) const { return operator[](n); }
173
174 TRUST_List& operator=(const _CLASSE_ &t)
175 {
176 list_ = t.list_;
177 return *this;
178 }
179
181 {
182 list_ = t.list_;
183 return *this;
184 }
185
186 int contient(const char* const ch) const { return contient_<_CLASSE_>(ch); }
187 int contient(const Objet_U& obj) const { return contient_<_CLASSE_>(obj); }
188 int rang(const char* const ch) const { return rang_<_CLASSE_>(ch); }
189 int rang(const _CLASSE_ &obj) const
190 {
191 int i = 0;
192 for (auto &itr : list_)
193 {
194 if (itr == obj) return i;
195 i++;
196 }
197 return -1;
198 }
199
200private:
201
202 template<typename _TYPE_> std::enable_if_t<_TYPE_::HAS_POINTER, _CLASSE_&>
203 operator_(const Nom& nom)
204 {
205 for (auto& itr : list_)
206 if (itr->le_nom() == nom) return itr;
207 Cerr << "TRUST_List : We have not found an object with name " << nom << finl, throw;
208 return list_.front();
209 }
210
211 template<typename _TYPE_> std::enable_if_t<_TYPE_::HAS_POINTER, const _CLASSE_&>
212 operator_(const Nom& nom) const
213 {
214 for (auto& itr : list_)
215 if (itr->le_nom() == nom) return itr;
216 Cerr << "TRUST_List : We have not found an object with name " << nom << finl, throw;
217 return list_.front();
218 }
219
220 template<typename _TYPE_> std::enable_if_t<_TYPE_::HAS_POINTER, int>
221 rang_(const char* const ch) const
222 {
223 Nom nom(ch);
224 int ind = 0;
225 for (auto& itr : list_)
226 {
227 if (itr->le_nom() == nom) return ind;
228 ind++;
229 }
230 return -1;
231 }
232
233 template<typename _TYPE_> std::enable_if_t<_TYPE_::HAS_POINTER, int>
234 contient_(const char* const ch) const
235 {
236 Nom nom(ch);
237 for (auto& itr : list_)
238 if (itr->le_nom() == nom) return 1;
239 return 0;
240 }
241
242 template<typename _TYPE_> std::enable_if_t<_TYPE_::HAS_POINTER, int>
243 contient_(const Objet_U& obj) const
244 {
245 for (auto &itr : list_)
246 if (itr->le_nom() == obj) return 1;
247 return 0;
248 }
249
250 template<typename _TYPE_> std::enable_if_t<!_TYPE_::HAS_POINTER, _CLASSE_&>
251 operator_(const Nom& nom)
252 {
253 for (auto& itr : list_)
254 if (itr.le_nom() == nom) return itr;
255 Cerr << "TRUST_List : We have not found an object with name " << nom << finl, throw;
256 return list_.front();
257 }
258
259 template<typename _TYPE_> std::enable_if_t<!_TYPE_::HAS_POINTER, const _CLASSE_&>
260 operator_(const Nom& nom) const
261 {
262 for (auto& itr : list_)
263 if (itr.le_nom() == nom) return itr;
264 Cerr << "TRUST_List : We have not found an object with name " << nom << finl, throw;
265 return list_.front();
266 }
267
268 template<typename _TYPE_> std::enable_if_t<!_TYPE_::HAS_POINTER, int>
269 rang_(const char* const ch) const
270 {
271 Nom nom(ch);
272 int ind = 0;
273 for (auto& itr : list_)
274 {
275 if (itr.le_nom() == nom) return ind;
276 ind++;
277 }
278 return -1;
279 }
280
281 template<typename _TYPE_> std::enable_if_t<!_TYPE_::HAS_POINTER, int>
282 contient_(const char* const ch) const
283 {
284 Nom nom(ch);
285 for (auto& itr : list_)
286 if (itr.le_nom() == nom) return 1;
287 return 0;
288 }
289
290 template<typename _TYPE_> std::enable_if_t<!_TYPE_::HAS_POINTER, int>
291 contient_(const Objet_U& obj) const
292 {
293 for (auto &itr : list_)
294 if (itr.le_nom() == obj) return 1;
295 return 0;
296 }
297};
298
299#endif /* TRUST_List_included */
Une chaine de caractere (Nom) en majuscules.
Definition Motcle.h:26
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
friend class Entree
Definition Objet_U.h:76
friend class Sortie
Definition Objet_U.h:75
Objet_U()
Constructeur par defaut : attribue un numero d'identifiant unique a l'objet (object_id_),...
Definition Objet_U.cpp:55
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Sortie & printOn(Sortie &os) const override
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition TRUST_List.h:45
_CLASSE_ & operator()(const Nom &n)
Definition TRUST_List.h:171
TRUST_List(const _CLASSE_ &t)
Definition TRUST_List.h:97
int rang(const char *const ch) const
Definition TRUST_List.h:188
int contient(const char *const ch) const
Definition TRUST_List.h:186
const _CLASSE_ & operator[](int i) const
Definition TRUST_List.h:153
const std::list< _CLASSE_ > & get_stl_list() const
Definition TRUST_List.h:92
_CLASSE_ & add_if_not(const _CLASSE_ &t)
Definition TRUST_List.h:124
int rang(const _CLASSE_ &obj) const
Definition TRUST_List.h:189
TRUST_List & operator=(const _CLASSE_ &t)
Definition TRUST_List.h:174
Iterator begin()
Definition TRUST_List.h:87
const _CLASSE_ & front() const
Definition TRUST_List.h:102
unsigned taille_memoire() const override
Definition TRUST_List.h:42
const _CLASSE_ & operator[](const Nom &nom) const
Definition TRUST_List.h:167
_CLASSE_ & add(const _CLASSE_ &t)
Definition TRUST_List.h:117
TRUST_List & add(const TRUST_List &a_list)
Definition TRUST_List.h:110
std::list< _CLASSE_ > & get_stl_list()
Definition TRUST_List.h:93
int duplique() const override
Definition TRUST_List.h:43
const _CLASSE_ & operator()(const Nom &n) const
Definition TRUST_List.h:172
int contient(const Objet_U &obj) const
Definition TRUST_List.h:187
const _CLASSE_ & dernier() const
Definition TRUST_List.h:104
int size() const
Definition TRUST_List.h:100
const CIterator end() const
Definition TRUST_List.h:90
void suppr(const _CLASSE_ &t)
Definition TRUST_List.h:107
_CLASSE_ & front()
Definition TRUST_List.h:103
Entree & readOn(Entree &is) override
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition TRUST_List.h:63
const CIterator begin() const
Definition TRUST_List.h:89
const _CLASSE_ & operator()(int i) const
Definition TRUST_List.h:170
int est_vide() const
Definition TRUST_List.h:106
TRUST_List & operator=(const TRUST_List &t)
Definition TRUST_List.h:180
_CLASSE_ & operator()(int i)
Definition TRUST_List.h:169
void vide()
Definition TRUST_List.h:101
TRUST_List(const TRUST_List &t)
Definition TRUST_List.h:98
Iterator end()
Definition TRUST_List.h:88
_CLASSE_ & dernier()
Definition TRUST_List.h:105
_CLASSE_ & operator[](const Nom &nom)
Definition TRUST_List.h:166
_CLASSE_ & operator[](int i)
Definition TRUST_List.h:140