VPTissue Reference Manual
AttributeStore.h
Go to the documentation of this file.
1 #ifndef ATTRIBUTE_STORE_H_INCLUDED
2 #define ATTRIBUTE_STORE_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 // WARNING: FILE ALSO GETS PROCESSED BY SWIG. SWIG (VERSIONS EARLY 2013) //
24 // DOES NOT ACCEPT TWO CONSECUTIVE >> TEMPLATE TYPE PARAMETERLIST ENDING //
25 // BRACKETS WITHOUT INTERVENING BLANK (EVEN THOUGH C++11 DOES ACCPT THIS) //
27 
28 #include <boost/property_tree/ptree.hpp>
29 #include <algorithm>
30 #include <cstddef>
31 #include <functional>
32 #include <map>
33 #include <stdexcept>
34 #include <string>
35 #include <tuple>
36 #include <vector>
37 
38 namespace SimPT_Sim {
39 
44 {
45 public:
49  AttributeStore() = default;
50 
83  AttributeStore(const boost::property_tree::ptree& pt);
84 
89  template<typename T>
90  std::vector<T>& Container(const std::string& name);
91 
96  template<typename T>
97  const std::vector<T>& Container(const std::string& name) const;
98 
106  boost::property_tree::ptree GetAttributeValues(std::size_t pos) const;
107 
112  template<typename T>
113  T GetDefaultValue(const std::string& name) const;
114 
118  template<typename T>
119  std::vector<std::string> GetAttributeNames() const;
120 
126  boost::property_tree::ptree GetIndexPtree() const;
127 
133  boost::property_tree::ptree GetIndexDump() const;
134 
142  template<typename T>
143  void Initialize(const std::string& name, const std::vector<T>& values);
144 
152  template<typename T>
153  void Initialize(const std::string& name, std::vector<T>&& values);
154 
158  template<typename T>
159  bool IsAttribute(const std::string& name) const;
160 
164  bool IsEmpty() const;
165 
172  std::tuple<bool, std::size_t> IsStrictSizeConsistent() const;
173 
177  bool IsWeakSizeConsistent(std::size_t N) const;
178 
179 private:
183  template<typename T>
184  bool SizeCheckStrict(std::size_t N) const;
185 
189  template<typename T>
190  bool SizeCheckWeak(std::size_t N) const;
191 
192 private:
193  template<typename T>
194  using MapType = std::map<std::string, std::pair<std::vector<T>, T> >;
195 
199  template<typename T>
200  MapType<T>& IndexMap();
201 
205  template<typename T>
206  const MapType<T>& IndexMap() const;
207 
208  MapType<bool> m_map_b;
209  MapType<int> m_map_i;
210  MapType<double> m_map_d;
211  MapType<std::string> m_map_s;
212  boost::property_tree::ptree m_ptree;
213 };
214 
218 std::ostream& operator<<(std::ostream& os, const AttributeStore& a);
219 
221 // IMPLEMENTATION OF PRIVATE ACCESS METHODS (need to be seen by compiler prior to first use).
223 
224 template<>
225 inline AttributeStore::MapType<bool>& AttributeStore::IndexMap<bool>()
226 {
227  return m_map_b;
228 }
229 
230 
231 template<>
232 inline const AttributeStore::MapType<bool>& AttributeStore::IndexMap<bool>() const
233 {
234  return m_map_b;
235 }
236 
237 
238 template<>
239 inline AttributeStore::MapType<int>& AttributeStore::IndexMap<int>()
240 {
241  return m_map_i;
242 }
243 
244 
245 template<>
246 inline const AttributeStore::MapType<int>& AttributeStore::IndexMap<int>() const
247 {
248  return m_map_i;
249 }
250 
251 
252 template<>
253 inline AttributeStore::MapType<double>& AttributeStore::IndexMap<double>()
254 {
255  return m_map_d;
256 }
257 
258 
259 template<>
260 inline const AttributeStore::MapType<double>& AttributeStore::IndexMap<double>() const
261 {
262  return m_map_d;
263 }
264 
265 
266 template<>
267 inline AttributeStore::MapType<std::string>& AttributeStore::IndexMap<std::string>()
268 {
269  return m_map_s;
270 }
271 
272 
273 template<>
274 inline const AttributeStore::MapType<std::string>& AttributeStore::IndexMap<std::string>() const
275 {
276  return m_map_s;
277 }
278 
280 // IMPLEMENTATION OF PUBLIC METHODS
282 
283 template<typename T>
284 inline std::vector<T>& AttributeStore::Container(const std::string& name)
285 {
286  const auto c = IndexMap<T>().find(name);
287  if (c == IndexMap<T>().end()) {
288  throw std::runtime_error("AttributeStore::Attributes(const std::string& name): name not in index: " + name);
289  }
290  return c->second.first;
291 }
292 
293 
294 template<typename T>
295 inline const std::vector<T>& AttributeStore::Container(const std::string& name) const
296 {
297  const auto c = IndexMap<T>().find(name);
298  if (c == IndexMap<T>().end()) {
299  throw std::runtime_error("AttributeStore::Attributes(const std::string& name): name not in index: " + name);
300  }
301  return c->second.first;
302 }
303 
304 
305 template<typename T>
306 inline std::vector<std::string> AttributeStore::GetAttributeNames() const
307 {
308  std::vector<std::string> names;
309  for (const auto& c : IndexMap<T>()) {
310  names.push_back(c.first);
311  }
312  return names;
313 }
314 
315 inline boost::property_tree::ptree AttributeStore::GetIndexPtree() const
316 {
317  return m_ptree;
318 }
319 
320 
321 template<typename T>
322 inline T AttributeStore::GetDefaultValue(const std::string& name) const
323 {
324  const auto c = IndexMap<T>().find(name);
325  if (c == IndexMap<T>().end()) {
326  throw std::runtime_error("AttributeStore::GetDefaultValue(const std::string& name): name not in index: " + name);
327  }
328  return c->second.second;
329 }
330 
331 
332 template<typename T>
333 inline void AttributeStore::Initialize(const std::string& name, const std::vector<T>& values)
334 {
335  if ( IsWeakSizeConsistent(values.size()) ) {
336  Container<T>(name) = values;
337  } else {
338  throw std::runtime_error("AttributeStore::Initialize: name not in index or size inconsistent " + name);
339  }
340 }
341 
342 
343 template<typename T>
344 inline void AttributeStore::Initialize(const std::string& name, std::vector<T>&& values)
345 {
346  if ( IsWeakSizeConsistent(values.size()) ) {
347  Container<T>(name) = std::move(values);
348  } else {
349  throw std::runtime_error("AttributeStore::Initialize: name not in index or size inconsistent " + name);
350  }
351 }
352 
353 
354 template<typename T>
355 inline bool AttributeStore::IsAttribute(const std::string& name) const
356 {
357  return IndexMap<T>().find(name) != IndexMap<T>().end();
358 }
359 
360 
361 inline std::tuple<bool, std::size_t> AttributeStore::IsStrictSizeConsistent() const
362 {
363  bool status = true;
364  size_t s = 0;
365 
366  if (!IndexMap<bool>().empty()) {
367  s = IndexMap<bool>().cbegin()->second.first.size();
368  status = SizeCheckStrict<bool>(s) && SizeCheckStrict<int>(s)
369  && SizeCheckStrict<double>(s) && SizeCheckStrict<std::string>(s);
370  } else if (!IndexMap<int>().empty()) {
371  s = IndexMap<int>().cbegin()->second.first.size();
372  status = SizeCheckStrict<int>(s) && SizeCheckStrict<double>(s) && SizeCheckStrict<std::string>(s);
373  } else if (!IndexMap<double>().empty()) {
374  s = IndexMap<int>().cbegin()->second.first.size();
375  status = SizeCheckStrict<double>(s) && SizeCheckStrict<std::string>(s);
376  } else if (!IndexMap<std::string>().empty()){
377  s = IndexMap<int>().cbegin()->second.first.size();
378  status = SizeCheckStrict<std::string>(s);
379  }
380 
381  return std::make_tuple(status, s);
382 }
383 
384 
385 inline bool AttributeStore::IsWeakSizeConsistent(std::size_t N) const
386 {
387  // Short circuit avoids redundant evaluations.
388  return SizeCheckWeak<int>(N) && SizeCheckWeak<double>(N) && SizeCheckWeak<std::string>(N);
389 }
390 
392 // IMPLEMENTATION OF PRIVATE METHODS.
394 
395 template<typename T>
396 inline bool AttributeStore::SizeCheckStrict(std::size_t N) const
397 {
398  bool status = true;
399  for (const auto& e : IndexMap<T>()) {
400  const auto s = e.second.first.size();
401  if ( s != N ) {
402  status = false;
403  break;
404  }
405  }
406  return status;
407 }
408 
409 
410 template<typename T>
411 inline bool AttributeStore::SizeCheckWeak(std::size_t N) const
412 {
413  bool status = true;
414  for (const auto& e : IndexMap<T>()) {
415  const auto s = e.second.first.size();
416  if ( !( s == N || s == 0) ) {
417  status = false;
418  break;
419  }
420  }
421  return status;
422 }
423 
424 } // namespace
425 
426 #endif // include guard
427 
boost::property_tree::ptree GetIndexPtree() const
Return original attribute ptree the AttributeStore was created from.
boost::property_tree::ptree GetAttributeValues(std::size_t pos) const
Return all attribute values at position i in the container in property tree format.
std::vector< T > & Container(const std::string &name)
Reference to the appropriate (type and name) attribute container.
T GetDefaultValue(const std::string &name) const
Return default value for attribute of type and name.
Namespace for the core simulator.
AttributeStore()=default
Default constructor creates empty AttributeStore.
bool IsAttribute(const std::string &name) const
Returns true iff there is an attribute of type and name.
boost::property_tree::ptree GetIndexDump() const
Produces representation of index structure in a ptree descriptor.
std::tuple< bool, std::size_t > IsStrictSizeConsistent() const
Strict size consistent iff all containers have the same size.
Store and manage attributes.
bool IsWeakSizeConsistent(std::size_t N) const
Weak size consistent iff all containers with non-zero size have the same size.
void Initialize(const std::string &name, const std::vector< T > &values)
Set container of type and name to values iff that container is currently empty and values is size con...
std::vector< std::string > GetAttributeNames() const
Return a vector containing the names of attributes of type T.
bool IsEmpty() const
Returns true iff no attributes are defined.