Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
DataFieldInfo.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/stella/DataFieldInfo.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 
17 #include "serialbox/core/Type.h"
19 
20 namespace serialbox {
21 
22 namespace stella {
23 
24 DataFieldInfo::DataFieldInfo() : fieldMetainfoImpl_(), metainfo_() {}
25 
26 DataFieldInfo::DataFieldInfo(const boost::shared_ptr<FieldMetainfoImpl>& fieldMetainfoImpl)
27  : fieldMetainfoImpl_(fieldMetainfoImpl),
28  metainfo_(internal::make_shared_ptr<MetainfoMapImpl>(fieldMetainfoImpl_->metaInfoPtr())) {}
29 
31  : fieldMetainfoImpl_(other.fieldMetainfoImpl_),
32  metainfo_(internal::make_shared_ptr<MetainfoMapImpl>(fieldMetainfoImpl_->metaInfoPtr())) {}
33 
35  *fieldMetainfoImpl_ = *other.fieldMetainfoImpl_;
36  return (*this);
37 }
38 
39 void DataFieldInfo::Init(std::string name, std::string type, int bytesPerElement, int rank,
40  int iSize, int jSize, int kSize, int lSize, int iMinusHalo, int iPlusHalo,
41  int jMinusHalo, int jPlusHalo, int kMinusHalo, int kPlusHalo,
42  int lMinusHalo, int lPlusHalo) {
43 
44  fieldMetainfoImpl_ = boost::make_shared<FieldMetainfoImpl>();
45  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(fieldMetainfoImpl_->metaInfoPtr()));
46 
47  try {
48  TypeID typeID = internal::TypeNameToTypeID(type);
49  fieldMetainfoImpl_->type() = typeID;
50 
51  if(bytesPerElement != TypeUtil::sizeOf(typeID))
52  throw Exception("inconsistent bytes-per-element: got '%i' but according to passed type "
53  "'%s' expected '%i'",
54  bytesPerElement, type, TypeUtil::sizeOf(typeID));
55 
56  fieldMetainfoImpl_->dims() = std::vector<int>{iSize, jSize, kSize, lSize};
57 
58  MetainfoMapImpl& metaInfo = fieldMetainfoImpl_->metaInfo();
59  metaInfo.clear();
60  metaInfo.insert("__name", name);
61  metaInfo.insert("__elementtype", type);
62  metaInfo.insert("__bytesperelement", bytesPerElement);
63  metaInfo.insert("__rank", rank);
64  metaInfo.insert("__isize", iSize);
65  metaInfo.insert("__jsize", jSize);
66  metaInfo.insert("__ksize", kSize);
67  metaInfo.insert("__lsize", lSize);
68  metaInfo.insert("__iminushalosize", iMinusHalo);
69  metaInfo.insert("__iplushalosize", iPlusHalo);
70  metaInfo.insert("__jminushalosize", jMinusHalo);
71  metaInfo.insert("__jplushalosize", jPlusHalo);
72  metaInfo.insert("__kminushalosize", kMinusHalo);
73  metaInfo.insert("__kplushalosize", kPlusHalo);
74  metaInfo.insert("__lminushalosize", lMinusHalo);
75  metaInfo.insert("__lplushalosize", lPlusHalo);
76 
77  } catch(Exception& e) {
78  internal::throwSerializationException("Error: %s", e.what());
79  }
80 }
81 
82 std::string DataFieldInfo::name() const {
83  return fieldMetainfoImpl_->metaInfo().hasKey("__name")
84  ? fieldMetainfoImpl_->metaInfo().as<std::string>("__name")
85  : "";
86 }
87 
88 std::string DataFieldInfo::type() const {
89  return fieldMetainfoImpl_->metaInfo().hasKey("__elementtype")
90  ? fieldMetainfoImpl_->metaInfo().as<std::string>("__elementtype")
91  : "";
92 }
93 
95  return fieldMetainfoImpl_->metaInfo().hasKey("__bytesperelement")
96  ? fieldMetainfoImpl_->metaInfo().as<int>("__bytesperelement")
97  : 0;
98 }
99 
100 int DataFieldInfo::rank() const {
101  return fieldMetainfoImpl_->metaInfo().hasKey("__rank")
102  ? fieldMetainfoImpl_->metaInfo().as<int>("__rank")
103  : 0;
104 }
105 
106 int DataFieldInfo::iSize() const {
107  return fieldMetainfoImpl_->metaInfo().hasKey("__isize")
108  ? fieldMetainfoImpl_->metaInfo().as<int>("__isize")
109  : 1;
110 }
111 
112 int DataFieldInfo::jSize() const {
113  return fieldMetainfoImpl_->metaInfo().hasKey("__jsize")
114  ? fieldMetainfoImpl_->metaInfo().as<int>("__jsize")
115  : 1;
116 }
117 
118 int DataFieldInfo::kSize() const {
119  return fieldMetainfoImpl_->metaInfo().hasKey("__ksize")
120  ? fieldMetainfoImpl_->metaInfo().as<int>("__ksize")
121  : 1;
122 }
123 
124 int DataFieldInfo::lSize() const {
125  return fieldMetainfoImpl_->metaInfo().hasKey("__lsize")
126  ? fieldMetainfoImpl_->metaInfo().as<int>("__lsize")
127  : 1;
128 }
129 
131  return fieldMetainfoImpl_->metaInfo().hasKey("__iminushalosize")
132  ? fieldMetainfoImpl_->metaInfo().as<int>("__iminushalosize")
133  : 0;
134 }
135 
137  return fieldMetainfoImpl_->metaInfo().hasKey("__iplushalosize")
138  ? fieldMetainfoImpl_->metaInfo().as<int>("__iplushalosize")
139  : 0;
140 }
141 
143  return fieldMetainfoImpl_->metaInfo().hasKey("__jminushalosize")
144  ? fieldMetainfoImpl_->metaInfo().as<int>("__jminushalosize")
145  : 0;
146 }
147 
149  return fieldMetainfoImpl_->metaInfo().hasKey("__jplushalosize")
150  ? fieldMetainfoImpl_->metaInfo().as<int>("__jplushalosize")
151  : 0;
152 }
153 
155  return fieldMetainfoImpl_->metaInfo().hasKey("__kminushalosize")
156  ? fieldMetainfoImpl_->metaInfo().as<int>("__kminushalosize")
157  : 0;
158 }
159 
161  return fieldMetainfoImpl_->metaInfo().hasKey("__kplushalosize")
162  ? fieldMetainfoImpl_->metaInfo().as<int>("__kplushalosize")
163  : 0;
164 }
165 
167  return fieldMetainfoImpl_->metaInfo().hasKey("__lminushalosize")
168  ? fieldMetainfoImpl_->metaInfo().as<int>("__lminushalosize")
169  : 0;
170 }
171 
173  return fieldMetainfoImpl_->metaInfo().hasKey("__lplushalosize")
174  ? fieldMetainfoImpl_->metaInfo().as<int>("__lplushalosize")
175  : 0;
176 }
177 
178 bool DataFieldInfo::operator==(const DataFieldInfo& other) const {
179  return (*fieldMetainfoImpl_ == *other.fieldMetainfoImpl_);
180 }
181 
182 std::string DataFieldInfo::ToString() const {
183  std::ostringstream ss;
184  const auto& metaInfo = fieldMetainfoImpl_->metaInfo();
185 
186  std::string name = metaInfo.hasKey("__name") ? metaInfo.as<std::string>("__name") : "";
187 
188  const auto& dims = fieldMetainfoImpl_->dims();
189  int iSize = dims.size() < 1 ? 1 : dims[0];
190  int jSize = dims.size() < 2 ? 1 : dims[1];
191  int kSize = dims.size() < 3 ? 1 : dims[2];
192  int lSize = dims.size() < 4 ? 1 : dims[3];
193 
194  ss << name << " (" << iSize << "x" << jSize << "x" << kSize << "x" << lSize << ") "
195  << metainfo_.ToString();
196  return ss.str();
197 }
198 
199 void DataFieldInfo::setImpl(const boost::shared_ptr<FieldMetainfoImpl>& fieldMetainfoImpl) {
200  metainfo_.setImpl(internal::make_shared_ptr<MetainfoMapImpl>(fieldMetainfoImpl_->metaInfoPtr()));
201 }
202 
203 boost::shared_ptr<FieldMetainfoImpl>& DataFieldInfo::getImpl() { return fieldMetainfoImpl_; }
204 
205 const boost::shared_ptr<FieldMetainfoImpl>& DataFieldInfo::getImpl() const {
206  return fieldMetainfoImpl_;
207 }
208 
209 } // namespace stella
210 
211 } // namespace serialbox
void clear() noexcept
All the elements in the MetainfoMapImpl are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.
boost::shared_ptr< FieldMetainfoImpl > & getImpl()
Get implementation pointer.
std::string name() const
The field name.
int iSize() const
The size of the field in i-direction, including the halo.
int kPlusHaloSize() const
The size of the halo in positive k-direction.
int kSize() const
The size of the field in k-direction, including the halo.
Information of a data-field.
Definition: DataFieldInfo.h:31
bool insert(KeyType &&key, ValueType &&value) noexcept
Insert a new element in the map.
int lSize() const
The size of the field in l-direction, including the halo.
bool operator==(const DataFieldInfo &other) const
Comparator operator.
std::string type() const
The field scalar type.
static int sizeOf(TypeID id)
Get size of the type.
Definition: Type.cpp:73
int kMinusHaloSize() const
The size of the halo in negative k-direction.
Namespace of the STELLA frontend.
Definition: ForwardDecl.h:27
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
void Init(const TDataField &dataField)
Initialize with field.
Definition: DataFieldInfo.h:51
int lMinusHaloSize() const
The size of the halo in negative l-direction.
std::string ToString() const
Creates a string that carries the metainfo in a human-readable form.
int lPlusHaloSize() const
The size of the halo in positive l-direction.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
int jMinusHaloSize() const
The size of the halo in negative j-direction.
void setImpl(const boost::shared_ptr< MetainfoMapImpl > &metaInfoMap)
Set implementation pointer.
int iMinusHaloSize() const
The size of the halo in negative i-direction.
int iPlusHaloSize() const
The size of the halo in positive i-direction.
int bytesPerElement() const
The dimension in bytes of a single element.
DataFieldInfo & operator=(const DataFieldInfo &other)
Assignment operator.
std::string ToString() const
Gives a string representation of the object, useful for debugging.
int rank() const
The number of dimensions of the field.
DataFieldInfo()
Default constructor.
Exception class which stores a human-readable error description.
Definition: Exception.h:30
void setImpl(const boost::shared_ptr< FieldMetainfoImpl > &fieldMetainfoImpl)
Set implementation pointer.
int jPlusHaloSize() const
The size of the halo in positive j-direction.
int jSize() const
The size of the field in j-direction, including the halo.