Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MetainfoSet.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/stella/MetainfoSet.cpp ------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
20 #include <boost/algorithm/string.hpp>
21 
22 namespace serialbox {
23 
24 namespace stella {
25 
26 namespace internal {
27 
28 template <class KeyType>
30 checkKeyExists(const boost::shared_ptr<MetainfoMapImpl>& mapImpl, KeyType&& key) {
31  auto it = mapImpl->find(key);
32  if(it == mapImpl->end())
33  internal::throwSerializationException("Error: requested key %s is not in set", key);
34  return it;
35 }
36 }
37 
38 MetainfoSet::MetainfoSet() : mapImpl_(boost::make_shared<MetainfoMapImpl>()) {}
39 
40 MetainfoSet::MetainfoSet(const boost::shared_ptr<MetainfoMapImpl>& map) : mapImpl_(map){};
41 
42 MetainfoSet::MetainfoSet(const MetainfoSet& other) { mapImpl_ = other.mapImpl_; }
43 
45  *mapImpl_ = *other.mapImpl_;
46  return (*this);
47 }
48 
49 void MetainfoSet::Cleanup() { mapImpl_->clear(); }
50 
51 bool MetainfoSet::HasKey(const std::string& key) const { return mapImpl_->hasKey(key); }
52 
53 void MetainfoSet::AddMetainfo(const std::string& key, const bool& value) {
54  if(!mapImpl_->insert(key, value))
55  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
56 }
57 
58 void MetainfoSet::AddMetainfo(const std::string& key, const int& value) {
59  if(!mapImpl_->insert(key, value))
60  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
61 }
62 
63 void MetainfoSet::AddMetainfo(const std::string& key, const float& value) {
64  if(!mapImpl_->insert(key, value))
65  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
66 }
67 
68 void MetainfoSet::AddMetainfo(const std::string& key, const double& value) {
69  if(!mapImpl_->insert(key, value))
70  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
71 }
72 
73 void MetainfoSet::AddMetainfo(const std::string& key, const std::string& value) {
74  if(!mapImpl_->insert(key, value))
75  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
76 }
77 
78 const boost::any& MetainfoSet::AsAny(const std::string& key) const {
79  return internal::checkKeyExists(mapImpl_, key)->second.any();
80 }
81 
82 bool MetainfoSet::AsBool(const std::string& key) const {
83  MetainfoValueImpl value = internal::checkKeyExists(mapImpl_, key)->second;
84  return value.as<bool>();
85 }
86 
87 int MetainfoSet::AsInt(const std::string& key) const {
88  MetainfoValueImpl value = internal::checkKeyExists(mapImpl_, key)->second;
89  try {
90  return value.as<int>();
91  } catch(Exception& e) {
92  internal::throwSerializationException("Error: %s", e.what());
93  }
94  serialbox_unreachable("unreachable");
95 }
96 
97 float MetainfoSet::AsFloat(const std::string& key) const {
98  MetainfoValueImpl value = internal::checkKeyExists(mapImpl_, key)->second;
99  return value.as<float>();
100 }
101 
102 double MetainfoSet::AsDouble(const std::string& key) const {
103  MetainfoValueImpl value = internal::checkKeyExists(mapImpl_, key)->second;
104  return value.as<double>();
105 }
106 
107 std::string MetainfoSet::AsString(const std::string& key) const {
108  MetainfoValueImpl value = internal::checkKeyExists(mapImpl_, key)->second;
109  return value.as<std::string>();
110 }
111 
112 std::string MetainfoSet::ToString() const {
113  std::ostringstream ss;
114  ss << "[ ";
115  for(auto it = mapImpl_->begin(), end = mapImpl_->end(); it != end; ++it)
116  if(!boost::algorithm::starts_with(it->first, "__"))
117  ss << it->first << "=" << it->second.toString() << " ";
118  ss << "]";
119  return ss.str();
120 }
121 
122 std::size_t MetainfoSet::size() const {
123  std::size_t s = 0;
124  for(auto it = mapImpl_->begin(), end = mapImpl_->end(); it != end; ++it)
125  if(!boost::algorithm::starts_with(it->first, "__"))
126  s++;
127  return s;
128 }
129 
130 bool MetainfoSet::operator==(const MetainfoSet& other) const {
131  return (*mapImpl_ == *other.mapImpl_);
132 }
133 
134 void MetainfoSet::setImpl(const boost::shared_ptr<MetainfoMapImpl>& metaInfoMap) {
135  mapImpl_ = metaInfoMap;
136 }
137 
138 std::vector<std::string> MetainfoSet::keys() const {
139  std::vector<std::string> keyStr;
140  for(auto it = mapImpl_->begin(), end = mapImpl_->end(); it != end; ++it)
141  keyStr.push_back(it->first);
142  return keyStr;
143 }
144 
145 boost::shared_ptr<MetainfoMapImpl>& MetainfoSet::getImpl() { return mapImpl_; }
146 const boost::shared_ptr<MetainfoMapImpl>& MetainfoSet::getImpl() const { return mapImpl_; }
147 
148 } // namespace stella
149 
150 } // namespace serialbox
Represent an immutable meta information value as a type-id and type-erased data.
double AsDouble(const std::string &key) const
MetainfoSet & operator=(const MetainfoSet &other)
Assignment operator.
Definition: MetainfoSet.cpp:44
std::vector< std::string > keys() const
Gives access to all existing keys.
bool AsBool(const std::string &key) const
Extracts a value in bool representation.
Definition: MetainfoSet.cpp:82
bool HasKey(const std::string &key) const
Check if key exists already in set.
Definition: MetainfoSet.cpp:51
void Cleanup()
Removes all metainfo and frees all memory.
Definition: MetainfoSet.cpp:49
std::string AsString(const std::string &key) const
Extracts a value assuming its type is string.
Namespace of the STELLA frontend.
Definition: ForwardDecl.h:27
Namespace of the serialbox library.
Definition: Archive.h:25
Meta-information set.
Definition: MetainfoSet.h:36
std::string ToString() const
Creates a string that carries the metainfo in a human-readable form.
bool operator==(const MetainfoSet &other) const
Comparison operator.
float AsFloat(const std::string &key) const
Extracts a value in single precision floating point representation.
Definition: MetainfoSet.cpp:97
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
void setImpl(const boost::shared_ptr< MetainfoMapImpl > &metaInfoMap)
Set implementation pointer.
int AsInt(const std::string &key) const
Extracts a value in int representation.
Definition: MetainfoSet.cpp:87
std::size_t size() const
Number elements in the set.
const boost::any & AsAny(const std::string &key) const
Gives access to the internal representation of the requested metainfo.
Definition: MetainfoSet.cpp:78
T as() const
Convert the value to type T
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: Unreachable.h:41
map_type::const_iterator const_iterator
A forward iterator to const value_type
boost::shared_ptr< MetainfoMapImpl > & getImpl()
Get implementation pointer.
void AddMetainfo(const std::string &key, const int &value)
Add a new key-value pair into the set. The key must not exist yet.
Definition: MetainfoSet.cpp:58
Exception class which stores a human-readable error description.
Definition: Exception.h:30
MetainfoSet()
Construct empty map.
Definition: MetainfoSet.cpp:38