Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Savepoint.cpp
1 /*===-- serialbox-c/Savepoint.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 Savepoint.
12  *
13 \*===------------------------------------------------------------------------------------------===*/
14 
15 #include "serialbox-c/Savepoint.h"
16 #include "serialbox-c/Utility.h"
17 
18 using namespace serialboxC;
19 
20 /*===------------------------------------------------------------------------------------------===*\
21  * Construction & Destruction
22 \*===------------------------------------------------------------------------------------------===*/
23 
25  serialboxSavepoint_t* savepoint = allocate<serialboxSavepoint_t>();
26  try {
27  savepoint->impl = new Savepoint(name);
28  savepoint->ownsData = 1;
29  } catch(std::exception& e) {
30  std::free(savepoint);
31  savepoint = NULL;
32  serialboxFatalError(e.what());
33  }
34  return savepoint;
35 }
36 
38  const Savepoint* sp = toConstSavepoint(other);
39  serialboxSavepoint_t* savepoint = allocate<serialboxSavepoint_t>();
40  try {
41  savepoint->impl = new Savepoint(*sp);
42  savepoint->ownsData = 1;
43  } catch(std::exception& e) {
44  serialboxFatalError(e.what());
45  }
46  return savepoint;
47 }
48 
50  if(savepoint) {
51  Savepoint* sp = toSavepoint(savepoint);
52  if(savepoint->ownsData)
53  delete sp;
54  std::free(savepoint);
55  }
56 }
57 
58 /*===------------------------------------------------------------------------------------------===*\
59  * Utility
60 \*===------------------------------------------------------------------------------------------===*/
61 
62 const char* serialboxSavepointGetName(const serialboxSavepoint_t* savepoint) {
63  const Savepoint* sp = toConstSavepoint(savepoint);
64  return sp->name().c_str();
65 }
66 
68  const Savepoint* sp1 = toConstSavepoint(s1);
69  const Savepoint* sp2 = toConstSavepoint(s2);
70  return ((*sp1) == (*sp2));
71 }
72 
74  const Savepoint* sp = toConstSavepoint(savepoint);
75  return allocateAndCopyString(sp->toString());
76 }
77 
78 size_t serialboxSavepointHash(const serialboxSavepoint_t* savepoint) {
79  const Savepoint* sp = toConstSavepoint(savepoint);
80  return std::hash<Savepoint>()(*sp);
81 }
82 
83 /*===------------------------------------------------------------------------------------------===*\
84  * Meta-information
85 \*===------------------------------------------------------------------------------------------===*/
86 
88  Savepoint* sp = toSavepoint(savepoint);
89  serialboxMetainfo_t* metaInfo = allocate<serialboxMetainfo_t>();
90  metaInfo->impl = sp->metaInfoPtr().get();
91  metaInfo->ownsData = 0;
92  return metaInfo;
93 }
const char * serialboxSavepointGetName(const serialboxSavepoint_t *savepoint)
Get the name of the Savepoint.
Definition: Savepoint.cpp:62
int serialboxSavepointEqual(const serialboxSavepoint_t *s1, const serialboxSavepoint_t *s2)
Check if Savepoint s1 is equal to savepoint s2
Definition: Savepoint.cpp:67
Refrence to a Metainfo.
Definition: Type.h:91
void serialboxSavepointDestroy(serialboxSavepoint_t *savepoint)
Destroy the savepoint and deallocate all memory.
Definition: Savepoint.cpp:49
serialbox::Exception exception
Exception class which stores a human-readable error description.
Definition: Exception.h:33
serialboxSavepoint_t * serialboxSavepointCreate(const char *name)
Construct a Savepoint.
Definition: Savepoint.cpp:24
void serialboxFatalError(const char *reason)
Report a fatal error.
serialboxMetainfo_t * serialboxSavepointGetMetainfo(serialboxSavepoint_t *savepoint)
Allocate a new serialboxMetainfo_t which maps to the meta-information of the Savepoint.
Definition: Savepoint.cpp:87
Refrence to a Savepoint.
Definition: Type.h:83
serialboxSavepoint_t * serialboxSavepointCreateFromSavepoint(const serialboxSavepoint_t *other)
Copy construct the Savepoint.
Definition: Savepoint.cpp:37
char * serialboxSavepointToString(const serialboxSavepoint_t *savepoint)
Convert to string.
Definition: Savepoint.cpp:73
size_t serialboxSavepointHash(const serialboxSavepoint_t *savepoint)
Compute hash of the Savepoint.
Definition: Savepoint.cpp:78