source: Sophya/trunk/SophyaLib/BaseTools/objfio.h@ 2384

Last change on this file since 2384 was 1981, checked in by cmv, 23 years ago

include<stdio.h> needed by sprintf cmv 03/05/02

File size: 1.7 KB
Line 
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
14namespace SOPHYA {
15
16
17template <class T>
18class ObjFileIO : public PPersist {
19
20public :
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
43protected :
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/*
52template <class T>
53inline POutPersist& operator << (POutPersist& os, T const & obj)
54{ ObjFileIO<T> fio((const_cast)&obj); fio.Write(os); return(os); }
55
56template <class T>
57inline 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
Note: See TracBrowser for help on using the repository browser.