| [269] | 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
 | 2 | // Classe de gestion des I/O fichiers des objets 
 | 
|---|
 | 3 | //                          R.Ansari          04/99
 | 
|---|
 | 4 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | #ifndef OBJFILEIO_H_SEEN
 | 
|---|
 | 7 | #define OBJFILEIO_H_SEEN
 | 
|---|
 | 8 | 
 | 
|---|
 | 9 | #include "machdefs.h"
 | 
|---|
| [1981] | 10 | #include <stdio.h>
 | 
|---|
| [269] | 11 | #include "anydataobj.h"
 | 
|---|
 | 12 | #include "ppersist.h"
 | 
|---|
 | 13 | 
 | 
|---|
| [2805] | 14 | /*!
 | 
|---|
 | 15 |    \class SOPHYA::ObjFileIO
 | 
|---|
 | 16 |    \ingroup BaseTools
 | 
|---|
 | 17 |    Template class to ease implementation of PPersist handlers for a 
 | 
|---|
 | 18 |    class T , inheriting from AnyDataObj. The methodes ReadSelf() and 
 | 
|---|
 | 19 |    WriteSelf() must be implemented.
 | 
|---|
 | 20 |    \sa PPersist
 | 
|---|
 | 21 | */
 | 
|---|
 | 22 | 
 | 
|---|
| [552] | 23 | namespace SOPHYA {
 | 
|---|
| [269] | 24 | 
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | template <class T>
 | 
|---|
 | 27 | class ObjFileIO : public PPersist {
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | public :
 | 
|---|
| [2698] | 30 |             ObjFileIO() { dobj=NULL; ownobj=true; }
 | 
|---|
| [277] | 31 |             ObjFileIO(T * obj) { dobj = obj; ownobj=false; }
 | 
|---|
 | 32 |   virtual   ~ObjFileIO() { if (ownobj && dobj) delete dobj; }
 | 
|---|
| [269] | 33 | 
 | 
|---|
 | 34 |   virtual   AnyDataObj* DataObj() { return(dobj); }
 | 
|---|
| [754] | 35 |   virtual   void        SetDataObj(AnyDataObj & o) 
 | 
|---|
| [1967] | 36 |   {  
 | 
|---|
 | 37 |     T * po = dynamic_cast< T * >(& o); 
 | 
|---|
 | 38 |     if (po == NULL)  {
 | 
|---|
 | 39 |       char buff[160];
 | 
|---|
 | 40 |       sprintf(buff,"ObjFileIO<T>::SetDataObj(%s) - Object type  error ! ",
 | 
|---|
 | 41 |               typeid(o).name());
 | 
|---|
 | 42 |       throw TypeMismatchExc(PExcLongMessage(buff));    
 | 
|---|
 | 43 |     }
 | 
|---|
 | 44 |     if (ownobj && dobj) delete dobj;  dobj = po; ownobj = false; 
 | 
|---|
 | 45 |   }    
 | 
|---|
 | 46 |   
 | 
|---|
| [2698] | 47 |   inline operator T&() { return(*dobj); }
 | 
|---|
| [269] | 48 | 
 | 
|---|
 | 49 | protected :
 | 
|---|
 | 50 |   virtual void       ReadSelf(PInPersist&);           
 | 
|---|
 | 51 |   virtual void       WriteSelf(POutPersist&) const;  
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |   T * dobj;
 | 
|---|
| [277] | 54 |   bool ownobj;       // True si dobj obtenu par new
 | 
|---|
| [269] | 55 | };
 | 
|---|
 | 56 | 
 | 
|---|
| [277] | 57 | /*
 | 
|---|
 | 58 | template <class T>
 | 
|---|
 | 59 | inline POutPersist& operator << (POutPersist& os, T const & obj)
 | 
|---|
 | 60 | { ObjFileIO<T> fio((const_cast)&obj);  fio.Write(os);  return(os); } 
 | 
|---|
| [269] | 61 | 
 | 
|---|
| [277] | 62 | template <class T>
 | 
|---|
 | 63 | inline PInPersist& operator >> (PInPersist& is, T & obj)
 | 
|---|
| [2477] | 64 | { ObjFileIO<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }  
 | 
|---|
| [277] | 65 | */
 | 
|---|
| [269] | 66 | 
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | } // Fin namespace
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | #endif
 | 
|---|