Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MetainfoSet.h
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/stella/MetainfoSet.h --------------------------------*- 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 
15 #ifndef SERIALBOX_CORE_FRONTEND_STELLA_METAINFOSET_H
16 #define SERIALBOX_CORE_FRONTEND_STELLA_METAINFOSET_H
17 
19 #include <boost/any.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <boost/type_traits.hpp>
22 #include <utility>
23 #include <vector>
24 
25 namespace serialbox {
26 
27 namespace stella {
28 
36 class MetainfoSet {
37 public:
39  MetainfoSet();
40 
42  explicit MetainfoSet(const boost::shared_ptr<MetainfoMapImpl>& map);
43 
45  MetainfoSet(const MetainfoSet& other);
46 
53  MetainfoSet& operator=(const MetainfoSet& other);
54 
56  void Cleanup();
57 
64  std::vector<std::string> keys() const;
65 
69  bool HasKey(const std::string& key) const;
70 
78  void AddMetainfo(const std::string& key, const int& value);
79  void AddMetainfo(const std::string& key, const bool& value);
80  void AddMetainfo(const std::string& key, const float& value);
81  void AddMetainfo(const std::string& key, const double& value);
82  void AddMetainfo(const std::string& key, const std::string& value);
83  void AddMetainfo(const std::string& key, const char* value) {
84  AddMetainfo(key, std::string(value));
85  }
87 
94  const boost::any& AsAny(const std::string& key) const;
95 
105  bool AsBool(const std::string& key) const;
106 
118  int AsInt(const std::string& key) const;
119 
128  float AsFloat(const std::string& key) const;
129 
138  double AsDouble(const std::string& key) const;
139 
149  template <typename RealType>
150  RealType AsReal(const std::string& key) const {
151  BOOST_STATIC_ASSERT(
152  (boost::is_same<RealType, float>::value || boost::is_same<RealType, double>::value));
153 
154  if(boost::is_same<RealType, float>::value)
155  return AsFloat(key);
156  else if(boost::is_same<RealType, double>::value)
157  return AsDouble(key);
158  }
159 
168  std::string AsString(const std::string& key) const;
169 
173  std::string ToString() const;
174 
178  std::size_t size() const;
179 
181  bool operator==(const MetainfoSet& other) const;
182 
184  void setImpl(const boost::shared_ptr<MetainfoMapImpl>& metaInfoMap);
185 
187  boost::shared_ptr<MetainfoMapImpl>& getImpl();
188  const boost::shared_ptr<MetainfoMapImpl>& getImpl() const;
189 
190 private:
191  boost::shared_ptr<MetainfoMapImpl> mapImpl_;
192 };
193 
194 } // namespace stella
195 
196 } // namespace serialbox
197 
198 #endif
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
RealType AsReal(const std::string &key) const
Extracts a value in RealType representation, where RealType is either float or double ...
Definition: MetainfoSet.h:150
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
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
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
MetainfoSet()
Construct empty map.
Definition: MetainfoSet.cpp:38