Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Metainfo.cpp
1 /*===-- serialbox-c/Metainfo.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  *
10  *! \file
11  *! This file contains the C implementation of the Metainfo.
12  *
13 \*===------------------------------------------------------------------------------------------===*/
14 
15 #include "serialbox-c/Metainfo.h"
16 #include "serialbox-c/Utility.h"
17 #include <cstdlib>
18 
19 using namespace serialboxC;
20 
21 /*===------------------------------------------------------------------------------------------===*\
22  * Construction & Destruction
23 \*===------------------------------------------------------------------------------------------===*/
24 
26  serialboxMetainfo_t* metaInfo = allocate<serialboxMetainfo_t>();
27  try {
28  metaInfo->impl = new MetainfoMap;
29  metaInfo->ownsData = 1;
30  } catch(std::exception& e) {
31  std::free(metaInfo);
32  metaInfo = NULL;
33  serialboxFatalError(e.what());
34  }
35  return metaInfo;
36 }
37 
39  const MetainfoMap* otherMap = toConstMetainfoMap(other);
40  serialboxMetainfo_t* metaInfo = allocate<serialboxMetainfo_t>();
41  try {
42  metaInfo->impl = new MetainfoMap(*otherMap);
43  metaInfo->ownsData = 1;
44  } catch(std::exception& e) {
45  std::free(metaInfo);
46  metaInfo = NULL;
47  serialboxFatalError(e.what());
48  }
49  return metaInfo;
50 }
51 
53  if(metaInfo) {
54  MetainfoMap* map = toMetainfoMap(metaInfo);
55  if(metaInfo->ownsData)
56  delete map;
57  std::free(metaInfo);
58  }
59 }
60 
61 /*===------------------------------------------------------------------------------------------===*\
62  * Utility
63 \*===------------------------------------------------------------------------------------------===*/
64 
66  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
67  return (int)map->size();
68 }
69 
71  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
72  return (int)map->empty();
73 }
74 
76  MetainfoMap* map = toMetainfoMap(metaInfo);
77  map->clear();
78 }
79 
81  const MetainfoMap* map1 = toConstMetainfoMap(m1);
82  const MetainfoMap* map2 = toConstMetainfoMap(m2);
83  return (*map1 == *map2);
84 }
85 
86 int serialboxMetainfoHasKey(const serialboxMetainfo_t* metaInfo, const char* key) {
87  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
88  return (int)map->hasKey(key);
89 }
90 
92  const char* key) {
93  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
94  serialboxTypeID id = Invalid;
95  try {
96  id = (serialboxTypeID)map->at(key).type();
97  } catch(std::exception& e) {
98  serialboxFatalError(e.what());
99  }
100  return id;
101 }
102 
104  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
105  std::stringstream ss;
106  ss << *map;
107  return allocateAndCopyString(ss.str());
108 }
109 
112  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
113  serialboxMetainfoElementInfo_t* elements = allocate<serialboxMetainfoElementInfo_t>();
114 
115  // keys
116  const auto keyVector = map->keys();
117  elements->keys = (char**)std::malloc(keyVector.size() * sizeof(char*));
118  for(std::size_t i = 0; i < keyVector.size(); ++i)
119  elements->keys[i] = allocateAndCopyString(keyVector[i]);
120 
121  // types
122  const auto typeVector = map->types();
123  elements->types = (int*)std::malloc(typeVector.size() * sizeof(int));
124  for(std::size_t i = 0; i < typeVector.size(); ++i)
125  elements->types[i] = (int)typeVector[i];
126 
127  // len
128  elements->len = (int)map->size();
129 
130  return elements;
131 }
132 
134  if(elementInfo) {
135  // keys
136  for(int i = 0; i < elementInfo->len; ++i)
137  std::free(elementInfo->keys[i]);
138  std::free(elementInfo->keys);
139 
140  // types
141  std::free(elementInfo->types);
142 
143  // element-info
144  std::free(elementInfo);
145  }
146 }
147 
148 SERIALBOX_API int serialboxMetainfoDeleteKey(serialboxMetainfo_t* metaInfo, const char* key) {
149  MetainfoMap* map = toMetainfoMap(metaInfo);
150  return map->erase(key);
151 }
152 
153 /*===------------------------------------------------------------------------------------------===*\
154  * Add meta-information
155 \*===------------------------------------------------------------------------------------------===*/
156 
157 #define SERIALBOX_METAINFO_ADD_IMPL(name, serialboxPrimitveType, serialboxArrayType, CXXType) \
158  int serialboxMetainfoAdd##name(serialboxMetainfo_t* metaInfo, const char* key, \
159  serialboxPrimitveType value) { \
160  MetainfoMap* map = toMetainfoMap(metaInfo); \
161  return static_cast<int>(map->insert(key, CXXType(value))); \
162  } \
163  \
164  int serialboxMetainfoAddArrayOf##name(serialboxMetainfo_t* metaInfo, const char* key, \
165  const serialboxArrayType* array) { \
166  MetainfoMap* map = toMetainfoMap(metaInfo); \
167  serialbox::Array<CXXType> vec(array->data, array->data + array->len); \
168  return static_cast<int>(map->insert(key, vec)); \
169  }
170 
171 SERIALBOX_METAINFO_ADD_IMPL(Boolean, serialboxBoolean_t, serialboxArrayOfBoolean_t, bool);
172 SERIALBOX_METAINFO_ADD_IMPL(Int32, serialboxInt32_t, serialboxArrayOfInt32_t, int);
173 SERIALBOX_METAINFO_ADD_IMPL(Int64, serialboxInt64_t, serialboxArrayOfInt64_t, std::int64_t);
174 SERIALBOX_METAINFO_ADD_IMPL(Float32, serialboxFloat32_t, serialboxArrayOfFloat32_t, float);
175 SERIALBOX_METAINFO_ADD_IMPL(Float64, serialboxFloat64_t, serialboxArrayOfFloat64_t, double);
176 
177 #undef SERIALBOX_METAINFO_ADD_IMPL
178 
179 int serialboxMetainfoAddString(serialboxMetainfo_t* metaInfo, const char* key, const char* value) {
180  MetainfoMap* map = toMetainfoMap(metaInfo);
181  return static_cast<int>(map->insert(key, std::string(value)));
182 }
183 
184 int serialboxMetainfoAddArrayOfString(serialboxMetainfo_t* metaInfo, const char* key,
185  const serialboxArrayOfString_t* array) {
186  MetainfoMap* map = toMetainfoMap(metaInfo);
187  serialbox::Array<std::string> vec(array->len);
188  for(std::size_t i = 0; i < vec.size(); ++i)
189  vec[i] = array->data[i];
190  return static_cast<int>(map->insert(key, vec));
191 }
192 
193 /*===------------------------------------------------------------------------------------------===*\
194  * Query meta-information
195 \*===------------------------------------------------------------------------------------------===*/
196 
197 #define SERIALBOX_METAINFO_GET_IMPL(name, serialboxType, CXXType) \
198  serialboxType serialboxMetainfoGet##name(const serialboxMetainfo_t* metaInfo, const char* key) { \
199  const MetainfoMap* map = toConstMetainfoMap(metaInfo); \
200  serialboxType value = 0; \
201  try { \
202  value = map->as<CXXType>(key); \
203  } catch(std::exception & e) { \
204  serialboxFatalError(e.what()); \
205  } \
206  return value; \
207  }
208 
209 SERIALBOX_METAINFO_GET_IMPL(Boolean, serialboxBoolean_t, bool);
210 SERIALBOX_METAINFO_GET_IMPL(Int32, serialboxInt32_t, int);
211 SERIALBOX_METAINFO_GET_IMPL(Int64, serialboxInt64_t, std::int64_t);
212 SERIALBOX_METAINFO_GET_IMPL(Float32, serialboxFloat32_t, float);
213 SERIALBOX_METAINFO_GET_IMPL(Float64, serialboxFloat64_t, double);
214 
215 #undef SERIALBOX_METAINFO_GET_IMPL
216 
217 serialboxString_t serialboxMetainfoGetString(const serialboxMetainfo_t* metaInfo, const char* key) {
218  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
219  char* value = NULL;
220  try {
221  // Deep copy the string
222  auto valueStr = map->as<std::string>(key);
223  value = allocateAndCopyString(valueStr);
224  } catch(std::exception& e) {
225  serialboxFatalError(e.what());
226  }
227  return value;
228 }
229 
230 #define SERIALBOX_METAINFO_GET_ARRAY_IMPL(name, serialboxPrimitveType, serialboxArrayType, \
231  CXXType) \
232  serialboxArrayType* serialboxMetainfoGetArrayOf##name(const serialboxMetainfo_t* metaInfo, \
233  const char* key) { \
234  const MetainfoMap* map = toConstMetainfoMap(metaInfo); \
235  serialboxArrayType* array = NULL; \
236  try { \
237  auto value = map->as<serialbox::Array<CXXType>>(key); \
238  array = allocate<serialboxArrayType>(); \
239  array->len = (int)value.size(); \
240  array->data = allocate<serialboxPrimitveType>(array->len * sizeof(serialboxPrimitveType)); \
241  \
242  for(std::size_t i = 0; i < value.size(); ++i) \
243  array->data[i] = value[i]; \
244  } catch(std::exception & e) { \
245  serialboxFatalError(e.what()); \
246  } \
247  return array; \
248  }
249 
250 SERIALBOX_METAINFO_GET_ARRAY_IMPL(Boolean, serialboxBoolean_t, serialboxArrayOfBoolean_t, bool);
251 SERIALBOX_METAINFO_GET_ARRAY_IMPL(Int32, serialboxInt32_t, serialboxArrayOfInt32_t, int);
252 SERIALBOX_METAINFO_GET_ARRAY_IMPL(Int64, serialboxInt64_t, serialboxArrayOfInt64_t, std::int64_t);
253 SERIALBOX_METAINFO_GET_ARRAY_IMPL(Float32, serialboxFloat32_t, serialboxArrayOfFloat32_t, float);
254 SERIALBOX_METAINFO_GET_ARRAY_IMPL(Float64, serialboxFloat64_t, serialboxArrayOfFloat64_t, double);
255 
256 serialboxArrayOfString_t* serialboxMetainfoGetArrayOfString(const serialboxMetainfo_t* metaInfo,
257  const char* key) {
258  const MetainfoMap* map = toConstMetainfoMap(metaInfo);
259  serialboxArrayOfString_t* array = NULL;
260  try {
261  auto value = map->as<serialbox::Array<std::string>>(key);
262  array = allocate<serialboxArrayOfString_t>();
263  array->len = (int)value.size();
264  array->data = allocate<serialboxString_t>(array->len * sizeof(serialboxString_t));
265 
266  for(std::size_t i = 0; i < value.size(); ++i)
267  array->data[i] = allocateAndCopyString(value[i]);
268  } catch(std::exception& e) {
269  serialboxFatalError(e.what());
270  }
271  return array;
272 }
273 
274 #undef SERIALBOX_METAINFO_GET_ARRAY_IMPL
Array of booleans.
Definition: Array.h:40
SERIALBOX_API int serialboxMetainfoDeleteKey(serialboxMetainfo_t *metaInfo, const char *key)
Delete element given by key
Definition: Metainfo.cpp:148
serialboxMetainfo_t * serialboxMetainfoCreateFromMetainfo(const serialboxMetainfo_t *other)
Copy construct the meta-information.
Definition: Metainfo.cpp:38
SERIALBOX_API typedef int serialboxBoolean_t
Boolean type of serialbox.
Definition: Type.h:41
void serialboxMetainfoDestroyElementInfo(serialboxMetainfoElementInfo_t *elementInfo)
Destroy the element-information and deallocate all memory.
Definition: Metainfo.cpp:133
SERIALBOX_API typedef int64_t serialboxInt64_t
64-bit integral type of serialbox
Definition: Type.h:51
int serialboxMetainfoHasKey(const serialboxMetainfo_t *metaInfo, const char *key)
Check if and element with key key exists.
Definition: Metainfo.cpp:86
SERIALBOX_API typedef int32_t serialboxInt32_t
32-bit integral type of serialbox
Definition: Type.h:46
std::vector< T > Array
Array class used by serialbox to store meta-information.
Definition: Array.h:31
void serialboxMetainfoDestroy(serialboxMetainfo_t *metaInfo)
Destroy the meta-information and deallocate all memory.
Definition: Metainfo.cpp:52
int serialboxMetainfoGetSize(const serialboxMetainfo_t *metaInfo)
Get number of elemenets in the meta-information.
Definition: Metainfo.cpp:65
Refrence to a Metainfo.
Definition: Type.h:91
Array of strings.
Definition: Array.h:80
serialboxTypeID serialboxMetainfoGetTypeIDOfKey(const serialboxMetainfo_t *metaInfo, const char *key)
Get TypeID of element with key key
Definition: Metainfo.cpp:91
SERIALBOX_API typedef char * serialboxString_t
String type of serialbox.
Definition: Type.h:66
Data-structure to query the info of the elements (keys and corresponding types) in the meta-informati...
Definition: Metainfo.h:136
void serialboxMetainfoClear(serialboxMetainfo_t *metaInfo)
All the elements in the Metainfo are dropped: their destructors are called, and they are removed from...
Definition: Metainfo.cpp:75
serialbox::Exception exception
Exception class which stores a human-readable error description.
Definition: Exception.h:33
SERIALBOX_API typedef float serialboxFloat32_t
32-bit floating point type of serialbox (float)
Definition: Type.h:56
int serialboxMetainfoEqual(const serialboxMetainfo_t *m1, const serialboxMetainfo_t *m2)
Check if Metainfo m1 is equal to Metainfo m2
Definition: Metainfo.cpp:80
SERIALBOX_API typedef double serialboxFloat64_t
64-bit floating point type of serialbox (double)
Definition: Type.h:61
Array of 32-bit integers.
Definition: Array.h:48
int serialboxMetainfoIsEmpty(const serialboxMetainfo_t *metaInfo)
Check if meta information is empty.
Definition: Metainfo.cpp:70
void serialboxFatalError(const char *reason)
Report a fatal error.
char * serialboxMetainfoToString(const serialboxMetainfo_t *metaInfo)
Convert to string.
Definition: Metainfo.cpp:103
Array of 32-bit floating point numbers (float)
Definition: Array.h:64
Array of 64-bit floating point numbers (double)
Definition: Array.h:72
Array of 64-bit integers.
Definition: Array.h:56
serialboxMetainfoElementInfo_t * serialboxMetainfoCreateElementInfo(const serialboxMetainfo_t *metaInfo)
Allocate and intialize the element-info.
Definition: Metainfo.cpp:111
serialboxTypeID
Type-id of types recognized by serialbox.
Definition: Type.h:116
serialboxMetainfo_t * serialboxMetainfoCreate(void)
Construct an empty meta-information.
Definition: Metainfo.cpp:25