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