source: Sophya/trunk/SophyaLib/BaseTools/ppfwrapstlv.h@ 2760

Last change on this file since 2760 was 2665, checked in by ansari, 20 years ago

Petites correction suite compilation sur SGI-CC , Reza 7/4/2005

File size: 3.4 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// Classe pour la permettre la persistance PPF des vecteurs STL
3// R. Ansari - Avril 2005
4// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
5
6#ifndef PPFWRAPSTLV_H
7#define PPFWRAPSTLV_H
8
9#include "machdefs.h"
10#include "anydataobj.h"
11#include "ppersist.h"
12#include "pexceptions.h"
13#include <vector>
14#include <iostream>
15
16namespace SOPHYA {
17
18/*!
19 \class SOPHYA::PPFWrapperSTLVector
20 \ingroup BaseTools
21 Class for managing PPF persistence of standard (STL) vectors.
22 Only the overloaded operators I/O operators on POutPersist
23 and PInPersist streams are intended to be used in user programs.
24 When applied to vectors of user defined types and classes (\b T), the
25 c++ I/O operators POutPersist& << and PInPersist& >> should be
26 defined for the type \b T and the resulting handler must be registered
27 using the macro \b PPRegister(PPFWrapperSTLVector<T>)
28*/
29
30template <class T>
31class PPFWrapperSTLVector : public PPersist , public AnyDataObj {
32public:
33 PPFWrapperSTLVector()
34 {
35 dobj = new std::vector<T> ;
36 ownobj=true;
37 }
38
39 // PPFWrapperSTLVector(string const & filename);
40 PPFWrapperSTLVector(std::vector<T> & obj)
41 {
42 dobj = &obj;
43 ownobj = false;
44 }
45
46 PPFWrapperSTLVector(std::vector<T> * obj)
47 {
48 if (obj == NULL)
49 throw ParmError("PPFWrapperSTLVector<T>::PPFWrapperSTLVector(* obj) obj=NULL (ppfwrapstlv.h)");
50 dobj = obj;
51 ownobj = false;
52 }
53 virtual ~PPFWrapperSTLVector()
54 {
55 if (ownobj && dobj) delete dobj;
56 }
57 virtual AnyDataObj* DataObj() { return this; }
58 virtual void SetDataObj(AnyDataObj & o)
59 {
60 throw NotAvailableOperation("PPFWrapperSTLVector<T>::SetDataObj() Not permitted ! (ppfwrapstlv.h)");
61 }
62 inline operator std::vector<T>() { return(*dobj); }
63
64protected :
65 virtual void ReadSelf(PInPersist& is)
66 {
67 if (dobj == NULL)
68 throw ParmError("PPFWrapperSTLVector<T>::ReadSelf() dobj=NULL (ppfwrapstlv.h)");
69 // On lit les 3 premiers uint_8
70 uint_8 itab[3];
71 is.Get(itab, 3);
72 // On efface le contenu du vecteur si necessaire - (on peut faire plus efficacement ...)
73 if ( dobj->size() > 0) dobj->erase(dobj->begin(), dobj->end());
74 T el;
75 for(uint_8 k=0; k<itab[1]; k++) {
76 is >> el;
77 dobj->push_back(el);
78 }
79 }
80 virtual void WriteSelf(POutPersist& os) const
81 {
82 if (dobj == NULL)
83 throw ParmError("PPFWrapperSTLVector<T>::WriteSelf() dobj=NULL (ppfwrapstlv.h)");
84 // On ecrit 3 uint_8
85 // 0 : Numero de version = 1 : Taille, 2 reserve a l
86 uint_8 itab[3];
87 itab[0] = 1;
88 itab[1] = dobj->size();
89 itab[2] = 0;
90 os.Put(itab, 3);
91 // On ecrit le vecteur de donnees
92 for(uint_8 k=0; k<itab[1]; k++) os << (*dobj)[k];
93 }
94
95 // Variables membres
96 std::vector<T> * dobj; // Le vecteur de la STL
97 bool ownobj; // true -> objet cree par le wrapper
98};
99
100/*! Writes the STL vector object in the POutPersist stream \b os */
101template <class T>
102inline POutPersist& operator << (POutPersist& os, std::vector<T> & obj)
103{ PPFWrapperSTLVector<T> fio(&obj); fio.Write(os); return(os); }
104/*! Reads in and initializes the STL vector object from the PInPersist stream \b is */
105template <class T>
106inline PInPersist& operator >> (PInPersist& is, std::vector<T> & obj)
107{ PPFWrapperSTLVector<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
108
109} // Fin du namespace
110
111#endif
Note: See TracBrowser for help on using the repository browser.