Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Savepoint.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/stella/Savepoint.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 
18 #include <ostream>
19 
20 namespace serialbox {
21 
22 namespace stella {
23 
24 namespace internal {
25 
26 template <class KeyType, class ValueType>
27 void insertHelper(boost::shared_ptr<SavepointImpl>& savepointImpl, KeyType&& key,
28  ValueType&& value) {
29  try {
30  savepointImpl->addMetainfo(key, value);
31  } catch(Exception& e) {
32  internal::throwSerializationException("Error: metainfo with key = %s exists already", key);
33  }
34 }
35 
36 } // namespace internal
37 
38 Savepoint::Savepoint() : savepointImpl_(), metainfo_() {}
39 
40 void Savepoint::Init(const std::string& name) {
41  savepointImpl_ = boost::make_shared<SavepointImpl>(name);
42  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(savepointImpl_->metaInfoPtr()));
43 }
44 
45 Savepoint::Savepoint(const boost::shared_ptr<SavepointImpl>& savepointImpl)
46  : savepointImpl_(savepointImpl),
47  metainfo_(internal::make_shared_ptr<MetainfoMapImpl>(savepointImpl_->metaInfoPtr())){};
48 
50  savepointImpl_ = other.savepointImpl_;
51  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(savepointImpl_->metaInfoPtr()));
52 }
53 
55  // Make sure savepointImpl_ is allocated
56  if(!savepointImpl_)
57  Init("");
58 
59  // Use the assignment operator of the implementation class
60  *savepointImpl_ = *other.savepointImpl_;
61  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(savepointImpl_->metaInfoPtr()));
62  return (*this);
63 }
64 
65 void Savepoint::AddMetainfo(const std::string& key, const bool& value) {
66  internal::insertHelper(savepointImpl_, key, value);
67 }
68 
69 void Savepoint::AddMetainfo(const std::string& key, const int& value) {
70  internal::insertHelper(savepointImpl_, key, value);
71 }
72 
73 void Savepoint::AddMetainfo(const std::string& key, const float& value) {
74  internal::insertHelper(savepointImpl_, key, value);
75 }
76 
77 void Savepoint::AddMetainfo(const std::string& key, const double& value) {
78  internal::insertHelper(savepointImpl_, key, value);
79 }
80 
81 void Savepoint::AddMetainfo(const std::string& key, const std::string& value) {
82  internal::insertHelper(savepointImpl_, key, value);
83 }
84 
85 bool Savepoint::operator==(const Savepoint& other) const {
86  return (*savepointImpl_ == *other.savepointImpl_);
87 }
88 
89 bool Savepoint::operator!=(const Savepoint& other) const {
90  return (*savepointImpl_ != *other.savepointImpl_);
91 }
92 
93 const std::string& Savepoint::name() const { return savepointImpl_->name(); }
94 
95 std::string Savepoint::ToString() const { return savepointImpl_->toString(); }
96 
97 std::ostream& operator<<(std::ostream& out, const Savepoint& sp) { return (out << sp.ToString()); }
98 
99 void Savepoint::setImpl(const boost::shared_ptr<SavepointImpl>& savepointImpl) {
100  savepointImpl_ = savepointImpl;
101  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(savepointImpl_->metaInfoPtr()));
102 }
103 
104 boost::shared_ptr<SavepointImpl>& Savepoint::getImpl() { return savepointImpl_; }
105 
106 const boost::shared_ptr<SavepointImpl>& Savepoint::getImpl() const { return savepointImpl_; }
107 
108 } // namespace stella
109 
110 } // namespace serialbox
void setImpl(const boost::shared_ptr< SavepointImpl > &savepointImpl)
Set implementation pointer.
Definition: Savepoint.cpp:99
Namespace of the STELLA frontend.
Definition: ForwardDecl.h:27
Namespace of the serialbox library.
Definition: Archive.h:25
bool operator==(const Savepoint &other) const
Compare equal.
Definition: Savepoint.cpp:85
void Init(const std::string &name)
Initialize the savepoint.
Definition: Savepoint.cpp:40
Implementation of the STELLA Savepoint.
Definition: Savepoint.h:30
Savepoint & operator=(const Savepoint &other)
Copy assignment.
Definition: Savepoint.cpp:54
void AddMetainfo(const std::string &key, const bool &value)
Add metainformation to the savepoint.
Definition: Savepoint.cpp:65
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.
Savepoint()
Construct empty savepoint with name name
Definition: Savepoint.cpp:38
const std::string & name() const
Access to the name.
Definition: Savepoint.cpp:93
boost::shared_ptr< SavepointImpl > & getImpl()
Get implementation pointer.
Definition: Savepoint.cpp:104
friend std::ostream & operator<<(std::ostream &out, const Savepoint &sp)
Convert to stream.
Definition: Savepoint.cpp:97
bool operator!=(const Savepoint &other) const
Compare unequal.
Definition: Savepoint.cpp:89
std::string ToString() const
Convert to string.
Definition: Savepoint.cpp:95