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