Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
FieldMetainfoImpl.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/FieldMetainfoImplImpl.h ------------------------------------------*- C++
2 //-*-===//
3 //
4 // S E R I A L B O X
5 //
6 // This file is distributed under terms of BSD license.
7 // See LICENSE.txt for more information
8 //
9 //===------------------------------------------------------------------------------------------===//
10 //
14 //===------------------------------------------------------------------------------------------===//
15 
17 #include <algorithm>
18 #include <memory>
19 
20 namespace serialbox {
21 
23  type_ = other.type_;
24  dims_ = other.dims_;
25  metaInfo_ = std::make_shared<MetainfoMapImpl>(*other.metaInfo_);
26  return (*this);
27 }
28 
30  std::swap(type_, other.type_);
31  dims_.swap(other.dims_);
32  metaInfo_->swap(*other.metaInfo_);
33 }
34 
35 bool FieldMetainfoImpl::operator==(const FieldMetainfoImpl& right) const noexcept {
36  if(type_ != right.type_)
37  return false;
38 
39  if(dims_.size() != right.dims_.size() ||
40  !std::equal(dims_.begin(), dims_.end(), right.dims_.begin()))
41  return false;
42 
43  return (*metaInfo_ == *right.metaInfo_);
44 }
45 
46 json::json FieldMetainfoImpl::toJSON() const {
47  json::json jsonNode;
48  jsonNode["type_id"] = static_cast<int>(type_);
49  jsonNode["dims"] = dims_;
50  jsonNode["meta_info"] = metaInfo_->toJSON();
51  return jsonNode;
52 }
53 
54 void FieldMetainfoImpl::fromJSON(const json::json& jsonNode) {
55  dims_.clear();
56  metaInfo_->clear();
57 
58  if(jsonNode.is_null() || jsonNode.empty())
59  throw Exception("node is empty");
60 
61  if(!jsonNode.count("type_id"))
62  throw Exception("no node 'type_id'");
63  type_ = static_cast<TypeID>(int(jsonNode["type_id"]));
64 
65  if(!jsonNode.count("dims"))
66  throw Exception("no node 'value'");
67  for(auto it = jsonNode["dims"].begin(), end = jsonNode["dims"].end(); it != end; ++it)
68  dims_.push_back(int(*it));
69 
70  if(!jsonNode.count("meta_info"))
71  throw Exception("no node 'meta_info'");
72  try {
73  metaInfo_->fromJSON(jsonNode["meta_info"]);
74  } catch(Exception& e) {
75  throw Exception("in node 'meta_info': %s", e.what());
76  }
77 }
78 
79 std::string FieldMetainfoImpl::toString() const {
80  std::stringstream ss;
81  ss << "type = " << TypeUtil::toString(type_) << ", dims = [" << ArrayUtil::toString(dims_)
82  << "], "
83  << "metainfo = " << *metaInfo_;
84  return ss.str();
85 }
86 
87 std::ostream& operator<<(std::ostream& stream, const FieldMetainfoImpl& f) {
88  return (stream << f.toString());
89 }
90 
91 } // namespace serialbox
static std::string toString(const Array< T > &array)
Convert to string.
Definition: Array.h:38
std::string toString() const
Convert to string.
Meta-information of a data field.
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
friend std::ostream & operator<<(std::ostream &stream, const FieldMetainfoImpl &f)
Convert to stream.
bool operator==(const FieldMetainfoImpl &right) const noexcept
Test for equality.
void swap(FieldMetainfoImpl &other) noexcept
Swap with other.
static std::string toString(TypeID id)
Convert to string.
Definition: Type.cpp:35
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
json::json toJSON() const
Convert to JSON.
FieldMetainfoImpl & operator=(const FieldMetainfoImpl &other)
Copy assignment.
Exception class which stores a human-readable error description.
Definition: Exception.h:30