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

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

changement commentaire doxygen, Reza 17/03/2012

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