| 1 | // Classe pour la gestion de persistance pour NDataBlock<T>
 | 
|---|
| 2 | //                         C.Magneville          04/99-03/2000
 | 
|---|
| 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
| 4 | #include "sopnamsp.h"
 | 
|---|
| 5 | #include "machdefs.h"
 | 
|---|
| 6 | #include <stdio.h>
 | 
|---|
| 7 | #include <stdlib.h>
 | 
|---|
| 8 | #include <iostream>
 | 
|---|
| 9 | #include <complex>
 | 
|---|
| 10 | #include "pexceptions.h"
 | 
|---|
| 11 | #include "datatype.h"
 | 
|---|
| 12 | #include "fiondblock.h"
 | 
|---|
| 13 | #include <typeinfo>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 16 | // -------------------------------------------------------------------------
 | 
|---|
| 17 | //   Les objets delegues pour la gestion de persistance 
 | 
|---|
| 18 | // -------------------------------------------------------------------------
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /*
 | 
|---|
| 21 | template <class T>
 | 
|---|
| 22 | void ObjFileIO< NDataBlock<T> >::ReadSelf(PInPersist& is)
 | 
|---|
| 23 | template <class T>
 | 
|---|
| 24 | void ObjFileIO< NDataBlock<T> >::WriteSelf(POutPersist& os)
 | 
|---|
| 25 | */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | 
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | FIO_NDataBlock<T>::FIO_NDataBlock()
 | 
