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