| 1 | Example to illustrate how to make a class persistent | 
|---|
| 2 | using SOPHYA PPF services | 
|---|
| 3 | ----------------------------------------------- | 
|---|
| 4 | (C) LAL-IN2P3/CNRS   (C) DAPNIA-SPP/CEA | 
|---|
| 5 | R. Ansari , 2007 | 
|---|
| 6 | ----------------------------------------------- | 
|---|
| 7 |  | 
|---|
| 8 | 0/ Content : | 
|---|
| 9 | - vfs.h : Declaration of the Vfs class Vfs . This is the class | 
|---|
| 10 | which we want to make persistent using SOPHYA PPF services | 
|---|
| 11 | - vfs.cc : the Vfs class implementation AND its PPF handler ObjFileIO<Vfs> | 
|---|
| 12 | - tvfs.cc : the test main program | 
|---|
| 13 | - makefile : compiles tvfs.cc and vfs.cc and links them to build tvfs | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | 1/ The class should inherit from the SOPHYA::AnyDataObj class. | 
|---|
| 17 | In the example here, we want to make the class Vfs persistent | 
|---|
| 18 | class Vfs : public SOPHYA::AnyDataObj { | 
|---|
| 19 | ... | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | 2/ A PPF handler should be implemented . The PPF handler class | 
|---|
| 23 | should inherit from SOPHYA::PPersist . The pure virtual methods | 
|---|
| 24 | of PPersist class (DataObj() , SetDataObj() , ReadSelf(), WriteSelf() | 
|---|
| 25 | has to be implemented. | 
|---|
| 26 |  | 
|---|
| 27 | class PPFHandlerVfs : public SOPHYA::PPersist { | 
|---|
| 28 | public: | 
|---|
| 29 | virtual SOPHYA::AnyDataObj* DataObj(); | 
|---|
| 30 | virtual void       SetDataObj(SOPHYA::AnyDataObj &); | 
|---|
| 31 | protected: | 
|---|
| 32 | virtual void       ReadSelf(SOPHYA::PInPersist&); | 
|---|
| 33 | virtual void       WriteSelf(SOPHYA::POutPersist&) const; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | It is possible to use the template class SOPHYA::ObjFileIO<T>, and | 
|---|
| 38 | specialize it for the tharget class (here SOPHYA::ObjFileIO<Vfs>), | 
|---|
| 39 | by defining the two methods : | 
|---|
| 40 | SOPHYA::ObjFileIO<Vfs>::ReadSelf(SOPHYA::PInPersist&) | 
|---|
| 41 | SOPHYA::ObjFileIO<Vfs>::WriteSelf(SOPHYA::POutPersist&) const | 
|---|
| 42 |  | 
|---|
| 43 | 3/ If it is NOT possible to have the target class inherit from AnyDataObj, | 
|---|
| 44 | a wrapper class should be used. The same class can play the role of wrapper | 
|---|
| 45 | AND the PPF handler. | 
|---|
| 46 | (See the PPF handler / wrapper class for STL vectors : | 
|---|
| 47 | SOPHYA::PPFWrapperSTLVector<T> in file BaseTools/ppfwrapstlv.h ) | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | 4/ Implement the ReadSelf() and WriteSelf() methods of the PPF handler. | 
|---|
| 51 | All the I/O services from the following classes can be used : | 
|---|
| 52 | - PPFBinaryIOStrem , PPFBinaryInputStream , PInPersist | 
|---|
| 53 | - PPFBinaryIOStrem , PPFBinaryOutputStream , POutPersist | 
|---|
| 54 | Writing and reading of the embeded objects for which a handler has been registered | 
|---|
| 55 | can simply be performed by : | 
|---|
| 56 | POutPersist::PutObject()   PInPersist::GetObject() | 
|---|
| 57 |  | 
|---|
| 58 | ==> The services associated with nametags (PPFBinaryOutputStream::WriteNameTag() | 
|---|
| 59 | PPFBinaryInputStream::GotoNameTag() ... ) are NOT intented to be used in | 
|---|
| 60 | WriteSelf() , ReadSelf() | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | 5/ The new PPF handler, as well as the list of classes it can handle has to be | 
|---|
| 64 | registered prior to use PPF read/write for the target classes. This must be | 
|---|
| 65 | performed during the initialization phase, for example at the beginning of the | 
|---|
| 66 | main() program. Another possibility is to use a module initializer | 
|---|
| 67 | (See SophyaInitiator class in file BaseTools/sophyainit.h ) | 
|---|
| 68 | and declare a static instance of the class (Note that this works only if | 
|---|
| 69 | the system loader handles correcly the call of constructor | 
|---|
| 70 | for the statically declared objects) | 
|---|
| 71 |  | 
|---|
| 72 | The registration can be performed using the CPP macros defined in BaseTools/ppersist.h | 
|---|
| 73 |  | 
|---|
| 74 | // First, register the PPF handler ObjFileIO<Vfs> | 
|---|
| 75 | PPRegister(ObjFileIO<Vfs>); | 
|---|
| 76 | // Register the list of classes which can be handled by ObjFileIO<Vfs> | 
|---|
| 77 | DObjRegister(ObjFileIO<Vfs>, Vfs); | 
|---|