1 #ifndef ATTRIBUTE_STORE_H_INCLUDED
2 #define ATTRIBUTE_STORE_H_INCLUDED
28 #include <boost/property_tree/ptree.hpp>
90 std::vector<T>&
Container(
const std::string& name);
97 const std::vector<T>&
Container(
const std::string& name)
const;
143 void Initialize(
const std::string& name,
const std::vector<T>& values);
153 void Initialize(
const std::string& name, std::vector<T>&& values);
184 bool SizeCheckStrict(std::size_t N)
const;
190 bool SizeCheckWeak(std::size_t N)
const;
194 using MapType = std::map<std::string, std::pair<std::vector<T>, T> >;
200 MapType<T>& IndexMap();
206 const MapType<T>& IndexMap()
const;
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;
218 std::ostream& operator<<(std::ostream& os,
const AttributeStore& a);
225 inline AttributeStore::MapType<bool>& AttributeStore::IndexMap<bool>()
232 inline const AttributeStore::MapType<bool>& AttributeStore::IndexMap<bool>()
const
239 inline AttributeStore::MapType<int>& AttributeStore::IndexMap<int>()
246 inline const AttributeStore::MapType<int>& AttributeStore::IndexMap<int>()
const
253 inline AttributeStore::MapType<double>& AttributeStore::IndexMap<double>()
260 inline const AttributeStore::MapType<double>& AttributeStore::IndexMap<double>()
const
267 inline AttributeStore::MapType<std::string>& AttributeStore::IndexMap<std::string>()
274 inline const AttributeStore::MapType<std::string>& AttributeStore::IndexMap<std::string>()
const
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);
290 return c->second.first;
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);
301 return c->second.first;
308 std::vector<std::string> names;
309 for (
const auto& c : IndexMap<T>()) {
310 names.push_back(c.first);
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);
328 return c->second.second;
336 Container<T>(name) = values;
338 throw std::runtime_error(
"AttributeStore::Initialize: name not in index or size inconsistent " + name);
347 Container<T>(name) = std::move(values);
349 throw std::runtime_error(
"AttributeStore::Initialize: name not in index or size inconsistent " + name);
357 return IndexMap<T>().find(name) != IndexMap<T>().end();
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);
381 return std::make_tuple(status, s);
388 return SizeCheckWeak<int>(N) && SizeCheckWeak<double>(N) && SizeCheckWeak<std::string>(N);
396 inline bool AttributeStore::SizeCheckStrict(std::size_t N)
const
399 for (
const auto& e : IndexMap<T>()) {
400 const auto s = e.second.first.size();
411 inline bool AttributeStore::SizeCheckWeak(std::size_t N)
const
414 for (
const auto& e : IndexMap<T>()) {
415 const auto s = e.second.first.size();
416 if ( !( s == N || s == 0) ) {
426 #endif // include guard
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.