source: Sophya/trunk/SophyaLib/BaseTools/fiondblock.cc@ 2763

Last change on this file since 2763 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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