[772] | 1 | // Persistence manager for template numerical arrays
|
---|
| 2 | // R. Ansari, C.Magneville 03/2000
|
---|
| 3 |
|
---|
[2615] | 4 | #include "sopnamsp.h"
|
---|
[772] | 5 | #include "pexceptions.h"
|
---|
[804] | 6 | #include "fiondblock.h"
|
---|
[772] | 7 | #include "fioarr.h"
|
---|
[804] | 8 | #include "tmatrix.h"
|
---|
| 9 | #include "tvector.h"
|
---|
[1953] | 10 | #include "datatype.h"
|
---|
| 11 | #include <typeinfo>
|
---|
[772] | 12 |
|
---|
| 13 | // --------------------------------------------------------
|
---|
| 14 | // Les objets delegues pour la gestion de persistance
|
---|
| 15 | // --------------------------------------------------------
|
---|
[926] | 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 | */
|
---|
[772] | 24 | ///////////////////////////////////////////////////////////
|
---|
| 25 |
|
---|
[894] | 26 | //! Default constructor
|
---|
[772] | 27 | template <class T>
|
---|
| 28 | FIO_TArray<T>::FIO_TArray()
|
---|
| 29 | {
|
---|
[804] | 30 | dobj=NULL;
|
---|
| 31 | ownobj=false;
|
---|
[772] | 32 | }
|
---|
| 33 |
|
---|
[894] | 34 |
|
---|
| 35 | //! Constructor from the file \b filename
|
---|
[772] | 36 | template <class T>
|
---|
| 37 | FIO_TArray<T>::FIO_TArray(string const & filename)
|
---|
| 38 | {
|
---|
[804] | 39 | dobj=NULL;
|
---|
| 40 | ownobj=false;
|
---|
[772] | 41 | Read(filename);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[894] | 44 | //! Constructor from the TArray \b obj
|
---|
[772] | 45 | template <class T>
|
---|
| 46 | FIO_TArray<T>::FIO_TArray(const TArray<T> & obj)
|
---|
| 47 | {
|
---|
[804] | 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 | }
|
---|
[772] | 55 | ownobj=true;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[894] | 58 | //! Connect with a TArray \b obj
|
---|
[772] | 59 | template <class T>
|
---|
| 60 | FIO_TArray<T>::FIO_TArray(TArray<T> * obj)
|
---|
| 61 | {
|
---|
| 62 | dobj = obj;
|
---|
| 63 | ownobj=false;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[894] | 66 | //! destructor
|
---|
[772] | 67 | template <class T>
|
---|
| 68 | FIO_TArray<T>::~FIO_TArray()
|
---|
| 69 | {
|
---|
| 70 | if (ownobj && dobj) delete dobj;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[894] | 73 | //! Return pointer to the connected TArray
|
---|
[772] | 74 | template <class T>
|
---|
| 75 | AnyDataObj* FIO_TArray<T>::DataObj()
|
---|
| 76 | {
|
---|
| 77 | return(dobj);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[894] | 80 | //! Connect TArray \b o
|
---|
[772] | 81 | template <class T>
|
---|
| 82 | void FIO_TArray<T>::SetDataObj(AnyDataObj & o)
|
---|
| 83 | {
|
---|
| 84 | TArray<T> * po = dynamic_cast< TArray<T> * >(&o);
|
---|
[1953] | 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 | }
|
---|
[772] | 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 | {
|
---|
[804] | 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
|
---|
[772] | 101 | uint_4 itab[5];
|
---|
| 102 | is.Get(itab,5);
|
---|
[1156] | 103 |
|
---|
| 104 | // Checking version number
|
---|
| 105 | if (itab[0] < 2)
|
---|
| 106 | FileFormatExc("FIO_TArray<T>::ReadSelf() - Unsupported (old V<2) version");
|
---|
| 107 |
|
---|
[804] | 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 | }
|
---|
[772] | 113 | // On lit les tailles, etc ...
|
---|
[1156] | 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];
|
---|
[1173] | 126 | int kk;
|
---|
[1156] | 127 |
|
---|
| 128 | is.Get(tmpi8s, BASEARRAY_MAXNDIMS);
|
---|
[1173] | 129 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++)
|
---|
[1156] | 130 | dobj->size_[kk] = tmpi8s[kk];
|
---|
| 131 | is.Get(tmpi8s, BASEARRAY_MAXNDIMS);
|
---|
[1173] | 132 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++)
|
---|
[1156] | 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 |
|
---|
[772] | 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;
|
---|
[804] | 152 | // On ecrit 5 uint_4 ....
|
---|
[772] | 153 | // 0: Numero de version, 1 : Type (Array, matrix, Vector, ...) 2 != 0 , has Info
|
---|
[804] | 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 |
|
---|
[772] | 164 | uint_4 itab[5];
|
---|
[1156] | 165 | itab[0] = 2; // Numero de version a 2 depuis Aout 2000 - Reza
|
---|
[804] | 166 | itab[1] = typa; // Real object type
|
---|
[772] | 167 | itab[2] = (dobj->mInfo != NULL) ? 1 : 0;
|
---|
| 168 | itab[3] = itab[4] = 0;
|
---|
[804] | 169 | os.Put(itab,5);
|
---|
[1156] | 170 |
|
---|
[772] | 171 | // On ecrit les tailles, etc ...
|
---|
[1156] | 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];
|
---|
[1173] | 187 | int kk;
|
---|
[1156] | 188 | // os.Put(dobj->size_, BASEARRAY_MAXNDIMS);
|
---|
[1173] | 189 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++)
|
---|
[1156] | 190 | tmpi8s[kk] = dobj->size_[kk];
|
---|
| 191 | os.Put(tmpi8s, BASEARRAY_MAXNDIMS);
|
---|
| 192 | // os.Put(dobj->step_, BASEARRAY_MAXNDIMS);
|
---|
[1173] | 193 | for(kk=0; kk<BASEARRAY_MAXNDIMS; kk++)
|
---|
[1156] | 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 |
|
---|
[772] | 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)
|
---|
[3661] | 219 | #pragma define_template FIO_TArray<uint_1>
|
---|
[772] | 220 | #pragma define_template FIO_TArray<uint_2>
|
---|
[2927] | 221 | #pragma define_template FIO_TArray<uint_4>
|
---|
| 222 | #pragma define_template FIO_TArray<uint_8>
|
---|
[3661] | 223 | #pragma define_template FIO_TArray<int_1>
|
---|
[2927] | 224 | #pragma define_template FIO_TArray<int_2>
|
---|
[772] | 225 | #pragma define_template FIO_TArray<int_4>
|
---|
| 226 | #pragma define_template FIO_TArray<int_8>
|
---|
| 227 | #pragma define_template FIO_TArray<r_8>
|
---|
| 228 | #pragma define_template FIO_TArray<r_4>
|
---|
| 229 | #pragma define_template FIO_TArray< complex<r_4> >
|
---|
| 230 | #pragma define_template FIO_TArray< complex<r_8> >
|
---|
| 231 | #endif
|
---|
| 232 |
|
---|
| 233 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[2868] | 234 | namespace SOPHYA {
|
---|
[772] | 235 | // Instances des delegues FileIO (PPersist)
|
---|
[3661] | 236 | template class FIO_TArray<uint_1>;
|
---|
[772] | 237 | template class FIO_TArray<uint_2>;
|
---|
[2927] | 238 | template class FIO_TArray<uint_4>;
|
---|
| 239 | template class FIO_TArray<uint_8>;
|
---|
[3661] | 240 | template class FIO_TArray<int_1>;
|
---|
[2927] | 241 | template class FIO_TArray<int_2>;
|
---|
[818] | 242 | template class FIO_TArray<int_4>;
|
---|
[772] | 243 | template class FIO_TArray<int_8>;
|
---|
| 244 | template class FIO_TArray<r_8>;
|
---|
| 245 | template class FIO_TArray<r_4>;
|
---|
| 246 | template class FIO_TArray< complex<r_4> >;
|
---|
| 247 | template class FIO_TArray< complex<r_8> >;
|
---|
[2868] | 248 | }
|
---|
[772] | 249 | #endif
|
---|