|---|
| 30 | {
 | 
|---|
| 31 | dobj=new NDataBlock<T>;
 | 
|---|
| 32 | ownobj=true;
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | template <class T>
 | 
|---|
| 36 | FIO_NDataBlock<T>::FIO_NDataBlock(string const & filename) 
 | 
|---|
| 37 | {
 | 
|---|
| 38 | dobj=new NDataBlock<T>;
 | 
|---|
| 39 | ownobj=true; 
 | 
|---|
| 40 | Read(filename);
 | 
|---|
| 41 | }
 | 
|---|
| 42 | 
 | 
|---|
| 43 | template <class T>
 | 
|---|
| 44 | FIO_NDataBlock<T>::FIO_NDataBlock(const NDataBlock<T> & obj) 
 | 
|---|
| 45 | { 
 | 
|---|
| 46 | dobj = new NDataBlock<T>(obj);
 | 
|---|
| 47 | ownobj=true; 
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | template <class T>
 | 
|---|
| 51 | FIO_NDataBlock<T>::FIO_NDataBlock(NDataBlock<T> * obj) 
 | 
|---|
| 52 | { 
 | 
|---|
| 53 | dobj = obj;
 | 
|---|
| 54 | ownobj=false; 
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | template <class T>
 | 
|---|
| 58 | FIO_NDataBlock<T>::~FIO_NDataBlock()
 | 
|---|
| 59 | {
 | 
|---|
| 60 | if (ownobj && dobj) delete dobj;
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | template <class T>
 | 
|---|
| 64 | AnyDataObj* FIO_NDataBlock<T>::DataObj()
 | 
|---|
| 65 | {
 | 
|---|
| 66 | return(dobj);
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | template <class T>
 | 
|---|
| 70 | void FIO_NDataBlock<T>::SetDataObj(AnyDataObj & o)
 | 
|---|
| 71 | {
 | 
|---|
| 72 | NDataBlock<T> * po = dynamic_cast< NDataBlock<T> * >(&o);
 | 
|---|
| 73 | if (po == NULL) {
 | 
|---|
| 74 |   char buff[160];
 | 
|---|
| 75 |   sprintf(buff,"FIO_NDataBlock<T><%s>::SetDataObj(%s) - Object type  error ! ",
 | 
|---|
| 76 |           DataTypeInfo<T>::getTypeName().c_str(), typeid(o).name());
 | 
|---|
| 77 |   throw TypeMismatchExc(PExcLongMessage(buff));    
 | 
|---|
| 78 | }
 | 
|---|
| 79 | if (ownobj && dobj) delete dobj;
 | 
|---|
| 80 | dobj = po; ownobj = false;
 | 
|---|
| 81 | } 
 | 
|---|
| 82 | 
 | 
|---|
| 83 | template <class T>
 | 
|---|
| 84 | uint_8 FIO_NDataBlock<T>::getMemOId() const
 | 
|---|
| 85 | {
 | 
|---|
| 86 |   uint_8 rv = 0;
 | 
|---|
| 87 |   if (dobj) rv = (uint_8)(dobj->DRefId());
 | 
|---|
| 88 |   return(rv);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | template <class T>
 | 
|---|
| 92 | void FIO_NDataBlock<T>::ShareDataReference(PPersist & pp)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   FIO_NDataBlock<T> *ppo = dynamic_cast< FIO_NDataBlock<T> * >(&pp);
 | 
|---|
| 95 |   if (ppo == NULL) throw TypeMismatchExc("FIO_NDataBlock<T>::ShareDataReference() - Type Mismatch Error");
 | 
|---|
| 96 |   if (ppo->dobj) {
 | 
|---|
| 97 |     if (dobj == NULL) { dobj = new NDataBlock<T>; ownobj = true; } 
 | 
|---|
| 98 |     dobj->Share(*(ppo->dobj));
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | template <class T>
 | 
|---|
| 103 | PPersist* FIO_NDataBlock<T>::CloneSharedReference()
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   FIO_NDataBlock<T> * ppo = new FIO_NDataBlock<T>;
 | 
|---|
| 106 |   if (dobj) (ppo->dobj)->Share(*dobj);
 | 
|---|
| 107 |   return(ppo);
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | //---------------------------------------------------------------------------
 | 
|---|
| 111 | // Pour compatibilite de lecture avec PPF V2 
 | 
|---|
| 112 | inline void PIOSReadArray(PInPersist & is, uint_1 * arr, size_t n)
 | 
|---|
| 113 | { is.GetBytes(arr, n); }
 | 
|---|
| 114 | inline void PIOSReadArray(PInPersist & is, uint_2 * arr, size_t n)
 | 
|---|
| 115 | { is.Get(arr, n); }
 | 
|---|
| 116 | inline void PIOSReadArray(PInPersist & is, int_2 * arr, size_t n)
 | 
|---|
| 117 | { is.Get(arr, n); }
 | 
|---|
| 118 | inline void PIOSReadArray(PInPersist & is, uint_4 * arr, size_t n)
 | 
|---|
| 119 | { is.Get(arr, n); }
 | 
|---|
| 120 | inline void PIOSReadArray(PInPersist & is, int_4 * arr, size_t n)
 | 
|---|
| 121 | { is.Get(arr, n); }
 | 
|---|
| 122 | inline void PIOSReadArray(PInPersist & is, uint_8 * arr, size_t n)
 | 
|---|
| 123 | { is.Get(arr, n); }
 | 
|---|
| 124 | inline void PIOSReadArray(PInPersist & is, int_8 * arr, size_t n)
 | 
|---|
| 125 | { is.Get(arr, n); }
 | 
|---|
| 126 | inline void PIOSReadArray(PInPersist & is, r_4 * arr, size_t n)
 | 
|---|
| 127 | { is.Get(arr, n); }
 | 
|---|
| 128 | inline void PIOSReadArray(PInPersist & is, r_8 * arr, size_t n)
 | 
|---|
| 129 | { is.Get(arr, n); }
 | 
|---|
| 130 | inline void PIOSReadArray(PInPersist & is, complex<float> * arr, size_t n)
 | 
|---|
| 131 | { r_4 * pr = (r_4 *)arr; is.Get(pr, n*2); }
 | 
|---|
| 132 | inline void PIOSReadArray(PInPersist & is, complex<double> * arr, size_t n)
 | 
|---|
| 133 | { r_8 * pr = (r_8 *)arr; is.Get(pr, n*2); }
 | 
|---|
| 134 | //---------------------------------------------------------------------------
 | 
|---|
| 135 | 
 | 
|---|
| 136 | template <class T>
 | 
|---|
| 137 | void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)
 | 
|---|
| 138 | {
 | 
|---|
| 139 | // On lit les 3 premiers uint_8
 | 
|---|
| 140 | uint_8 itab[3];
 | 
|---|
| 141 | is.Get(itab, 3);
 | 
|---|
| 142 | if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);
 | 
|---|
| 143 | else if (itab[1] != dobj->Size()) dobj->ReSize(itab[1]);
 | 
|---|
| 144 | // On lit le tableau de nombres
 | 
|---|
| 145 | if (is.Version() <= 2)   // lecture ancienne version PPF
 | 
|---|
| 146 |   PIOSReadArray(is, dobj->Data(), dobj->Size()); 
 | 
|---|
| 147 | else is.Get(dobj->Data(), dobj->Size());
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | 
 | 
|---|
| 151 | template <class T>
 | 
|---|
| 152 | void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const
 | 
|---|
| 153 | {
 | 
|---|
| 154 | if (dobj == NULL)   return;  // Attention - $CHECK$ Reza 26/04/99
 | 
|---|
| 155 | //  On ecrit 3 uint_8 
 | 
|---|
| 156 | //  0 : Numero de version,  1 : Taille,  2  reserve a l
 | 
|---|
| 157 | uint_8 itab[3];
 | 
|---|
| 158 | itab[0] = 1;
 | 
|---|
| 159 | itab[1] = dobj->Size();
 | 
|---|
| 160 | itab[2] = 0;
 | 
|---|
| 161 | os.Put(itab, 3);
 | 
|---|
| 162 | //  On ecrit le tableau de nombres 
 | 
|---|
| 163 | os.Put(dobj->Data(), dobj->Size());
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | ///////////////////////////////////////////////////////////////
 | 
|---|
| 167 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
| 168 | // Instances des delegues FileIO (PPersist)
 | 
|---|
| 169 | #pragma define_template FIO_NDataBlock<uint_1>
 | 
|---|
| 170 | #pragma define_template FIO_NDataBlock<uint_2>
 | 
|---|
| 171 | #pragma define_template FIO_NDataBlock<int_2>
 | 
|---|
| 172 | #pragma define_template FIO_NDataBlock<int_4>
 | 
|---|
| 173 | #pragma define_template FIO_NDataBlock<int_8>
 | 
|---|
| 174 | #pragma define_template FIO_NDataBlock<uint_4>
 | 
|---|
| 175 | #pragma define_template FIO_NDataBlock<uint_8>
 | 
|---|
| 176 | #pragma define_template FIO_NDataBlock<r_8>
 | 
|---|
| 177 | #pragma define_template FIO_NDataBlock<r_4>
 | 
|---|
| 178 | #pragma define_template FIO_NDataBlock< complex<r_4> >
 | 
|---|
| 179 | #pragma define_template FIO_NDataBlock< complex<r_8> >
 | 
|---|
| 180 | #endif
 | 
|---|
| 181 | 
 | 
|---|
| 182 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| 183 | // Instances des delegues FileIO (PPersist)
 | 
|---|
| 184 | template class FIO_NDataBlock<uint_1>;
 | 
|---|
| 185 | template class FIO_NDataBlock<uint_2>;
 | 
|---|
| 186 | template class FIO_NDataBlock<int_2>;
 | 
|---|
| 187 | template class FIO_NDataBlock<int_4>;
 | 
|---|
| 188 | template class FIO_NDataBlock<int_8>;
 | 
|---|
| 189 | template class FIO_NDataBlock<uint_4>;
 | 
|---|
| 190 | template class FIO_NDataBlock<uint_8>;
 | 
|---|
| 191 | template class FIO_NDataBlock<r_8>;
 | 
|---|
| 192 | template class FIO_NDataBlock<r_4>;
 | 
|---|
| 193 | template class FIO_NDataBlock< complex<r_4> >;
 | 
|---|
| 194 | template class FIO_NDataBlock< complex<r_8> >;
 | 
|---|
| 195 | #endif
 | 
|---|