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 | namespace SOPHYA {
|
---|
15 |
|
---|
16 |
|
---|
17 | template <class T>
|
---|
18 | class ObjFileIO : public PPersist {
|
---|
19 |
|
---|
20 | public :
|
---|
21 | ObjFileIO() { dobj=new T; ownobj=true; }
|
---|
22 | ObjFileIO(string const & filename)
|
---|
23 | { dobj=new T; ownobj=true; Read(filename); }
|
---|
24 | ObjFileIO(const T & obj) { dobj = new T(obj); ownobj=true; }
|
---|
25 | ObjFileIO(T * obj) { dobj = obj; ownobj=false; }
|
---|
26 | virtual ~ObjFileIO() { if (ownobj && dobj) delete dobj; }
|
---|
27 |
|
---|
28 | virtual AnyDataObj* DataObj() { return(dobj); }
|
---|
29 | virtual void SetDataObj(AnyDataObj & o)
|
---|
30 | {
|
---|
31 | T * po = dynamic_cast< T * >(& o);
|
---|
32 | if (po == NULL) {
|
---|
33 | char buff[160];
|
---|
34 | sprintf(buff,"ObjFileIO<T>::SetDataObj(%s) - Object type error ! ",
|
---|
35 | typeid(o).name());
|
---|
36 | throw TypeMismatchExc(PExcLongMessage(buff));
|
---|
37 | }
|
---|
38 | if (ownobj && dobj) delete dobj; dobj = po; ownobj = false;
|
---|
39 | }
|
---|
40 |
|
---|
41 | inline operator T() { return(*dobj); }
|
---|
42 |
|
---|
43 | protected :
|
---|
44 | virtual void ReadSelf(PInPersist&);
|
---|
45 | virtual void WriteSelf(POutPersist&) const;
|
---|
46 |
|
---|
47 | T * dobj;
|
---|
48 | bool ownobj; // True si dobj obtenu par new
|
---|
49 | };
|
---|
50 |
|
---|
51 | /*
|
---|
52 | template <class T>
|
---|
53 | inline POutPersist& operator << (POutPersist& os, T const & obj)
|
---|
54 | { ObjFileIO<T> fio((const_cast)&obj); fio.Write(os); return(os); }
|
---|
55 |
|
---|
56 | template <class T>
|
---|
57 | inline PInPersist& operator >> (PInPersist& is, T & obj)
|
---|
58 | { ObjFileIO<T> fio(&obj); fio.Read(is); return(is); }
|
---|
59 | */
|
---|
60 |
|
---|
61 |
|
---|
62 | } // Fin namespace
|
---|
63 |
|
---|
64 | #endif
|
---|