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

Last change on this file since 3415 was 3385, checked in by ansari, 18 years ago

protection pour I/O PPF de NDataBlock, lorsque taille (Size()) == 0, Reza 21/11/2007

File size: 6.1 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, uint_2 * arr, size_t n)
120{ is.Get(arr, n); }
121inline void PIOSReadArray(PInPersist & is, int_2 * arr, size_t n)
122{ is.Get(arr, n); }
123inline void PIOSReadArray(PInPersist & is, uint_4 * arr, size_t n)
124{ is.Get(arr, n); }
125inline void PIOSReadArray(PInPersist & is, int_4 * arr, size_t n)
126{ is.Get(arr, n); }
127inline void PIOSReadArray(PInPersist & is, uint_8 * arr, size_t n)
128{ is.Get(arr, n); }
129inline void PIOSReadArray(PInPersist & is, int_8 * arr, size_t n)
130{ is.Get(arr, n); }
131inline void PIOSReadArray(PInPersist & is, r_4 * arr, size_t n)
132{ is.Get(arr, n); }
133inline void PIOSReadArray(PInPersist & is, r_8 * arr, size_t n)
134{ is.Get(arr, n); }
135inline void PIOSReadArray(PInPersist & is, complex<float> * arr, size_t n)
136{ r_4 * pr = (r_4 *)arr; is.Get(pr, n*2); }
137inline void PIOSReadArray(PInPersist & is, complex<double> * arr, size_t n)
138{ r_8 * pr = (r_8 *)arr; is.Get(pr, n*2); }
139//---------------------------------------------------------------------------
140
141template <class T>
142void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)
143{
144// On lit les 3 premiers uint_8
145uint_8 itab[3];
146is.Get(itab, 3);
147if (dobj == NULL) {
148 if (itab[1] > 0) dobj = new NDataBlock<T>(itab[1]);
149 else dobj = new NDataBlock<T>();
150}
151else {
152 if (itab[1] != dobj->Size()) {
153 if (itab[1] > 0) dobj->ReSize(itab[1]);
154 else dobj->Dealloc();
155 }
156}
157if (dobj->Size() < 1) return; // Pas de donnees a lire ...
158// On lit le tableau de nombres
159if (is.Version() <= 2) // lecture ancienne version PPF
160 PIOSReadArray(is, dobj->Data(), dobj->Size());
161else is.Get(dobj->Data(), dobj->Size());
162}
163
164
165template <class T>
166void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const
167{
168if (dobj == NULL) return; // Attention - $CHECK$ Reza 26/04/99
169// On ecrit 3 uint_8
170// 0 : Numero de version, 1 : Taille, 2 reserve a l
171uint_8 itab[3];
172itab[0] = 1;
173itab[1] = dobj->Size();
174itab[2] = 0;
175os.Put(itab, 3);
176// On ecrit le tableau de nombres
177if (dobj->Size() > 0) // On ecrit les donnees, s'il y en a ... (sz>0)
178 os.Put(dobj->Data(), dobj->Size());
179}
180
181///////////////////////////////////////////////////////////////
182#ifdef __CXX_PRAGMA_TEMPLATES__
183// Instances des delegues FileIO (PPersist)
184#pragma define_template FIO_NDataBlock<uint_1>
185#pragma define_template FIO_NDataBlock<uint_2>
186#pragma define_template FIO_NDataBlock<int_2>
187#pragma define_template FIO_NDataBlock<int_4>
188#pragma define_template FIO_NDataBlock<int_8>
189#pragma define_template FIO_NDataBlock<uint_4>
190#pragma define_template FIO_NDataBlock<uint_8>
191#pragma define_template FIO_NDataBlock<r_8>
192#pragma define_template FIO_NDataBlock<r_4>
193#pragma define_template FIO_NDataBlock< complex<r_4> >
194#pragma define_template FIO_NDataBlock< complex<r_8> >
195#endif
196
197#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
198namespace SOPHYA {
199// Instances des delegues FileIO (PPersist)
200template class FIO_NDataBlock<uint_1>;
201template class FIO_NDataBlock<uint_2>;
202template class FIO_NDataBlock<int_2>;
203template class FIO_NDataBlock<int_4>;
204template class FIO_NDataBlock<int_8>;
205template class FIO_NDataBlock<uint_4>;
206template class FIO_NDataBlock<uint_8>;
207template class FIO_NDataBlock<r_8>;
208template class FIO_NDataBlock<r_4>;
209template class FIO_NDataBlock< complex<r_4> >;
210template class FIO_NDataBlock< complex<r_8> >;
211}
212#endif
Note: See TracBrowser for help on using the repository browser.