|

Persistent Pointer Factory
|
|
|
|
|
|
|
This
product gives you a smart pointer class, PersistPtr
If
you replace all pointers by this pointer, your data become instantly
persistent (C++ Persistence). This is a simple, fast, and elegant implementation.
The total source is under 100kB, the binary version 35kB. It is also
simple to use, the entire documentation consists of only 10 pages.
Objects
and arrays of each class are stored in one disk file, and paged
to memory on demand. There is no limit on the size of your data
except for the available space on your disk drive. You control the
size of one page, and how many pages you want in memory, as you
can see from the following code sample:
|
|
#include "factory.h" // definition of the smart pointer
class A {
PersistClass(A); // register class A as persistent
int a;
public:
void prt(){cout << a << "\n";}
// ...
};
class B {
PersistClass(B); // register class B as persistent
PersistPtr<A> p; // equivalent of A *p;
public:
void prt(){if(p==NULL)return; else p->prt();}
// ...
};
int main(){
A::startPager(1024,5,2000000); // pageSize=1024, max=5 pages, estim=2MB
B::startPager(64,1,256); // pageSize=64, max=1 page, estim=256B
// ...
PersistPtr<B> pb=new B;
PersistPtr<A> pa=new A;
// ...
pb.freeObj(); // equivalent of delete pb;
pa.freeObj(); // equivalent of delete pb;
// ...
A::closePager();
B::closePager();
return 0;
}
|
|
| Note
that when starting the Pager (storage to disk), you must specify the
size of one page, and how many pages will be in the memory. You also
can provide an estimate of the entire space required for this class.
Providing a good estimate is not necessary, but it helps to avoid
gradual growing and repeated re-allocation of the internal data structures.
Also
note that removal of the objects must be done differently than expected;
operator delete() cannot be used, because pa and pb are objects,
not pointers. Also, you cannot use delete &pb in order to delete
object of type B, because then there would not be distinction between
destroying B and destroying PersistPtr<B> itself.
The
call pa.freeObj() provides essentially the same service as you would
expect from (syntactically incorrect) call: delete pa; Here is what
happens in the background: Each class keeps a list of free objects,
and if the data file can be truncated, these objects are released.
The
data is kept on file *.ppf, so after running the code fragment shown
above, you will have on your disk files A.ppf and B.ppf. When running
the same program again, startPager() opens the same file again.
You can chose the same, or completely different paging parameters.
|
|