GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Mapped.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 _GranOO_libCore_Mapped_hpp_
31 #define _GranOO_libCore_Mapped_hpp_
32 
33 #include <string>
34 #include <iterator>
35 #include <map>
36 #include <boost/archive/text_oarchive.hpp>
37 #include <boost/archive/text_iarchive.hpp>
38 
39 
40 #include "GranOO3/Core/Macro.hpp"
41 #include "GranOO3/Core/String.hpp"
42 
43 
44 #define GRANOO_MAPPED_GET(TYPE) \
45 public: \
46  static TYPE& get(const std::string& id) {return Core::Mapped<TYPE >::get(id);} \
47  static const TYPE& get_const(const std::string& id) {return Core::Mapped<TYPE >::get_const(id);} \
48  static bool exist(const std::string& id) {return Core::Mapped<TYPE >::exist(id);}
49 
50 namespace GranOO3
51 {
52  namespace Core
53  {
54  template<class T>
55  class Mapped
56  {
57  public:
58  typedef std::map<const std::string, T* > Map;
59  static T& get(const std::string& id);
60  static const T& get_const(const std::string& id);
61  static bool exist(const std::string& id);
62  static Map& get_map();
63 
64  public:
65  const std::string& get_ID() const;
66  void set_ID(const std::string&);
67 
68  protected:
69  Mapped(const std::string& id);
70  virtual ~Mapped();
71  Mapped();
72 
73  private:
74  //SERIALIZATION
76  template<class Archive> void save(Archive&, const unsigned int ) const;
77  template<class Archive> void load(Archive&, const unsigned int);
79 
80  private:
81  std::string _ID;
82 
83  };
84 
85  template<typename T>
86  typename Mapped<T>::Map&
88  static Map* mymap = 0;
89  if (mymap == 0)
90  mymap = new Map();
91  return *mymap;
92  }
93 
94  template<typename T>
95 
96  Mapped<T>::Mapped(const std::string& id)
97  : _ID(id) {
98  T* me = static_cast<T*>(this);
99  SafeModeAssert(get_map().count(_ID) == 0,
100  "The " + String::quote(_ID) + " for " + String::quote(T::class_ID()) + " is already registered");
101 
102  get_map()[_ID] = me;
103  }
104 
105  template<typename T>
106 
108  : _ID("") {
109  }
110 
111  template<typename T>
112 
114  SafeModeAssert(get_map().count(_ID) == 1,
115  "Can't delete the " + String::quote(_ID) + " item for " + String::quote(T::class_ID()) + ". It is not registered.");
116  get_map().erase(_ID);
117  }
118 
119  template<typename T>
120  T&
121  Mapped<T>::get(const std::string& id) {
122  SafeModeAssert(get_map().count(id) == 1,
123  "Can't grep the " + String::quote(id) + " item for " + String::quote(T::class_ID()) + ". It is not registered.");
124  return *get_map()[id];
125  }
126 
127  template<typename T>
128  const T&
129  Mapped<T>::get_const(const std::string& id) {
130  SafeModeAssert(get_map().count(id) == 1,
131  "Can't grep the " + String::quote(id) + " item for " + String::quote(T::class_ID()) + ". It is not registered.");
132  return *get_map()[id];
133  }
134 
135  template<typename T>
136  bool
137  Mapped<T>::exist(const std::string& id) {
138  return (get_map().count(id) == 1);
139  }
140 
141  template<typename T>
142  const std::string&
144  return _ID;
145  }
146 
147  template<typename T>
148  void
149  Mapped<T>::set_ID(const std::string& id) {
150 
151  SafeModeAssert(get_map().count(_ID) == 1,
152  "The "+String::quote(_ID)+" item for "+String::quote(T::class_ID())+ " is not registered.");
153  get_map().erase (_ID);
154  SafeModeAssert(get_map().count(_ID) == 0,
155  "The "+String::quote(_ID)+" for "+String::quote(T::class_ID())+" is already registered");
156  _ID = id;
157  get_map()[_ID] = this;
158  }
159 
160  template<typename T>
161  template<class Archive>
162  void
163  Mapped<T>::save(Archive& ar, const unsigned int) const {
164  ar << _ID;
165  }
166 
167  template<typename T>
168  template<class Archive>
169  void
170  Mapped<T>::load(Archive& ar, const unsigned int) {
171  ar >> _ID;
172  T* me = static_cast<T*>(this);
173  SafeModeAssert(get_map().count(_ID) == 0,
174  "The " + String::quote(_ID) + " for " + String::quote(T::class_ID()) + " is already registered");
175 
176  get_map()[_ID] = me;
177  }
178 
179  }
180 
181 }
182 
183 
184 
185 
186 
187 #endif // _GranOO_libCore_Mapped_hpp_
#define SafeModeAssert(condition, message)
Definition: Macro.hpp:47
Definition: Mapped.hpp:56
static const T & get_const(const std::string &id)
Definition: Mapped.hpp:129
const std::string & get_ID() const
Definition: Mapped.hpp:143
void set_ID(const std::string &)
Definition: Mapped.hpp:149
std::map< const std::string, T * > Map
Definition: Mapped.hpp:58
virtual ~Mapped()
Definition: Mapped.hpp:113
void load(Archive &, const unsigned int)
Definition: Mapped.hpp:170
static Map & get_map()
Definition: Mapped.hpp:87
static bool exist(const std::string &id)
Definition: Mapped.hpp:137
static T & get(const std::string &id)
Definition: Mapped.hpp:121
friend class boost::serialization::access
Definition: Mapped.hpp:75
void save(Archive &, const unsigned int) const
Definition: Mapped.hpp:163
Mapped(const std::string &id)
Definition: Mapped.hpp:96
std::string _ID
Definition: Mapped.hpp:81
Mapped()
Definition: Mapped.hpp:107
Definition: Common.hpp:198
static std::string quote(const std::string &str)
Definition: String.cpp:73