The main routine creates the container and an 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>
class CkptObjTest : public CkptObject {
public:
static const int CHAR_ARRAY_SIZE=32;
CkptObjTest() : CkptObject() { }
CkptObjTest(int i, std::string str) : CkptObject() {
intVar = i;
str_obj = str;
memcpy(charArray, "Blahblah\0", 9);
}
CkptObjTest(CkptObjTest const &rhs) {
intVar = rhs.intVar;
str_obj = rhs.str_obj;
memcpy(charArray, rhs.charArray, CHAR_ARRAY_SIZE);
}
CkptObjTest* clone() const { return new CkptObjTest(*this); }
virtual void serialize(DAOInterface &dao) {
dao & intVar;
dao.byteCopy(charArray, CHAR_ARRAY_SIZE);
}
COAL_DECLARE_CLASSNAME("CkptObjTest")
int intVar; std::string str_obj; char charArray[CHAR_ARRAY_SIZE];
};
COAL_REGISTER_TYPE(CkptObjTest)
int main() {
unsigned long MaxKeys = 1024;
unsigned long MaxObjectSize = sizeof(CkptObjTest);
// Create a container for objects of type CkptObjTest
CkptSimpleContainer <CkptObjTest> *otContainer;
otContainer = new CkptSimpleContainer<CkptObjTest>("MyContainerName", MaxKeys, MaxObjectSize);
// Create a new object and a key for it
CkptObjTest obj(42, "Hello world");
obj.setKey("obj1");
// Persist the object
otContainer->persist(&obj);
// Do other stuff...
// ...
// Retrieve an object by key
CkptObjectKey ptKey("obj1");
CkptObjTest *objPtr = otContainer->get(ptKey);
// Change the object's data
objPtr->intVar = 420;
// Update (commit) the changed object
otContainer->commit(objPtr);
// OR, if you prefer: objPtr->commit();
// Remove the object from the container (Destroy the data in the replica)
otContainer->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; }
1.5.2