Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
FieldMetainfo.cpp
1 /*===-- serialbox-c/FieldMetainfo.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 FieldMetainfoImpl.
12  *
13 \*===------------------------------------------------------------------------------------------===*/
14 
15 #include "serialbox-c/FieldMetainfo.h"
16 #include "serialbox-c/Utility.h"
17 
18 using namespace serialboxC;
19 
20 /*===------------------------------------------------------------------------------------------===*\
21  * Construction & Destruction
22 \*===------------------------------------------------------------------------------------------===*/
23 
25  int numDimensions) {
26  serialboxFieldMetainfo_t* fieldMetainfo = allocate<serialboxFieldMetainfo_t>();
27 
28  std::vector<int> dims(dimensions, dimensions + numDimensions);
29  try {
30  fieldMetainfo->impl = new FieldMetainfo(serialbox::TypeID((int)type), dims);
31  fieldMetainfo->ownsData = 1;
32  } catch(std::exception& e) {
33  std::free(fieldMetainfo);
34  fieldMetainfo = NULL;
35  serialboxFatalError(e.what());
36  }
37  return fieldMetainfo;
38 }
39 
42  const FieldMetainfo* otherInfo = toConstFieldMetainfo(other);
43  serialboxFieldMetainfo_t* fieldMetainfo = allocate<serialboxFieldMetainfo_t>();
44  try {
45  fieldMetainfo->impl = new FieldMetainfo(*otherInfo);
46  fieldMetainfo->ownsData = 1;
47  } catch(std::exception& e) {
48  std::free(fieldMetainfo);
49  fieldMetainfo = NULL;
50  serialboxFatalError(e.what());
51  }
52  return fieldMetainfo;
53 }
54 
56  if(fieldMetainfo) {
57  FieldMetainfo* info = toFieldMetainfo(fieldMetainfo);
58  if(fieldMetainfo->ownsData)
59  delete info;
60  std::free(fieldMetainfo);
61  }
62 }
63 
64 /*===------------------------------------------------------------------------------------------===*\
65  * Utility
66 \*===------------------------------------------------------------------------------------------===*/
67 
69  const serialboxFieldMetainfo_t* f2) {
70  const FieldMetainfo* info1 = toConstFieldMetainfo(f1);
71  const FieldMetainfo* info2 = toConstFieldMetainfo(f2);
72  return ((*info1) == (*info2));
73 }
74 
76  const FieldMetainfo* info = toConstFieldMetainfo(fieldMetainfo);
77  return allocateAndCopyString(info->toString());
78 }
79 
80 /*===------------------------------------------------------------------------------------------===*\
81  * Dimensions and TypeID
82 \*===------------------------------------------------------------------------------------------===*/
83 
85  const FieldMetainfo* info = toConstFieldMetainfo(fieldMetainfo);
86  return serialboxTypeID((int)info->type());
87 }
88 
90  const FieldMetainfo* info = toConstFieldMetainfo(fieldMetainfo);
91  return info->dims().data();
92 }
93 
95  const FieldMetainfo* info = toConstFieldMetainfo(fieldMetainfo);
96  return (int)info->dims().size();
97 }
98 
99 /*===------------------------------------------------------------------------------------------===*\
100  * Meta-information
101 \*===------------------------------------------------------------------------------------------===*/
102 
104  FieldMetainfo* info = toFieldMetainfo(fieldMetainfo);
105  serialboxMetainfo_t* metaInfo = allocate<serialboxMetainfo_t>();
106  metaInfo->impl = info->metaInfoPtr().get();
107  metaInfo->ownsData = 0;
108  return metaInfo;
109 }
void serialboxFieldMetainfoDestroy(serialboxFieldMetainfo_t *fieldMetainfo)
Destroy the field meta-information and deallocate all memory.
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
Refrence to a FieldMetainfoImpl.
Definition: Type.h:99
Refrence to a Metainfo.
Definition: Type.h:91
serialboxFieldMetainfo_t * serialboxFieldMetainfoCreateFromFieldMetainfo(const serialboxFieldMetainfo_t *other)
Copy construct the FieldMetainfoImpl.
serialboxMetainfo_t * serialboxFieldMetainfoGetMetainfo(serialboxFieldMetainfo_t *fieldMetainfo)
Allocate a new serialboxMetainfo_t which maps to the meta-information of the Field meta-information...
serialboxTypeID serialboxFieldMetainfoGetTypeID(const serialboxFieldMetainfo_t *fieldMetainfo)
Get type-id.
serialbox::Exception exception
Exception class which stores a human-readable error description.
Definition: Exception.h:33
serialboxFieldMetainfo_t * serialboxFieldMetainfoCreate(serialboxTypeID type, const int *dimensions, int numDimensions)
Construct a meta-information for a field.
const int * serialboxFieldMetainfoGetDimensions(const serialboxFieldMetainfo_t *fieldMetainfo)
Get dimensions.
int serialboxFieldMetainfoGetNumDimensions(const serialboxFieldMetainfo_t *fieldMetainfo)
Get number of dimensions.
void serialboxFatalError(const char *reason)
Report a fatal error.
int serialboxFieldMetainfoEqual(const serialboxFieldMetainfo_t *f1, const serialboxFieldMetainfo_t *f2)
Check if FieldMetainfoImpl f1 is equal to FieldMetainfoImpl f2
char * serialboxFieldMetainfoToString(const serialboxFieldMetainfo_t *fieldMetainfo)
Convert to string.
serialboxTypeID
Type-id of types recognized by serialbox.
Definition: Type.h:116