Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
SavepointImpl.h
Go to the documentation of this file.
1 //===-- serialbox/core/SavepointImpl.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_SAVEPOINTIMPL_H
16 #define SERIALBOX_CORE_SAVEPOINTIMPL_H
17 
19 #include "serialbox/core/Json.h"
21 #include <functional>
22 #include <iosfwd>
23 #include <string>
24 #include <utility>
25 
26 namespace serialbox {
27 
30 
39 public:
41  template <class StringType,
42  class = typename std::enable_if<!std::is_same<StringType, json::json>::value>::type>
43  explicit SavepointImpl(const StringType& name)
44  : name_(name), metaInfo_(std::make_shared<MetainfoMapImpl>()) {}
45 
47  template <class StringType, class MetainfoType>
48  SavepointImpl(StringType&& name, MetainfoType&& metaInfo)
49  : name_(name),
50  metaInfo_(std::make_shared<MetainfoMapImpl>(std::forward<MetainfoType>(metaInfo))) {}
51 
53  SavepointImpl(const SavepointImpl& other) { *this = other; }
54 
56  SavepointImpl(SavepointImpl&&) = default;
57 
59  explicit SavepointImpl(const json::json& jsonNode) { fromJSON(jsonNode); }
60 
62  SavepointImpl& operator=(const SavepointImpl& other);
63 
66 
73  template <class StringType, class ValueType>
74  void addMetainfo(StringType&& key, ValueType&& value) {
75  if(!metaInfo_->insert(std::forward<StringType>(key), std::forward<ValueType>(value)))
76  throw Exception("cannot add element with key '%s' to metaInfo: element already exists", key);
77  }
78 
85  template <class T, class StringType>
86  T getMetainfoAs(StringType&& key) const {
87  try {
88  return metaInfo_->at(key).template as<T>();
89  } catch(Exception& e) {
90  throw Exception("cannot get element with key '%s' from metaInfo: %s", key, e.what());
91  }
92  }
93 
95  bool operator==(const SavepointImpl& right) const {
96  return (name_ == right.name_) && (*metaInfo_ == *right.metaInfo_);
97  }
98 
100  bool operator!=(const SavepointImpl& right) const { return (!(*this == right)); }
101 
103  void swap(SavepointImpl& other) noexcept {
104  name_.swap(other.name_);
105  metaInfo_->swap(*other.metaInfo_);
106  }
107 
109  const std::string& name() const noexcept { return name_; }
110 
112  MetainfoMapImpl& metaInfo() noexcept { return *metaInfo_; }
113  const MetainfoMapImpl& metaInfo() const noexcept { return *metaInfo_; }
114 
117  bool empty() const noexcept { return metaInfo_->empty(); }
118 
120  json::json toJSON() const;
121 
125  void fromJSON(const json::json& jsonNode);
126 
128  std::string toString() const;
129 
131  friend std::ostream& operator<<(std::ostream& stream, const SavepointImpl& s);
132 
134  std::shared_ptr<MetainfoMapImpl>& metaInfoPtr() noexcept { return metaInfo_; }
135  const std::shared_ptr<MetainfoMapImpl>& metaInfoPtr() const noexcept { return metaInfo_; }
136 
137 private:
138  std::string name_;
139  std::shared_ptr<MetainfoMapImpl> metaInfo_;
140 };
141 
143 
144 } // namespace serialbox
145 
146 namespace std {
147 
153 template <>
154 struct hash<serialbox::SavepointImpl> {
155  std::size_t operator()(const serialbox::SavepointImpl& s) const noexcept {
156  return std::hash<std::string>()(s.name());
157  }
158 };
159 
160 } // namespace std
161 
162 #endif
json::json toJSON() const
Convert to JSON.
SavepointImpl(const json::json &jsonNode)
Construct from JSON.
Definition: SavepointImpl.h:59
void swap(SavepointImpl &other) noexcept
Swap with other.
T getMetainfoAs(StringType &&key) const
Query metaInfo for key key and retrieve value as type T
Definition: SavepointImpl.h:86
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
std::string toString() const
Convert savepoint to string.
Definition: json.hpp:10517
bool empty() const noexcept
Returns a bool value indicating whether the savepoint is empty (i.e has no meta-information attached)...
SavepointImpl(StringType &&name, MetainfoType &&metaInfo)
Construct savepoint with name and metaInfo
Definition: SavepointImpl.h:48
void addMetainfo(StringType &&key, ValueType &&value)
Add a new key = value pair to the metaInfo of the Savepoint.
Definition: SavepointImpl.h:74
bool operator==(const SavepointImpl &right) const
Test for equality.
Definition: SavepointImpl.h:95
SavepointImpl(const SavepointImpl &other)
Copy constructor.
Definition: SavepointImpl.h:53
Namespace of the serialbox library.
Definition: Archive.h:25
bool operator!=(const SavepointImpl &right) const
Test for inequality.
const std::string & name() const noexcept
Access name.
SavepointImpl & operator=(const SavepointImpl &other)
Copy assignment.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
SavepointImpl(const StringType &name)
Construct an empty savepoint (wihtout metaInfo i.e this->empty() == true)
Definition: SavepointImpl.h:43
std::shared_ptr< MetainfoMapImpl > & metaInfoPtr() noexcept
Get meta-info pointer.
MetainfoMapImpl & metaInfo() noexcept
Access meta-info.
Exception class which stores a human-readable error description.
Definition: Exception.h:30
Shared implementation of the Savepoint.
Definition: SavepointImpl.h:38
friend std::ostream & operator<<(std::ostream &stream, const SavepointImpl &s)
Convert to stream.