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

Last change on this file since 4051 was 4051, checked in by ansari, 14 years ago

modifs ds PPersist (classe PIOPersist) pour permettre l'enregistrement automatique (1ere lecture ou ecriture) de PPFWrapperSTLVector<T> (gestionnaire ppersist de std::vector<T> , Reza+cmv 25/02/2012

File size: 3.9 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
16/*!
17 \class SOPHYA::PPFWrapperSTLVector
18 \ingroup BaseTools
19 Class for managing PPF persistence of standard (STL) vectors.
20 Only the overloaded operators I/O operators on POutPersist
21 and PInPersist streams are intended to be used in user programs.
22 When applied to vectors of user defined types and classes (\b T), the
23 c++ I/O operators POutPersist& << and PInPersist& >> should be
24 defined for the type \b T and the resulting handler must be registered
25 using the macro \b PPRegister(PPFWrapperSTLVector<T>)
26*/
27
28namespace SOPHYA {
29
30
31template <class T>
32class PPFWrapperSTLVector : public PPersist , public AnyDataObj {
33public:
34 PPFWrapperSTLVector()
35 {
36 dobj = new std::vector<T> ;
37 ownobj=true;
38 }
39
40 // PPFWrapperSTLVector(string const & filename);
41 PPFWrapperSTLVector(std::vector<T> & obj)
42 {
43 dobj = &obj;
44 ownobj = false;
45 }
46
47 PPFWrapperSTLVector(std::vector<T> * obj)
48 {
49 if (obj == NULL)
50 throw ParmError("PPFWrapperSTLVector<T>::PPFWrapperSTLVector(* obj) obj=NULL (ppfwrapstlv.h)");
51 dobj = obj;
52 ownobj = false;
53 }
54 virtual ~PPFWrapperSTLVector()
55 {
56 if (ownobj && dobj) delete dobj;
57 }
58 virtual AnyDataObj* DataObj() { return this; }
59 virtual void SetDataObj(AnyDataObj & o)
60 {
61 throw NotAvailableOperation("PPFWrapperSTLVector<T>::SetDataObj() Not permitted ! (ppfwrapstlv.h)");
62 }
63 inline operator std::vector<T>() { return(*dobj); }
64
65 virtual void Write(POutPersist& os) const
66 {
67 if (!PIOPersist::checkPPClassId(*this)) { // on enregistre la classe
68 PPRegister(PPFWrapperSTLVector< T >);
69 DObjRegister(PPFWrapperSTLVector< T >, std::vector< T >);
70 }
71 PPersist::Write(os);
72 }
73 virtual void Read(PInPersist& os)
74 {
75 if (!PIOPersist::checkPPClassId(*this)) { // on enregistre la classe
76 PPRegister(PPFWrapperSTLVector< T >);
77 DObjRegister(PPFWrapperSTLVector< T >, std::vector< T >);
78 }
79 PPersist::Read(os);
80 }
81
82protected :
83 virtual void ReadSelf(PInPersist& is)
84 {
85 if (dobj == NULL)
86 throw ParmError("PPFWrapperSTLVector<T>::ReadSelf() dobj=NULL (ppfwrapstlv.h)");
87 // On lit les 3 premiers uint_8
88 uint_8 itab[3];
89 is.Get(itab, 3);
90 // On efface le contenu du vecteur si necessaire - (on peut faire plus efficacement ...)
91 if ( dobj->size() > 0) dobj->erase(dobj->begin(), dobj->end());
92 for(uint_8 k=0; k<itab[1]; k++) {
93 T el;
94 is >> el;
95 dobj->push_back(el);
96 }
97 }
98 virtual void WriteSelf(POutPersist& os) const
99 {
100 if (dobj == NULL)
101 throw ParmError("PPFWrapperSTLVector<T>::WriteSelf() dobj=NULL (ppfwrapstlv.h)");
102 // On ecrit 3 uint_8
103 // 0 : Numero de version = 1 : Taille, 2 reserve a l
104 uint_8 itab[3];
105 itab[0] = 1;
106 itab[1] = dobj->size();
107 itab[2] = 0;
108 os.Put(itab, 3);
109 // On ecrit le vecteur de donnees
110 for(uint_8 k=0; k<itab[1]; k++) os << (*dobj)[k];
111 }
112
113 // Variables membres
114 std::vector<T> * dobj; // Le vecteur de la STL
115 bool ownobj; // true -> objet cree par le wrapper
116};
117
118/*! Writes the STL vector object in the POutPersist stream \b os */
119template <class T>
120inline POutPersist& operator << (POutPersist& os, std::vector<T> & obj)
121{ PPFWrapperSTLVector<T> fio(&obj); fio.Write(os); return(os); }
122/*! Reads in and initializes the STL vector object from the PInPersist stream \b is */
123template <class T>
124inline PInPersist& operator >> (PInPersist& is, std::vector<T> & obj)
125{ PPFWrapperSTLVector<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
126
127} // Fin du namespace
128
129#endif
Note: See TracBrowser for help on using the repository browser.