| 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 | 
 | 
|---|
| 28 | namespace SOPHYA {
 | 
|---|
| 29 | 
 | 
|---|
| 30 |    
 | 
|---|
| 31 | template <class T>
 | 
|---|
| 32 | class PPFWrapperSTLVector : public  PPersist , public AnyDataObj {
 | 
|---|
| 33 | public:
 | 
|---|
| 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 | protected :
 | 
|---|
| 66 |   virtual void       ReadSelf(PInPersist& is)
 | 
|---|
| 67 |   {
 | 
|---|
| 68 |     if (dobj == NULL) 
 | 
|---|
| 69 |       throw ParmError("PPFWrapperSTLVector<T>::ReadSelf() dobj=NULL (ppfwrapstlv.h)");
 | 
|---|
| 70 |     // On lit les 3 premiers uint_8
 | 
|---|
| 71 |     uint_8 itab[3];
 | 
|---|
| 72 |     is.Get(itab, 3);
 | 
|---|
| 73 |     // On efface le contenu du vecteur si necessaire - (on peut faire plus efficacement ...)
 | 
|---|
| 74 |     if ( dobj->size() > 0) dobj->erase(dobj->begin(), dobj->end());
 | 
|---|
| 75 |     T el;
 | 
|---|
| 76 |     for(uint_8 k=0; k<itab[1]; k++) { 
 | 
|---|
| 77 |       is >> el;
 | 
|---|
| 78 |       dobj->push_back(el);
 | 
|---|
| 79 |     }
 | 
|---|
| 80 |   }         
 | 
|---|
| 81 |   virtual void       WriteSelf(POutPersist& os) const
 | 
|---|
| 82 |   {
 | 
|---|
| 83 |     if (dobj == NULL) 
 | 
|---|
| 84 |       throw ParmError("PPFWrapperSTLVector<T>::WriteSelf() dobj=NULL (ppfwrapstlv.h)");
 | 
|---|
| 85 |     //  On ecrit 3 uint_8 
 | 
|---|
| 86 |     //  0 : Numero de version = 1 : Taille,  2  reserve a l
 | 
|---|
| 87 |     uint_8 itab[3];
 | 
|---|
| 88 |     itab[0] = 1;
 | 
|---|
| 89 |     itab[1] = dobj->size();
 | 
|---|
| 90 |     itab[2] = 0;
 | 
|---|
| 91 |     os.Put(itab, 3);
 | 
|---|
| 92 |     // On ecrit le vecteur de donnees 
 | 
|---|
| 93 |     for(uint_8 k=0; k<itab[1]; k++) os << (*dobj)[k];
 | 
|---|
| 94 |   }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   // Variables membres 
 | 
|---|
| 97 |   std::vector<T> * dobj;  // Le vecteur de la STL 
 | 
|---|
| 98 |   bool ownobj;  // true -> objet cree par le wrapper 
 | 
|---|
| 99 | };
 | 
|---|
| 100 | 
 | 
|---|
| 101 | /*! Writes the STL vector object in the POutPersist stream \b os */
 | 
|---|
| 102 | template <class T>
 | 
|---|
| 103 | inline POutPersist& operator << (POutPersist& os, std::vector<T> & obj)
 | 
|---|
| 104 | { PPFWrapperSTLVector<T> fio(&obj);  fio.Write(os);  return(os); }
 | 
|---|
| 105 | /*! Reads in and initializes the STL vector object from the PInPersist stream \b is */
 | 
|---|
| 106 | template <class T>
 | 
|---|
| 107 | inline PInPersist& operator >> (PInPersist& is, std::vector<T> & obj)
 | 
|---|
| 108 | { PPFWrapperSTLVector<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | } // Fin du namespace
 | 
|---|
| 111 | 
 | 
|---|
| 112 | #endif
 | 
|---|