GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
XmlObjectManager.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_XmlObjectManager_hpp_
31 #define _GranOO_libCore_XmlObjectManager_hpp_
32 
33 #include <string>
34 #include <iterator>
35 #include <map>
36 #include <iostream>
37 #include <typeinfo>
38 
39 #include "GranOO3/Core/TinyXml.hpp"
41 #include "GranOO3/Core/String.hpp"
42 
43 #define GRANOO_XML_READ_SHAPE(CLASS) \
44  CLASS* \
45  CLASS::new_object(const TiXmlElement* el) \
46  { \
47  Core::XmlParser& parser = Core::XmlParser::get(); \
48  const std::string id = parser.read_attribute<std::string>(Core::Attribute::GRANOO_OPTIONAL, "ID"); \
49  \
50  parser.CheckAllowedAttributeValue("Type", Core::ObjectFactory< CLASS >::get_ID()); \
51  const std::string shapeType = parser.read_attribute<std::string>(Attr::GRANOO_REQUIRED, "Type"); \
52  CLASS* mf = Core::ObjectFactory< CLASS >::new_object(shapeType); \
53  const_cast<std::string&>(mf->_ID) = id; \
54  mf->read_xml_element(parser.get_current_element()); \
55  return mf; \
56  } \
57  \
58  void \
59  CLASS::read_xml_element(const TiXmlElement* el) \
60  { \
61  Core::XmlParser& parser = Core::XmlParser::get(); \
62  parser.set_current_element(el); \
63  \
64  if (parser.attribute_exist("FrameID")) \
65  { \
66  parser.CheckAllowedAttributeValue("FrameID", Core::XmlObjectManager< Geom::Frame >::get_ID()); \
67  const std::string frameId = parser.read_attribute<std::string>("FrameID", ""); \
68  Geom::Frame* f = Core::XmlObjectManager< Geom::Frame >::get_item(frameId); \
69  set_center(f->get_center()); \
70  set_quaternion(f->get_quaternion()); \
71  } \
72  } \
73  \
74  CLASS& \
75  CLASS::glob(const std::string& id) \
76  { \
77  return Core::XmlObjectManager<CLASS>::get(id); \
78  }
79 
80 
81 
82 namespace GranOO3
83 {
84  namespace Core
85  {
86  // The definition of 'XmlObjectManager' has moved in the XmlParser.hpp
87  // file for circular dependencies problem of headers
88 
89 
90  template<typename T>
91  std::map<const std::string, T *> XmlObjectManager<T>::map_;
92 
93  template<typename T> std::string
95  std::string list = "";
96  for (auto& it : get_ID())
97  list += it + " ";
98  return list;
99  }
100 
101  template<typename T> void
102  XmlObjectManager<T>::add_item(const std::string & key, T * item) {
103  UserAssert(map_.count(key) == 0,
104  "XmlObjectManager<" + T::class_ID() + ">. Can't add item, the id=" + Core::String::quote(key) +
105  " is already used");
106  map_[key] = item;
107  }
108 
109  template<typename T> bool
110  XmlObjectManager<T>::exist(const std::string & key) {
111  return (map_.count(key) == 1);
112  }
113 
114  template<typename T> T&
115  XmlObjectManager<T>::get(const std::string & key) {
116  SafeModeAssert(map_.count(key) == 1,
117  "XmlObjectManager<" + T::class_ID() + ">. Can't get item, the id=" + Core::String::quote(key) +
118  " is unknown, please use instead the following id " +
119  get_list());
120  return *map_[key];
121  }
122 
123  template<typename T> T *
124  XmlObjectManager<T>::get_item(const std::string & key) {
125  if (map_.count(key) == 0)
126  return 0;
127 
128 
129  return map_[key];
130  }
131 
132  template<typename T> void
134  map_.clear();
135  }
136 
137  template<typename T> std::string
138  XmlObjectManager<T>::get_ID (const T* item) {
139  typename std::map<const std::string, T*>::iterator it;
140  for ( it = map_.begin(); it != map_.end(); it++ ) {
141  if ((*it).second==item)
142  return (*it).first;
143  }
145  }
146 
147  template<typename T> void
149  typename std::map<const std::string, T*>::iterator it;
150  for ( it = map_.begin(); it != map_.end(); it++ ) {
151  if ((*it).second==item)
152  map_.erase(it);
153  }
154 
155 
156  UserAssert(0, "can't erase the required item");
157  }
158 
159  template<typename T> T*
160  XmlObjectManager<T>::read_element(const TiXmlElement* el, bool ID_required) {
162  parser.set_current_element(el);
163 
164  std::string id = "";
165 
166  if (ID_required)
167  parser.read_attribute<std::string>(Attribute::GRANOO_REQUIRED, "ID", id);
168  else
169  parser.read_attribute<std::string>(Attribute::GRANOO_OPTIONAL, "ID", id);
170 
171  T* item = T::new_object(el);
172 
173  if (id != "") {
174  parser.XmlAssert(map_.count(id) == 0, "The id " + Core::String::quote(id) + " must be unique");
175  map_[id] = item;
176  map_[id]->add_glob(id);
177  }
178 
179  // Display some info
180  granoo::cout << "--> instanciate new " << String::quote(T::class_ID());
181  if (id != "")
182  granoo::cout << " with ID=" << String::quote(id);
184 
185  return item;
186 
187  }
188 
189  template<typename T> std::vector<std::string>
191  typedef std::map<const std::string, T *> MAP;
192 
193  std::vector<std::string> vec;
194  for (typename MAP::iterator it = map_.begin(); it!=map_.end(); ++it)
195  vec.push_back(it->first);
196 
197  return vec;
198  }
199 
200  }
201 
202 }
203 
204 
205 
206 
207 
208 #endif // _GranOO_libCore_XmlObjectManager_hpp_
#define SafeModeAssert(condition, message)
Definition: Macro.hpp:47
#define UserAssert(condition, message)
Definition: Macro.hpp:54
static void add_item(const std::string &, T *)
Definition: XmlObjectManager.hpp:102
static T * read_element(const TiXmlElement *, bool ID_required=true)
Definition: XmlObjectManager.hpp:160
static bool exist(const std::string &)
Definition: XmlObjectManager.hpp:110
static void clear()
Definition: XmlObjectManager.hpp:133
static std::vector< std::string > get_ID()
Definition: XmlObjectManager.hpp:190
static void erase_item(const T *)
Definition: XmlObjectManager.hpp:148
static T & get(const std::string &)
Definition: XmlObjectManager.hpp:115
static std::string get_list()
Definition: XmlObjectManager.hpp:94
static std::map< const std::string, T * > map_
Definition: XmlParser.hpp:100
static T * get_item(const std::string &)
Definition: XmlObjectManager.hpp:124
Definition: XmlParser.hpp:122
static XmlParser & get()
void read_attribute(const Attribute::State, const std::string &, T &)
void set_current_element(const TiXmlElement *)
static granoo_endl endl
Definition: Out.hpp:106
static GranOO3::Core::Out cout
Definition: Out.hpp:100
Definition: Common.hpp:198
static std::string quote(const std::string &str)
Definition: String.cpp:73
static const std::string empty_string
Definition: String.hpp:45