The main routine creates the container and a struct, decorates the struct with the CkptSimpleStructWrapper object, sets the object's key, and then performs the typical CRUD (Create, Retrieve, Update, Destroy) operations. This example does not contain proper error checking for the sake of brevity.
#include <string> #include <CkptObject.h> #include <CkptContainer.h>
struct existingStruct { int i; long l; char c; unsigned short s; };
// Invoke the SimpleStructWrapper macro to define a type named WrappedEStruct // that is auto-registered and can be used with CkptContainer. CkptSimpleStructWrapper(existingStruct, WrappedEStruct)
int main() { unsigned long MaxKeys = 256; unsigned long MaxObjectSize = sizeof(WrappedEStruct);
// Create a container for objects of type WrappedEStruct CkptSimpleContainer <WrappedEStruct> *eContainer; eContainer = new CkptSimpleContainer<WrappedEStruct>("MyContainerName", MaxKeys, MaxObjectSize);
// Create an instance of our wrapper class and we can use it like a pointer to // the structure it wraps. WrappedEStruct wes; wes->i = 42; wes->c = 'a'; wes->l = 1024*1024; wes->s = 12345;
// Set a key for the struct wes.setKey("wes1");
// Persist the object eContainer->persist(&wes);
// Do other stuff... // ...
// Retrieve an object by key CkptObjectKey ptKey("wes1"); WrappedEStruct *objPtr = eContainer->get(ptKey);
// The tricky thing here is that objPtr points to the Wrapper // Accessing it via -> wouldn't get us to the underlying struct, we have to use // (*objPtr) to change the object's data (*objPtr)->i = 420;
// Update (commit) the changed object eContainer->commit(objPtr); // OR, if you prefer: objPtr->commit();
// Remove the object from the container (Destroy the data in the replica) eContainer->remove(objPtr);
// The object isn't in the container, but we still have our snapshot copy // from the call to get() // Free up the memory associated with our copy of the object delete objPtr;
return 0; }