GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
SetOf.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _libDEM_SetOf_hpp_
31 #define _libDEM_SetOf_hpp_
32 
33 #include <string>
34 #include <vector>
35 #include <map>
36 #include <list>
37 
38 #include <boost/serialization/vector.hpp>
39 #include <boost/serialization/string.hpp>
40 #include <boost/serialization/serialization.hpp>
41 #include <boost/serialization/export.hpp>
42 #include <boost/type_traits.hpp>
43 #include <boost/static_assert.hpp>
44 
45 #include <vtkUnstructuredGrid.h>
46 
51 #include "GranOO3/Core/Signal.hpp"
52 
53 namespace GranOO3
54 {
55  namespace Core
56  {
57  // ** Clarification & Explanation **
58  //
59  // 4 class are declared in this file:
60  //
61  // - "SetOfMap" : This class contain all the SetOf. This class is a singleton, just one SetOfMap per type is allowed.
62  // To access a SetOf you need his string's key. Example SetOfMap<Bond>::get().get_setof("UpperWall").
63  // You can access the map container by the map() method.
64  //
65  // - "SetOfBase" : This class is the base class of SetOf. contain a lot of Usefull method
66  //
67  // - "SetOf" : Child class of "SetOfBase". When this class is instanciated, it is automatically registered in the SetOfMap with its strKey.
68  // A Special Instance of SetOf is the static _globalset. Thanks to Register item are automatically registered
69  // in this SetOf. This SetOf has always "Global" for string's id.
70  // Sometimes users need just simple container without registration managing. So use it, but be carefull !
71  //
72  // - "Register" : An interface class between the SetOf<type>, globalSetOf<type> and the <type>.
73  // "Register" manage the registering and the de-registering of the object in SetOf<type> and globalSetOf<type>.
74  //
75  // - "ExtendedSetOf" : SetOf own a ExtendedSetOf. The ExtendedSetOf class is only specialized class that can do special treatments on kind
76  // kind of SetOf<T>. You can ask to tools some specific treamtments for discrete elements etc...
77  //
78  //
79  // **comment**
80  //
81  // - Easy access method are implemented in IO::Domain class
82 
83 
84  //--------------------------------//
85  // SETOFMAP //
86  //--------------------------------//
87  //this class is a singleton, only one map per type is allowed
88  template<typename type> class SetOf;
89 
90  template<typename type>
91  class SetOfMap : public SetOfMapBase, public Core::Singleton<SetOfMap<type> >
92  {
93  GRANOO_SINGLETON_CLASS(SetOfMap);
94  friend class SetOf<type>;
95 
96  public:
97  typedef std::map<const std::string, SetOf<type>*> mapType;
98 
99  public:
100  void clear();
102  SetOf<type>& get_setof(const std::string&);
103  void change_key(const std::string& oldKey, SetOf<type>& set);
104  bool exist(const std::string&) const;
105  size_t size() const;
107  const SetOf<type>& first_setof() const;
109  void save_it(boost::archive::text_oarchive&, const unsigned int);
110  void load_it(boost::archive::text_iarchive&, const unsigned int);
111  void record(boost::archive::text_oarchive&);
112  void record(boost::archive::text_iarchive&);
113 
114  std::ostream& write_ascii(std::ostream& out) const;
115  std::istream& read_ascii(std::istream& in);
116 
118  size_t item_number() const;
119  void to_vtk(vtkUnstructuredGrid*);
120  std::string info() const;
121 
122  std::vector<SetOfGeneric*> get_all_setofgeneric();
123  SetOfGeneric& get_setofgeneric(const std::string& setID);
124  bool is_set_exist(const std::string& setID) const;
125 
126  private:
130  virtual ~SetOfMap();
131 
134 
135  //Boost serialization
137  template<class Archive> void save(Archive&, const unsigned int ) const;
138  template<class Archive> void load(Archive&, const unsigned int);
140 
141  private:
144 
145 
146  };
147 
148  //--------------------------------//
149  // SETOFBASE //
150  //--------------------------------//
151  template<typename type>
152  class SetOfBase : public ExtendedSetOf<type>
153  {
154  friend class SetOfMap<type>;
155  friend class ExtendedSetOf<type>;
156 
157  public:
158  // Constructors and destructor
161  virtual ~SetOfBase();
162 
163  // container size and rank of item
164  size_t size() const;
165  size_t Size() const;
166  size_t get_rank(const type* item) const;
167  size_t get_rank(const type& item) const;
168  bool is_empty() const;
169 
170  // Accessing an item
171  type* get_item_by_rank(size_t i);
172  type& get_item(size_t i);
173  type* operator[](size_t i);
174  type& operator()(size_t i);
175  const type* get_item_by_rank(size_t i) const;
176  const type& get_item(size_t i) const;
177  const type* operator[](size_t i) const;
178  const type& operator()(size_t i) const;
179 
180  type& first();
181  const type& first() const;
182 
183  type& last();
184  const type& last() const;
185 
186  // Add and erase shortcut
187  void add(type& item);
188  void erase(type& item);
189 
190  const std::vector<type *>& get_vector() const;
191 
192  // Adding items
193  virtual void add_item(type* item, bool check = true);
194  virtual void add_item(type& item, bool check = true);
195  virtual void add_item(std::vector<type *>& vector, bool check = true);
196  virtual void add_item(SetOfBase<type>&, bool check = true);
197 
198  // Removing items
199  virtual void erase_item(type* item);
200  virtual void erase_item(type& item);
201  virtual void clear();
202  virtual void clear_and_delete();
203  virtual void clear_and_delete_with_progress(const std::string&);
204 
205  // Check if an item is contained
206  bool contain(const type* item) const;
207  bool contain(const type& item) const;
208 
209  typename std::vector<type *>::iterator begin();
210  typename std::vector<type *>::iterator end();
211 
212  typename std::vector<type *>::const_iterator begin() const;
213  typename std::vector<type *>::const_iterator end() const;
214 
215  protected:
216  std::vector<type *> _item_vector;
217 
218  private:
220  template<class Archive> void save(Archive& ar, const unsigned int version) const;
221  template<class Archive> void load(Archive& ar, const unsigned int version);
223  };
224 
225  //--------------------------------//
226  // SETOF //
227  //--------------------------------//
228  template<class type> class Register;
229  class Domain;
230 
231  template < typename T>
232  struct type_wrapper {typedef T type_;};
233 
234  template<typename type>
235  class SetOf : public SetOfBase<type>, public SetOfGeneric
236  {
237 
238  friend class Domain;
239  friend class SetOfMap<type>;
240  friend struct type_wrapper<type>;
241  friend struct type_wrapper<Register<type> >;
242 
243  public:
244  // Usefull for internal use only, NOT DOCUMENTED !
245  static std::string class_ID() { return std::string("Core::SetOf<" + type::class_ID()+">");}
246  static std::string get_global_str_key() {return std::string ("Global");}
247 
248  // Static usefull methods
250  static SetOf<type>& get_setof_by_ID(const std::string& id);
251  static SetOf<type>* get_setof_ptr_by_ID(const std::string& id);
252  static SetOf<type>& get();
253  static SetOf<type>& get(const std::string& id);
254  static SetOf<type>& pull(const std::string& id); //It build a new SetOf if it does not exist
255  static bool exist(const std::string& id);
256  static type& get_item_by_base_class_rank(size_t i);
257  static void* get_item_by_base_class_rank(size_t i, size_t classID);
259  static type& first_item();
260 
261  public:
262  // Constructors and destructor
263  SetOf(const std::string & id);
264  SetOf(SetOfBase<type>& set, const std::string& id);
265  virtual ~SetOf();
266 
267  // Usefull for internal use only, NOT DOCUMENTED !
268  std::string get_type_key() const;
269 
270  // Id management
271  void set_ID(const std::string& newId);
272  bool is_global_set() const;
273 
274  // Adding item
275  virtual void add_item(type *, bool check = true);
276  virtual void add_item(type &, bool check = true);
277  virtual void add_item_if_not_indexed(type &);
279  virtual void add_item(std::vector<type *> &, bool check = true);
280  virtual void add_item(SetOfBase<type>&, bool check = true);
281 
282  // Removing item
283  virtual void erase_item(type *);
284  virtual void erase_item(type &);
285  virtual void erase_item_if_indexed(type &);
287  void clear_and_delete_with_progress(const std::string&);
288  void clear();
289  std::vector<Base*> get_allbase_item();
290 
291  // Xml
292  std::string info() const;
293  std::string type_ID() const;
294 
295  // Experimental for SmartSetOf
296  virtual void Debug() const {};
297  void add_item_in_class_hierarchy(type *, SetOfGeneric*); // Add in all subclass set
298  void erase_item_in_class_hierarchy(type *, SetOfGeneric*); // erase in all subclass set
299 
300  std::ostream& write_ascii(std::ostream& out) const;
301  std::istream& read_ascii(std::istream& in);
302  void add_this_item(type *, bool check = true); // For just adding item in the set
303  void erase_this_item(type *); // For just removing item in the set
304 
305 
306  // Operation
307  void make_union(SetOf<type>& set1, SetOf<type>& set2 );
310  bool is_equal(SetOf<type>& set);
312  bool include(SetOf<type>& set);
313  unsigned int common_item(SetOf<type>& set);
314 
315  // Manage signal
318 
319  protected:
320  SetOf(); // Default constructor is required for boost serialization
321 
322  private:
324  static void add_to_map(SetOf< type >&);
326 
328 
329  //Boost serialization
331  template<class Archive> void save(Archive&, const unsigned int ) const;
332  template<class Archive> void load(Archive&, const unsigned int);
334 
335  private:
339  };
340 
341  //--------------------------------//
342  // REGISTER //
343  //--------------------------------//
344  template<class type>
345  class Register
346  {
347 
348  friend class SetOf<type>;
349 
350  public:
351  // Constructors and destructor
353  virtual ~Register();
354 
355  // Usefull for internal use only, NOT DOCUMENTED !
357 
358  // SetOf management
359  bool belong_to_setof(const std::string& setOfId) const;
360  bool belong_to_setof(const SetOf<type>& set) const;
361  std::list< SetOf<type>* > & get_setof_list();
362 
363  // get_ a unique numeric id
364  unsigned long long int get_numeric_ID() const;
365 
366  private:
370 
371  private:
372  static unsigned long long int _numericID_counter;
373 
374  private:
376  std::list< SetOf<type>* > _setof_list;
377  const unsigned long long int _numericID;
378  };
379 
380 
381 
382  }
383 }
384 
385 #include "GranOO3/Core/SetOfMap.tpp"
386 #include "GranOO3/Core/SetOfBase.tpp"
387 #include "GranOO3/Core/SetOf.tpp"
388 #include "GranOO3/Core/Register.tpp"
389 
390 
391 #endif
Definition: Domain.hpp:46
Definition: ExtendedSetOf.hpp:44
Definition: SetOf.hpp:346
const unsigned long long int _numericID
Definition: SetOf.hpp:377
bool belong_to_setof(const std::string &setOfId) const
bool already_exist(SetOf< type > &)
bool belong_to_setof(const SetOf< type > &set) const
static unsigned long long int _numericID_counter
Definition: SetOf.hpp:372
std::list< SetOf< type > * > _setof_list
Definition: SetOf.hpp:376
void erase_setof(SetOf< type > &)
void add_setof(SetOf< type > &)
std::list< SetOf< type > * > & get_setof_list()
SetOf< type > & _globalset
Definition: SetOf.hpp:375
unsigned long long int get_numeric_ID() const
Definition: SetOf.hpp:153
virtual void clear_and_delete()
virtual void add_item(std::vector< type * > &vector, bool check=true)
bool contain(const type &item) const
std::vector< type * >::iterator begin()
void add(type &item)
std::vector< type * >::iterator end()
const type * operator[](size_t i) const
size_t get_rank(const type *item) const
virtual void erase_item(type &item)
virtual void clear_and_delete_with_progress(const std::string &)
std::vector< type * >::const_iterator end() const
SetOfBase(const SetOfBase< type > &set)
const type * get_item_by_rank(size_t i) const
virtual void erase_item(type *item)
void save(Archive &ar, const unsigned int version) const
const type & last() const
bool contain(const type *item) const
const std::vector< type * > & get_vector() const
type * operator[](size_t i)
void erase(type &item)
virtual void add_item(type &item, bool check=true)
type * get_item_by_rank(size_t i)
const type & first() const
virtual void add_item(SetOfBase< type > &, bool check=true)
size_t get_rank(const type &item) const
type & operator()(size_t i)
std::vector< type * >::const_iterator begin() const
virtual void add_item(type *item, bool check=true)
type & get_item(size_t i)
void load(Archive &ar, const unsigned int version)
std::vector< type * > _item_vector
Definition: SetOf.hpp:216
friend class boost::serialization::access
Definition: SetOf.hpp:219
const type & get_item(size_t i) const
const type & operator()(size_t i) const
Definition: SetOfGeneric.hpp:46
Definition: SetOf.hpp:236
bool is_global_set() const
void add_item_in_class_hierarchy(type *, SetOfGeneric *)
static SetOf< type > & pull(const std::string &id)
std::istream & read_ascii(std::istream &in)
SetOf(const std::string &id)
static type & first_item()
SetOf< typename type::base > * _childset
Definition: SetOf.hpp:336
void erase_item_in_class_hierarchy(type *, SetOfGeneric *)
void make_difference(SetOf< type > &set1, SetOf< type > &set2)
std::string info() const
std::ostream & write_ascii(std::ostream &out) const
virtual void add_item(std::vector< type * > &, bool check=true)
static void delete_from_map(SetOf< type > &)
void load(Archive &, const unsigned int)
static SetOf< type > & get(const std::string &id)
static std::string get_global_str_key()
Definition: SetOf.hpp:246
static SetOf< type > & get()
Signal< type & > & get_signal_erase_item()
void make_intersection(SetOf< type > &set1, SetOf< type > &set2)
static SetOf< type > & get_global_set()
virtual void erase_item(type *)
bool has_intersection(SetOf< type > &set)
static SetOf< type > * _globalset
Definition: SetOf.hpp:323
std::string get_type_key() const
std::vector< Base * > get_allbase_item()
virtual void Debug() const
Definition: SetOf.hpp:296
void erase_this_item(type *)
static type & get_item_by_base_class_rank(size_t i)
static SetOf< type > & get_setof_by_ID(const std::string &id)
void set_ID(const std::string &newId)
static std::string class_ID()
Definition: SetOf.hpp:245
virtual void add_item_if_not_indexed(SetOf< type > &)
Signal< type & > _signal_add
Definition: SetOf.hpp:337
Signal< type & > & get_signal_add_item()
virtual void add_item(type &, bool check=true)
virtual void erase_item(type &)
SetOf(SetOfBase< type > &set, const std::string &id)
std::string type_ID() const
SetOf< typename type::base > * get_base_class_set(SetOfGeneric *)
void clear_and_delete_with_progress(const std::string &)
Signal< type & > _signal_erase
Definition: SetOf.hpp:338
unsigned int common_item(SetOf< type > &set)
void save(Archive &, const unsigned int) const
void make_union(SetOf< type > &set1, SetOf< type > &set2)
static SetOf< type > & get_safe(SetOfGeneric *parent)
bool is_equal(SetOf< type > &set)
virtual void add_item(SetOfBase< type > &, bool check=true)
virtual void add_item_if_not_indexed(type &)
virtual void erase_item_if_indexed(type &)
static void * get_item_by_base_class_rank(size_t i, size_t classID)
friend class boost::serialization::access
Definition: SetOf.hpp:330
bool include(SetOf< type > &set)
static SetOf< type > * get_setof_ptr_by_ID(const std::string &id)
static bool exist(const std::string &id)
static void add_to_map(SetOf< type > &)
virtual void add_item(type *, bool check=true)
void add_this_item(type *, bool check=true)
Definition: SetOfMapBase.hpp:50
Definition: SetOf.hpp:92
SetOfMap< type >::mapType _map
Definition: SetOf.hpp:142
void load_it(boost::archive::text_iarchive &, const unsigned int)
std::istream & read_ascii(std::istream &in)
void save_it(boost::archive::text_oarchive &, const unsigned int)
std::ostream & write_ascii(std::ostream &out) const
bool exist(const std::string &) const
void erase_setof(SetOf< type > &)
std::map< const std::string, SetOf< type > * > mapType
Definition: SetOf.hpp:97
std::string info() const
void change_key(const std::string &oldKey, SetOf< type > &set)
std::vector< SetOfGeneric * > get_all_setofgeneric()
SetOfMap(const SetOf< type > &)
void record(boost::archive::text_oarchive &)
void add_setof(SetOf< type > &)
SetOfGeneric & get_setofgeneric(const std::string &setID)
void record(boost::archive::text_iarchive &)
size_t item_number() const
bool is_set_exist(const std::string &setID) const
SetOf< type > & first_setof()
friend class boost::serialization::access
Definition: SetOf.hpp:136
void save(Archive &, const unsigned int) const
static SetOfMap< type > * _the_setof_map
Definition: SetOf.hpp:143
const SetOf< type > & first_setof() const
SetOf< type > & get_setof(const std::string &)
void to_vtk(vtkUnstructuredGrid *)
SetOfMap< type > & operator=(const SetOfMap< type > &)
void load(Archive &, const unsigned int)
Definition: Singleton.hpp:75
Definition: Common.hpp:198
static char_cptr version
Definition: Exprtk.hpp:44221
Definition: SetOf.hpp:232
T type_
Definition: SetOf.hpp:232