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