| 1 | //     Persistence manager for template numerical arrays | 
|---|
| 2 | //               R. Ansari, C.Magneville   03/2000 | 
|---|
| 3 |  | 
|---|
| 4 | #include "sopnamsp.h" | 
|---|
| 5 | #include "pexceptions.h" | 
|---|
| 6 | #include "fiondblock.h" | 
|---|
| 7 | #include "fioarr.h" | 
|---|
| 8 | #include "tmatrix.h" | 
|---|
| 9 | #include "tvector.h" | 
|---|
| 10 | #include "datatype.h" | 
|---|
| 11 | #include <typeinfo> | 
|---|
| 12 |  | 
|---|
| 13 | // -------------------------------------------------------- | 
|---|
| 14 | //   Les objets delegues pour la gestion de persistance | 
|---|
| 15 | // -------------------------------------------------------- | 
|---|
| 16 | /*! | 
|---|
| 17 | \class SOPHYA::FIO_TArray | 
|---|
| 18 | \ingroup TArray | 
|---|
| 19 | Class for persistent management of TArray | 
|---|
| 20 |  | 
|---|
| 21 | This class manage also persistence for TMatrix and TVector. | 
|---|
| 22 | \sa TArray TMatrix TVector. | 
|---|
| 23 | */ | 
|---|
| 24 | /////////////////////////////////////////////////////////// | 
|---|
| 25 |  | 
|---|
| 26 | //! Default constructor | 
|---|
| 27 | template <class T> | 
|---|
| 28 | FIO_TArray<T>::FIO_TArray() | 
|---|
| 29 | { | 
|---|
| 30 | dobj=NULL; | 
|---|
| 31 | ownobj=false; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | //! Constructor from the file \b filename | 
|---|
| 36 | template <class T> | 
|---|
| 37 | FIO_TArray<T>::FIO_TArray(string const & filename) | 
|---|
| 38 | { | 
|---|
| 39 | dobj=NULL; | 
|---|
| 40 | ownobj=false; | 
|---|
| 41 | Read(filename); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | //! Constructor from the TArray \b obj | 
|---|
| 45 | template <class T> | 
|---|
| 46 | FIO_TArray<T>::FIO_TArray(const TArray<T> & obj) | 
|---|
| 47 | { | 
|---|
| 48 | const TVector<T> * tv = dynamic_cast<const TVector<T> *>(&obj); | 
|---|
| 49 | if (tv != NULL)  dobj = new TVector<T>(*tv, true); | 
|---|
| 50 | else { | 
|---|
| 51 | const TMatrix<T> * tm = dynamic_cast<const TMatrix<T> *>(&obj); | 
|---|
| 52 | if (tm != NULL)  dobj = new TMatrix<T>(*tm, true); | 
|---|
| 53 | else dobj = new TArray<T>(obj, true); | 
|---|
| 54 | } | 
|---|
| 55 | ownobj=true; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | //! Connect with a TArray \b obj | 
|---|
| 59 | template <class T> | 
|---|
| 60 | FIO_TArray<T>::FIO_TArray(TArray<T> * obj) | 
|---|
| 61 | { | 
|---|
| 62 | dobj = obj; | 
|---|
| 63 | ownobj=false; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | //! destructor | 
|---|
| 67 | template <class T> | 
|---|
| 68 | FIO_TArray<T>::~FIO_TArray() | 
|---|
| 69 | { | 
|---|
| 70 | if (ownobj && dobj) delete dobj; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | //! Return pointer to the connected TArray | 
|---|
| 74 | template <class T> | 
|---|
| 75 | AnyDataObj* FIO_TArray<T>::DataObj() | 
|---|
| 76 | { | 
|---|
| 77 | return(dobj); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | //! Connect TArray \b o | 
|---|
| 81 | template <class T> | 
|---|
| 82 | void FIO_TArray<T>::SetDataObj(AnyDataObj & o) | 
|---|
| 83 | { | 
|---|
| 84 | TArray<T> * po = dynamic_cast< TArray<T> * >(&o); | 
|---|
| 85 | if (po == NULL) { | 
|---|
| 86 | char buff[160]; | 
|---|
| 87 | sprintf(buff,"FIO_TArray<%s>::SetDataObj(%s) - Object type  error ! ", | 
|---|
| 88 | DataTypeInfo<T>::getTypeName().c_str(), typeid(o).name()); | 
|---|
| 89 | throw TypeMismatchExc(PExcLongMessage(buff)); | 
|---|
| 90 | } | 
|---|
| 91 | if (ownobj && dobj) delete dobj; | 
|---|
| 92 | dobj = po; ownobj = false; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | template <class T> | 
|---|
| 96 | void FIO_TArray<T>::ReadSelf(PInPersist& is) | 
|---|
| 97 | { | 
|---|
| 98 | // On lit les 5 premiers uint_4 | 
|---|
| 99 | //  0: Numero de version,  1 : Type (Array, matrix, Vector, ...)  2 != 0 , has Info | 
|---|
| 100 | //          1:Type = 0 TArray , 12=(4+8) TMatrix , 48=(16+32) TVector | 
|---|
| 101 | uint_4 itab[5]; | 
|---|
| 102 | is.Get(itab,5); | 
|---|
| 103 |  | 
|---|
| 104 | // Checking version number | 
|---|
| 105 | if (itab[0] < 2) | 
|---|
| 106 | FileFormatExc("FIO_TArray<T>::ReadSelf() - Unsupported (old V<2) version"); | 
|---|
| 107 |  | 
|---|
| 108 | if (dobj == NULL) { | 
|---|
| 109 | if (itab[1] == 12)   dobj = new TMatrix<T>; | 
|---|
| 110 | else if (itab[1] == 48)   dobj = new TVector<T>; | 
|---|
| 111 | else   dobj = new TArray<T>; | 
|---|
| 112 | } | 
|---|
| 113 | // On lit les tailles, etc ... | 
|---|
| 114 | // On ecrit les tailles, etc ... | 
|---|
| 115 | int_4 tmpi4s[5]; | 
|---|
| 116 | is.Get(tmpi4s, 5); | 
|---|
| 117 | dobj->ndim_ = tmpi4s[0]; | 
|---|
| 118 | dobj->marowi_ = tmpi4s[1]; | 
|---|
| 119 | dobj->macoli_ = tmpi4s[2]; | 
|---|
| 120 | dobj->veceli_ = tmpi4s[3]; | 
|---|
| 121 | // tmpi4s[4] Reserved for future use | 
|---|
| 122 |  | 
|---|
| 123 | // Tous les sa_size_t sont ecrit/lu en int_8 afin de maintenir la compatibilite | 
|---|
| 124 | // entre version du programme utilisant int_4 OU int_8 pour sa_size_t | 
|---|
| 125 | int_8 tmpi8s[BASEARRAY_MAXNDIMS]; | 
|---|
| 126 | int kk; | 
|---|
| 127 |  | 
|---|
| 128 | is.Get(tmpi8s, BASEARRAY_MAXNDIMS); | 
|---|
| 129 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++) | 
|---|
| 130 | dobj->size_[kk] = tmpi8s[kk]; | 
|---|
| 131 | is.Get(tmpi8s, BASEARRAY_MAXNDIMS); | 
|---|
| 132 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++) | 
|---|
| 133 | dobj->step_[kk] = tmpi8s[kk]; | 
|---|
| 134 |  | 
|---|
| 135 | is.Get(tmpi8s, 5); | 
|---|
| 136 | dobj->totsize_ = tmpi8s[0]; | 
|---|
| 137 | dobj->offset_ = tmpi8s[1]; | 
|---|
| 138 | dobj->minstep_ = tmpi8s[2]; | 
|---|
| 139 | dobj->moystep_ = tmpi8s[3]; | 
|---|
| 140 | // tmpi8s[4] Reserved for future use | 
|---|
| 141 |  | 
|---|
| 142 | // On lit le datablock | 
|---|
| 143 | is >> dobj->DataBlock(); | 
|---|
| 144 | // On ecrit le DVList info si necessaire | 
|---|
| 145 | if (itab[2] != 0)  is >> dobj->Info(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | template <class T> | 
|---|
| 149 | void FIO_TArray<T>::WriteSelf(POutPersist& os) const | 
|---|
| 150 | { | 
|---|
| 151 | if (dobj == NULL)   return; | 
|---|
| 152 | //  On ecrit 5 uint_4 .... | 
|---|
| 153 | //  0: Numero de version,  1 : Type (Array, matrix, Vector, ...)  2 != 0 , has Info | 
|---|
| 154 | //          1:Type = 0 TArray , 12=(4+8) TMatrix , 48=(16+32) TVector | 
|---|
| 155 | uint_4 typa = 0; | 
|---|
| 156 | TVector<T> * tv = dynamic_cast<TVector<T> *>(dobj); | 
|---|
| 157 | if (tv != NULL)  typa = 48; | 
|---|
| 158 | else { | 
|---|
| 159 | TMatrix<T> * tm = dynamic_cast<TMatrix<T> *>(dobj); | 
|---|
| 160 | if (tm != NULL)  typa = 12; | 
|---|
| 161 | else  typa = 0; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | uint_4 itab[5]; | 
|---|
| 165 | itab[0] = 2;  // Numero de version a 2 depuis Aout 2000  - Reza | 
|---|
| 166 | itab[1] = typa;  // Real object type | 
|---|
| 167 | itab[2] = (dobj->mInfo != NULL) ? 1 : 0; | 
|---|
| 168 | itab[3] = itab[4] = 0; | 
|---|
| 169 | os.Put(itab,5); | 
|---|
| 170 |  | 
|---|
| 171 | // On ecrit les tailles, etc ... | 
|---|
| 172 | int_4 tmpi4s[5]; | 
|---|
| 173 | //  os.Put(dobj->ndim_); | 
|---|
| 174 | //  os.Put(dobj->marowi_); | 
|---|
| 175 | //  os.Put(dobj->macoli_); | 
|---|
| 176 | //  os.Put(dobj->veceli_); | 
|---|
| 177 | tmpi4s[0] = dobj->ndim_; | 
|---|
| 178 | tmpi4s[1] = dobj->marowi_; | 
|---|
| 179 | tmpi4s[2] = dobj->macoli_; | 
|---|
| 180 | tmpi4s[3] = dobj->veceli_; | 
|---|
| 181 | tmpi4s[4] = 0;   // Reserved for future use | 
|---|
| 182 | os.Put(tmpi4s, 5); | 
|---|
| 183 |  | 
|---|
| 184 | // Tous les sa_size_t sont ecrit en int_8 afin de pouvoir etre ecrit/relu | 
|---|
| 185 | // entre version du programme utilisant int_4 OU int_8 pour sa_size_t | 
|---|
| 186 | int_8 tmpi8s[BASEARRAY_MAXNDIMS]; | 
|---|
| 187 | int kk; | 
|---|
| 188 | //  os.Put(dobj->size_, BASEARRAY_MAXNDIMS); | 
|---|
| 189 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++) | 
|---|
| 190 | tmpi8s[kk] = dobj->size_[kk]; | 
|---|
| 191 | os.Put(tmpi8s, BASEARRAY_MAXNDIMS); | 
|---|
| 192 | //  os.Put(dobj->step_, BASEARRAY_MAXNDIMS); | 
|---|
| 193 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++) | 
|---|
| 194 | tmpi8s[kk] = dobj->step_[kk]; | 
|---|
| 195 | os.Put(tmpi8s, BASEARRAY_MAXNDIMS); | 
|---|
| 196 |  | 
|---|
| 197 | //  os.Put(dobj->totsize_); | 
|---|
| 198 | //  os.Put(dobj->offset_); | 
|---|
| 199 | //  os.Put(dobj->minstep_); | 
|---|
| 200 | //  os.Put(dobj->moystep_); | 
|---|
| 201 | tmpi8s[0] = dobj->totsize_; | 
|---|
| 202 | tmpi8s[1] = dobj->offset_; | 
|---|
| 203 | tmpi8s[2] = dobj->minstep_; | 
|---|
| 204 | tmpi8s[3] = dobj->moystep_; | 
|---|
| 205 | tmpi8s[4] = 0;  // Reserved for future use | 
|---|
| 206 | os.Put(tmpi8s, 5); | 
|---|
| 207 |  | 
|---|
| 208 | // On ecrit le datablock | 
|---|
| 209 | os << dobj->DataBlock(); | 
|---|
| 210 | // On ecrit le DVList info si necessaire | 
|---|
| 211 | if (itab[2] != 0)  os << dobj->Info(); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 216 | /////////////////////////////////////////////////////////////// | 
|---|
| 217 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 218 | // Instances des delegues FileIO (PPersist) | 
|---|
| 219 | // #pragma define_template FIO_TArray<uint_1> | 
|---|
| 220 | #pragma define_template FIO_TArray<uint_2> | 
|---|
| 221 | #pragma define_template FIO_TArray<uint_4> | 
|---|
| 222 | #pragma define_template FIO_TArray<uint_8> | 
|---|
| 223 | #pragma define_template FIO_TArray<int_2> | 
|---|
| 224 | #pragma define_template FIO_TArray<int_4> | 
|---|
| 225 | #pragma define_template FIO_TArray<int_8> | 
|---|
| 226 | #pragma define_template FIO_TArray<r_8> | 
|---|
| 227 | #pragma define_template FIO_TArray<r_4> | 
|---|
| 228 | #pragma define_template FIO_TArray< complex<r_4> > | 
|---|
| 229 | #pragma define_template FIO_TArray< complex<r_8> > | 
|---|
| 230 | #endif | 
|---|
| 231 |  | 
|---|
| 232 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 233 | namespace SOPHYA { | 
|---|
| 234 | // Instances des delegues FileIO (PPersist) | 
|---|
| 235 | // template class FIO_TArray<uint_1>; | 
|---|
| 236 | template class FIO_TArray<uint_2>; | 
|---|
| 237 | template class FIO_TArray<uint_4>; | 
|---|
| 238 | template class FIO_TArray<uint_8>; | 
|---|
| 239 | template class FIO_TArray<int_2>; | 
|---|
| 240 | template class FIO_TArray<int_4>; | 
|---|
| 241 | template class FIO_TArray<int_8>; | 
|---|
| 242 | template class FIO_TArray<r_8>; | 
|---|
| 243 | template class FIO_TArray<r_4>; | 
|---|
| 244 | template class FIO_TArray< complex<r_4> >; | 
|---|
| 245 | template class FIO_TArray< complex<r_8> >; | 
|---|
| 246 | } | 
|---|
| 247 | #endif | 
|---|