devxlogo

How to Create Persistent Objects

How to Create Persistent Objects

ames, distributed database systems, multimedia and graphic applications use persistent objects extensively. Yet presently, C++ doesn’t support persistence directly (there are proposals for adding persistence and reflection to C++ in the future). Persistence support is not as trivial as it may seem at first. The size and memory layout of the same object may vary from one platform to another. Different byte ordering, or endian-ness, complicate matters even further.

In this article, I will show how to implement persistence without resorting to third-party frameworks such as DCOM and CORBA. For small and portable applications, this is an effective and satisfactory solution.

To make an object persistent, we have to reserve its state in a non-volatile storage device. Consider an application that records and plays MP3 files. Every clip is represented as an object that contains the title, album, performer, time, bit-rate, recording date and the matching MP3 file. The application displays recently played tracks on the track list.



We need to to implement object persistence without resorting to third-party frameworks such as DCOM and CORBA.



Write a persistent object to retain its state outside the scope of the program in which it was created.

Step 1: Serializing Built-in Datatypes
Ultimately, every object consists of built-in data members such as int, bool, char [] and so on. Our first exercise is to write such datatypes to an ofstream (read more on file I/O here). We store the values in their binary format. For this purpose, we use the write() and read() member functions. write() takes the address and the size of a variable and writes its bit pattern to a file stream. read() takes two arguments of type char * and long which contain buffer’s address and its size in bytes, respectively. The following example demonstrates how to store two integers in an ofstream:
#include <:fstream>:using namespace std;int main(){ int x,y; //mouse coordinates //..assign values to x and y ofstream archive(“coord.dat”, ios::binary); archive.write(reinterpret_cast(&x), sizeof (x)); archive.write(reinterpret_cast(&x), sizeof (x)); archive.close();}

The use of reinterpret_cast is necessary because write() takes const char * as its first argument whereas &x and &y are of type int *.

We retrieve the previously stored values as follows:

#include using namespace std;int main(){ int x,y; ifstream archive(“coord.dat”); archive.read((reinterpret_cast(&x), sizeof(x)); archive.read((reinterpret_cast(&y), sizeof(y));}
Step 2: Serializing Objects
To serialize a complete object, we write its data members to a file:

class MP3_clip{private: std::time_t date; std::string name; int bitrate; bool stereo;public: void serialize(); void deserialize(); //..};void MP3_clip::serialize(){{int size=name.size();// store name’s length  //empty file if it already exists before writing new data ofstream arc(“mp3.dat”, ios::binary|ios::trunc); arc.write(reinterpret_cast(&date),sizeof(date)); arc.write(reinterpret_cast(&size),sizeof(size)); arc.write(name.c_str(), size+1); // write final ‘

devx-admin

Share the Post: