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

Last change on this file since 1967 was 1967, checked in by ansari, 23 years ago

Ajout throw TypeMismatchExc ds PPersist_X::SetDataObj() ds les differents classes handler PPersist, a place du return simple, lorsque le type d'objet n'etait pas bon ... Reza 26/4/2002

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