GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
ObjectWithID.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Jean-luc CHARLES I2M-DuMAS/ENSAM Talence France
4 // <jean-luc.charles@ensam.eu>
5 // - Damien ANDRE SPCTS/ENS Ceramique industrielle, Limoges France
6 // <damien.andre@unilim.fr>
7 // - Jeremie GIRARDOT I2M-DuMAS/ENSAM Talence France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UVHC Valenciennes France
10 // <cedric.hubert@univ-valenciennes.fr>
11 // - Ivan IORDANOFF I2M-DuMAS-MPI/ENSAM Talence France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2016 JL. CHARLES, D. ANDRE, I. IORDANOFF, J. GIRARDOT
15 //
16 //
17 //
18 // This program is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program. If not, see <http://www.gnu.org/licenses/>.
30 
31 
32 #ifndef _CommonLibs_Util_ObjectWithID_HPP_
33 #define _CommonLibs_Util_ObjectWithID_HPP_
34 
35 #include <type_traits>
36 
37 #include "GranOO3/Core/Macro.hpp"
38 
39 
40 # define GRANOO_OBJECT_WITH_ID_CLASS(CLASS) \
41 private: \
42  friend class GranOO3::Core::ObjectWithID<CLASS>; \
43 public: \
44  static CLASS & get(const std::string& ID) { \
45  return GranOO3::Core::ObjectWithID<CLASS>::get(ID); \
46  } \
47  static const CLASS & get_const(const std::string& ID) { \
48  return GranOO3::Core::ObjectWithID<CLASS>::get(ID); \
49  } \
50  static bool exist(const std::string& ID) { \
51  return GranOO3::Core::ObjectWithID<CLASS>::exist(ID); \
52  }
53 
54 
55 namespace GranOO3
56 {
57  namespace Core
58  {
59 
60 
61  template <typename T>
63  {
64  public:
65  static T& get(const std::string& ID);
66  static bool exist(const std::string& ID);
67 
68  protected:
69  ObjectWithID();
70  ObjectWithID(const std::string& ID);
71  virtual ~ObjectWithID();
72 
73  public:
74  const std::string& get_uID() const;
75 
76  private:
78  template<class Archive> void save(Archive&, const unsigned int ) const;
79  template<class Archive> void load(Archive&, const unsigned int);
81 
82 
83  protected:
84  static std::map<const std::string, T*> _all;
85 
86  protected:
87  std::string _ID;
88 
89  };
90 
91  template<class T> std::map<const std::string, T*> ObjectWithID<T>::_all = std::map<const std::string, T*>()
92  ;
93 
94  template<class T>
95  T&
96  ObjectWithID<T>::get(const std::string& ID) {
97  AssertMsg(exist(ID), "The " + T::class_ID() + " object with '" + ID + "' does not exist");
98  return *_all[ID];
99  }
100 
101  template<class T>
102  bool
103  ObjectWithID<T>::exist(const std::string& ID) {
104  return (_all.count(ID) == 1);
105  }
106 
107  template<class T>
109  : _ID("") {
110  }
111 
112  template<class T>
113  ObjectWithID<T>::ObjectWithID(const std::string& ID)
114  : _ID(ID) {
115  AssertMsg(_all.count(_ID) == 0, "The " + T::class_ID() + " object with '" + ID + "' already exists");
116  _all[_ID] = static_cast<T*>(this);
117  }
118 
119  template<class T>
121  InternAssert(_all.count(_ID) == 1);
122  auto it = _all.find(_ID);
123  _all.erase(it);
124  }
125 
126  template<class T>
127  const std::string&
129  return _ID;
130  }
131 
132 
133  template<class T>
134  template<class Archive> void
135  ObjectWithID<T>::save(Archive& ar, const unsigned int) const {
136  ar << _ID;
137  }
138 
139  template<class T>
140  template<class Archive> void
141  ObjectWithID<T>::load(Archive& ar, const unsigned int) {
142  ar >> _ID;
143  auto& m = Core::SetOfManager::get();
144  _ID = m.get_SerializationPrefix() + _ID + m.get_SerializationSuffix();
145  InternAssert(_all.count(_ID) == 0);
146  _all[_ID] = static_cast<T*>(this);
147  }
148 
149  }
150 }
151 
152 #endif
#define InternAssert(condition)
Definition: Macro.hpp:81
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
Definition: ObjectWithID.hpp:63
static bool exist(const std::string &ID)
Definition: ObjectWithID.hpp:103
virtual ~ObjectWithID()
Definition: ObjectWithID.hpp:120
static T & get(const std::string &ID)
Definition: ObjectWithID.hpp:96
ObjectWithID()
Definition: ObjectWithID.hpp:108
std::string _ID
Definition: ObjectWithID.hpp:87
void save(Archive &, const unsigned int) const
Definition: ObjectWithID.hpp:135
friend class boost::serialization::access
Definition: ObjectWithID.hpp:77
static std::map< const std::string, T * > _all
Definition: ObjectWithID.hpp:84
void load(Archive &, const unsigned int)
Definition: ObjectWithID.hpp:141
const std::string & get_uID() const
Definition: ObjectWithID.hpp:128
static SetOfManager & get()
Definition: Singleton.hpp:127
Definition: Common.hpp:198