TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TRUST_Vector.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_Vector_included
17#define TRUST_Vector_included
18
19#include <type_traits>
20#include <algorithm> // pour std::transform
21#include <vector>
22#include <memory> // pour std::shared_ptr
23#include <Nom.h>
24#include <Separateur.h>
25
26class MD_Vector;
27
28// MACRO to replace VECT(THECLASS) by TRUST_Vector<THECLASS> & keep previous syntax for some developers
29#define VECT(_TYPE_) TRUST_Vector<_TYPE_>
30
31/*! @brief classe TRUST_Vector
32 *
33 * - La classe template TRUST_Vector est utilisable pour n'importe quelle classe
34 *
35 * Utilisation (par exemple):
36 *
37 * - TRUST_Vector<MD_Vector>
38 * - TRUST_Vector<Milieu_base>
39 * - TRUST_Vector<TRUSTArray<double>>
40 */
41template<typename _CLASSE_>
42class TRUST_Vector: public Objet_U
43{
44 using value_type = _CLASSE_;
45 using pointer_type = std::shared_ptr<_CLASSE_>;
46 using STLVect = std::vector<pointer_type>; // vector of shared ptr to _CLASSE_
47 using Iterator = typename STLVect::iterator;
48 using Const_Iterator = typename STLVect::const_iterator;
49
50 // XXX : Elie Saikali
51 // We have an std vector of shared ptrs => iterators return pointers and not the passed value type
52 // These classes allows a return of the value type
53 class IteratorWrapper
54 {
55 public:
56 explicit IteratorWrapper(Iterator it) : it_(it) { }
57 value_type& operator*() { return *it_->get(); }
58 value_type* operator->() { return it_->get(); }
59 IteratorWrapper& operator++() { ++it_; return *this; }
60 IteratorWrapper operator++(int) { return IteratorWrapper(it_++); }
61 bool operator==(const IteratorWrapper& other) const { return it_ == other.it_; }
62 bool operator!=(const IteratorWrapper& other) const { return it_ != other.it_; }
63
64 private:
65 Iterator it_;
66 };
67
68 class ConstIteratorWrapper
69 {
70 public:
71 explicit ConstIteratorWrapper(Const_Iterator it) : it_(it) {}
72 const value_type& operator*() const { return *it_->get(); }
73 const value_type* operator->() const { return it_->get(); }
74 ConstIteratorWrapper& operator++() { ++it_; return *this; }
75 ConstIteratorWrapper operator++(int) { return ConstIteratorWrapper(it_++); }
76 bool operator==(const ConstIteratorWrapper& other) const { return it_ == other.it_; }
77 bool operator!=(const ConstIteratorWrapper& other) const { return it_ != other.it_; }
78
79 private:
80 Const_Iterator it_;
81 };
82
83protected:
84 unsigned taille_memoire() const override { throw; }
85 int duplique() const override { throw; }
86#ifndef LATATOOLS
87 Sortie& printOn(Sortie& s) const override { return printOn_<_CLASSE_>(s); }
88 Entree& readOn(Entree& s) override { return readOn_<_CLASSE_>(s); }
89#else
90 Sortie& printOn(Sortie& s) const override { return s; }
91 Entree& readOn(Entree& s) override { return s; }
92#endif
93private:
94 STLVect z_vect_;
95
96#ifndef LATATOOLS
97 template<typename _TYPE_>
98 std::enable_if_t< !(std::is_same<_TYPE_,MD_Vector>::value), Entree&>
99 readOn_(Entree& s)
100 {
101 int i;
102 s >> i;
103 clear();
104 _TYPE_ obj;
105 for (int ind = 0; ind < i; ind++)
106 {
107 s >> obj;
108 add(std::move(obj));
109 }
110 return s;
111 }
112
113 template<typename _TYPE_>
114 std::enable_if_t< !(std::is_same<_TYPE_,MD_Vector>::value), Sortie&>
115 printOn_(Sortie& s) const
116 {
117 s << (int) z_vect_.size() << tspace;
118 for (auto &itr : z_vect_) s << *itr << tspace;
119 s << finl;
120 return s;
121 }
122
123 // MD_Vector class does not derive from Objet_U => no readOn & printOn
124 template<typename _TYPE_>
125 std::enable_if_t<(std::is_same<_TYPE_,MD_Vector>::value), Entree&>
126 readOn_(Entree& s) { return s; }
127
128 template<typename _TYPE_>
129 std::enable_if_t<(std::is_same<_TYPE_,MD_Vector>::value), Sortie&>
130 printOn_(Sortie& s) const { return s ; }
131#else
132 Sortie& printOn_(Sortie& s) const { return s; }
133 Entree& readOn_(Entree& s) { return s; }
134#endif
135public:
136 ~TRUST_Vector() { z_vect_.clear(); }
137
138 TRUST_Vector() = default;
139
140 explicit TRUST_Vector(int i) { dimensionner_force(i); } /* clear, resize to i & fill with i empty shared ptrs */
141
142 // XXX : Elie Saikali
143 // Nota Bene : Deep copy : should not use the same memory !! So dangerous attention !!
144 TRUST_Vector(const TRUST_Vector& avect) : Objet_U(avect)
145 {
146 clear();
147 for (int i = 0; i < avect.size(); i++)
148 z_vect_.push_back(std::make_shared<_CLASSE_>(*avect.z_vect_[i]));
149 }
150
151 // get stl vector : attention its a vector of shared ptrs !!
152 const STLVect& get_stl_vect() const { return z_vect_; }
153 STLVect& get_stl_vect() { return z_vect_; }
154
155 // iterators on TRUST_Vector
156 IteratorWrapper begin() { return IteratorWrapper(z_vect_.begin()); }
157 IteratorWrapper end() { return IteratorWrapper(z_vect_.end()); }
158 const ConstIteratorWrapper begin() const { return ConstIteratorWrapper(z_vect_.begin()); }
159 const ConstIteratorWrapper end() const { return ConstIteratorWrapper(z_vect_.end()); }
160
161 /* Iterator begin() { return (z_vect_.begin()); }
162 Iterator end() { return (z_vect_.end()); }
163 const Const_Iterator begin() const { return (z_vect_.begin()); }
164 const Const_Iterator end() const { return (z_vect_.end()); } */
165
166 const value_type& operator[](int i) const { return *z_vect_[i]; }
167 value_type& operator[](int i) { return *z_vect_[i]; }
168
169 // XXX : Elie Saikali : Pas de negociation pour ca desole ...
170 const value_type& operator()(int i) const = delete;
171 value_type& operator()(int i) = delete;
172 value_type& add() = delete; //{ return add(_CLASSE_()); }
173
174 int size() const { return (int)z_vect_.size(); }
175 void reset() { z_vect_.clear(); }
176 void clear() { z_vect_.clear(); }
177 void resize(int i) { dimensionner(i); }
178 value_type& back() { return *(z_vect_.back()); }
179 value_type& front() { return *(z_vect_.front()); }
180
181 // XXX : Elie Saikali
182 // Attention : it is so important to implement dimensionner and dimensionner_force as this otherwise issue with the standard = operator.
183 // A pre-initialised shared ptr is required in each zone memory of the sized vector.
184 void dimensionner(int i)
185 {
186 const int old_size = (int)z_vect_.size();
187 if (old_size == i) return;
188
189 if (old_size != 0)
190 {
191 Cerr << "WARNING : dimensionner method of a TRUST_Vector" << finl;
192 Cerr << "Old vector size : " << old_size << finl;
193 Cerr << "New vector size : " << i << finl;
194 }
195
196 z_vect_.resize(i);
197 for (int j = old_size; j < i; j++)
198 z_vect_[j] = std::make_shared<_CLASSE_>();
199 }
200
202 {
203 z_vect_.clear();
204 z_vect_.resize(i);
205 for (auto& itr : z_vect_) itr = std::make_shared<_CLASSE_>();
206 }
207#ifndef LATATOOLS
208 Entree& lit(Entree& s) { return readOn_<_CLASSE_>(s); }
209#endif
211 {
212 if (this == &avect) return *this;
213
214 clear();
215 for (int i = 0; i < avect.size(); i++)
216 z_vect_.push_back(std::make_shared<_CLASSE_>(*avect.z_vect_[i]));
217
218 return *this;
219 }
220
221 /* Add a new element to the vect */
222 value_type& add(value_type&& data_to_add)
223 {
224 z_vect_.emplace_back(std::make_shared<_CLASSE_>(std::forward<_CLASSE_>(data_to_add)));
225 return *(z_vect_.back());
226 }
227
228 value_type& add(const value_type& data_to_add)
229 {
230 z_vect_.push_back(std::make_shared<_CLASSE_>(data_to_add));
231 return *(z_vect_.back());
232 }
233
234 /* Append a vect to a vect */
235 void add(const TRUST_Vector& v2)
236 {
237 STLVect tmp_;
238 tmp_.reserve(v2.size());
239 auto lambda_ = [](pointer_type const &iter) { return std::make_shared<_CLASSE_>(*iter); };
240 std::transform(v2.get_stl_vect().begin(), v2.get_stl_vect().end(), std::back_inserter(tmp_), lambda_);
241 z_vect_.insert(z_vect_.end(), std::make_move_iterator(tmp_.begin()), std::make_move_iterator(tmp_.end()));
242 }
243};
244
245#endif /* TRUST_Vector_included */
: Cette classe est un OWN_PTR mais l'objet pointe est partage entre plusieurs
Definition MD_Vector.h:48
friend int operator!=(const Objet_U &, const Objet_U &)
Comparaison de deux Objet_U x et y Renvoie 1 - x.
Definition Objet_U.cpp:244
friend class Entree
Definition Objet_U.h:76
friend class Sortie
Definition Objet_U.h:75
friend int operator==(const Objet_U &, const Objet_U &)
retourne x.
Definition Objet_U.cpp:231
Objet_U()
Constructeur par defaut : attribue un numero d'identifiant unique a l'objet (object_id_),...
Definition Objet_U.cpp:55
Classe de base des flux de sortie.
Definition Sortie.h:52
Entree & lit(Entree &s)
value_type & operator[](int i)
value_type & back()
IteratorWrapper begin()
TRUST_Vector(int i)
void dimensionner_force(int i)
value_type & add(const value_type &data_to_add)
void dimensionner(int i)
IteratorWrapper end()
TRUST_Vector()=default
TRUST_Vector & operator=(const TRUST_Vector &avect)
void resize(int i)
int duplique() const override
void add(const TRUST_Vector &v2)
value_type & add(value_type &&data_to_add)
value_type & front()
const STLVect & get_stl_vect() const
const ConstIteratorWrapper begin() const
const ConstIteratorWrapper end() const
Entree & readOn(Entree &s) override
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
value_type & add()=delete
TRUST_Vector(const TRUST_Vector &avect)
Sortie & printOn(Sortie &s) const override
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
STLVect & get_stl_vect()
value_type & operator()(int i)=delete
const value_type & operator[](int i) const
const value_type & operator()(int i) const =delete
unsigned taille_memoire() const override
int size() const