| 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" | 
|---|
| 10 | #include <stdio.h> | 
|---|
| 11 | #include "anydataobj.h" | 
|---|
| 12 | #include "ppersist.h" | 
|---|
| 13 |  | 
|---|
| 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 |  | 
|---|
| 23 | namespace SOPHYA { | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | template <class T> | 
|---|
| 27 | class ObjFileIO : public PPersist { | 
|---|
| 28 |  | 
|---|
| 29 | public : | 
|---|
| 30 | ObjFileIO() { dobj=NULL; ownobj=true; } | 
|---|
| 31 | ObjFileIO(T * obj) { dobj = obj; ownobj=false; } | 
|---|
| 32 | virtual   ~ObjFileIO() { if (ownobj && dobj) delete dobj; } | 
|---|
| 33 |  | 
|---|
| 34 | virtual   AnyDataObj* DataObj() { return(dobj); } | 
|---|
| 35 | virtual   void        SetDataObj(AnyDataObj & o) | 
|---|
| 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 |  | 
|---|
| 47 | inline operator T&() { return(*dobj); } | 
|---|
| 48 |  | 
|---|
| 49 | protected : | 
|---|
| 50 | virtual void       ReadSelf(PInPersist&); | 
|---|
| 51 | virtual void       WriteSelf(POutPersist&) const; | 
|---|
| 52 |  | 
|---|
| 53 | T * dobj; | 
|---|
| 54 | bool ownobj;       // True si dobj obtenu par new | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 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); } | 
|---|
| 61 |  | 
|---|
| 62 | template <class T> | 
|---|
| 63 | inline PInPersist& operator >> (PInPersist& is, T & obj) | 
|---|
| 64 | { ObjFileIO<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); } | 
|---|
| 65 | */ | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | } // Fin namespace | 
|---|
| 69 |  | 
|---|
| 70 | #endif | 
|---|