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

Last change on this file since 4054 was 3750, checked in by ansari, 16 years ago

Prise en charge de float 128 bits (r_16, complex<r_16>) par les NDataBlock<T> et PPersist, controlee par le flag de compilation SO_LDBLE128 defini ds machdefs.h , Reza 03/03/2010

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