15 #include "serialbox-c/Metainfo.h" 16 #include "serialbox-c/Utility.h" 28 metaInfo->impl =
new MetainfoMap;
29 metaInfo->ownsData = 1;
39 const MetainfoMap* otherMap = toConstMetainfoMap(other);
42 metaInfo->impl =
new MetainfoMap(*otherMap);
43 metaInfo->ownsData = 1;
54 MetainfoMap* map = toMetainfoMap(metaInfo);
55 if(metaInfo->ownsData)
66 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
67 return (
int)map->size();
71 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
72 return (
int)map->empty();
76 MetainfoMap* map = toMetainfoMap(metaInfo);
81 const MetainfoMap* map1 = toConstMetainfoMap(m1);
82 const MetainfoMap* map2 = toConstMetainfoMap(m2);
83 return (*map1 == *map2);
87 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
88 return (
int)map->hasKey(key);
93 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
104 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
105 std::stringstream ss;
107 return allocateAndCopyString(ss.str());
112 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
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]);
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];
128 elements->len = (int)map->size();
136 for(
int i = 0; i < elementInfo->len; ++i)
137 std::free(elementInfo->keys[i]);
138 std::free(elementInfo->keys);
141 std::free(elementInfo->types);
144 std::free(elementInfo);
149 MetainfoMap* map = toMetainfoMap(metaInfo);
150 return map->erase(key);
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))); \ 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)); \ 177 #undef SERIALBOX_METAINFO_ADD_IMPL 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)));
186 MetainfoMap* map = toMetainfoMap(metaInfo);
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));
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; \ 202 value = map->as<CXXType>(key); \ 203 } catch(std::exception & e) { \ 204 serialboxFatalError(e.what()); \ 215 #undef SERIALBOX_METAINFO_GET_IMPL 218 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
222 auto valueStr = map->as<std::string>(key);
223 value = allocateAndCopyString(valueStr);
230 #define SERIALBOX_METAINFO_GET_ARRAY_IMPL(name, serialboxPrimitveType, serialboxArrayType, \ 232 serialboxArrayType* serialboxMetainfoGetArrayOf##name(const serialboxMetainfo_t* metaInfo, \ 234 const MetainfoMap* map = toConstMetainfoMap(metaInfo); \ 235 serialboxArrayType* array = NULL; \ 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)); \ 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()); \ 258 const MetainfoMap* map = toConstMetainfoMap(metaInfo);
262 array = allocate<serialboxArrayOfString_t>();
263 array->len = (int)value.size();
264 array->data = allocate<serialboxString_t>(array->len *
sizeof(
serialboxString_t));
266 for(std::size_t i = 0; i < value.size(); ++i)
267 array->data[i] = allocateAndCopyString(value[i]);
274 #undef SERIALBOX_METAINFO_GET_ARRAY_IMPL
SERIALBOX_API typedef int serialboxBoolean_t
Boolean type of serialbox.
SERIALBOX_API typedef int64_t serialboxInt64_t
64-bit integral type of serialbox
SERIALBOX_API typedef int32_t serialboxInt32_t
32-bit integral type of serialbox
std::vector< T > Array
Array class used by serialbox to store meta-information.
SERIALBOX_API typedef char * serialboxString_t
String type of serialbox.
SERIALBOX_API typedef float serialboxFloat32_t
32-bit floating point type of serialbox (float)
SERIALBOX_API typedef double serialboxFloat64_t
64-bit floating point type of serialbox (double)
Array of 32-bit integers.
void serialboxFatalError(const char *reason)
Report a fatal error.
Array of 32-bit floating point numbers (float)
Array of 64-bit floating point numbers (double)
Array of 64-bit integers.
serialboxTypeID
Type-id of types recognized by serialbox.