source: Sophya/trunk/SophyaLib/BaseTools/ndatablock.cc@ 461

Last change on this file since 461 was 441, checked in by ansari, 26 years ago

#include <piocmplx.h> -> entre double quotes yvon+cmv 27/9/99

File size: 19.0 KB
RevLine 
[268]1// Gestion de block de donnees avec partage de references
2// C.Magneville 04/99
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
[245]4#include "machdefs.h"
5#include <stdio.h>
6#include <stdlib.h>
7#include <iostream.h>
8#include <complex>
9#include "pexceptions.h"
10#include "ndatablock.h"
[269]11#include "objfio.h"
[245]12
[268]13using namespace PlanckDPC;
[245]14
[275]15// define DEBUG_NDATABLOCK
[268]16
[245]17#ifdef DEBUG_NDATABLOCK
18 static size_t NallocData = 0;
19 static size_t NallocSRef = 0;
20#endif
21
[275]22////////////////////////////////////////////////////////////////
[289]23//************ Createur, Destructeur
[245]24
25template <class T>
26NDataBlock<T>::NDataBlock(size_t n)
27// Createur d'une structure de "n" donnees
28: mSz(0), mSRef(NULL), mIsTemp(false)
29{
[268]30#ifdef DEBUG_NDATABLOCK
[289]31cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
[268]32#endif
33
[245]34Alloc(n);
35}
36
37template <class T>
38NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
39// Createur d'une structure de "n" donnees, avec donnees preallouees
[275]40// (Voir explications dans Alloc())
[245]41: mSz(0), mSRef(NULL), mIsTemp(false)
42{
[268]43#ifdef DEBUG_NDATABLOCK
[289]44cout<<"?_NDataBlock::NDataBlock("<<this
[268]45 <<",data="<<data<<",br="<<br<<")"<<endl;
46#endif
47
[245]48Alloc(n,data,br);
49}
50
51template <class T>
52NDataBlock<T>::NDataBlock()
53// Createur par default
54: mSz(0), mSRef(NULL), mIsTemp(false)
55{
[268]56#ifdef DEBUG_NDATABLOCK
[289]57cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
[268]58#endif
[245]59}
60
61template <class T>
[268]62NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
[289]63// Createur par copie: partage les donnees si "a" temporaire, clone sinon.
[245]64: mSz(0), mSRef(NULL), mIsTemp(false)
65{
[268]66#ifdef DEBUG_NDATABLOCK
[289]67cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
[268]68#endif
69
[289]70CloneOrShare(a);
[245]71}
72
73template <class T>
[268]74NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
[289]75// Createur avec choix de partager ou non selon "share"
[245]76: mSz(0), mSRef(NULL), mIsTemp(false)
77{
[268]78#ifdef DEBUG_NDATABLOCK
[289]79cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl;
[268]80#endif
81
[245]82if(share) Share(a); else Clone(a);
83}
84
85template <class T>
86NDataBlock<T>::~NDataBlock()
87// Destructeur
88{
[268]89#ifdef DEBUG_NDATABLOCK
[289]90cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
[268]91#endif
92
[245]93Delete();
94}
95
[275]96////////////////////////////////////////////////////////////////
[289]97//************ Gestion de donnees
[275]98
[245]99template <class T>
100void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br)
[285]101// Allocation d'un NOUVEL espace de stoquage de "n" donnees
[275]102// Si data==NULL : allocation de l'espace memoire (vide)
[245]103// data!=NULL : partage des donnees avec l'adresse data
104// Si br==NULL : les donnees nous appartiennent
[275]105// br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
106//
[257]107// Exemple: on veut connecter a un tableau de T*
[275]108// 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "data"
109// a- Premiere solution
110// float *x = new float[5]; ... remplissage de x[] ...;
111// NDataBlock A(5,x,new Bridge);
112// delete [] x; // Il faut deleter explicitement
113// (et Bridge est delete par le destructeur de la classe)
114// b- Autre solution:
115// NDataBlock A(5); A.FillFrom(5,x);
116// delete [] x; // Il faut deleter explicitement
117// 2- On veut que NDataBlock desalloue le tableau
118// float *x = new float[5]; ... remplissage de x[] ...;
119// NDataBlock A(5,x);
120// (Ne Pas Faire "delete [] x;")
[245]121{
[268]122#ifdef DEBUG_NDATABLOCK
[289]123cout<<"?_NDataBlock::Alloc("<<this<<","
[268]124 <<n<<","<<data<<","<<br<<") mSz="<<mSz
125 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
126#endif
127
[275]128if(br && !data)
129 throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
[245]130if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
131if(mSRef) Delete();
132mSz = n;
133mSRef = new NDREF;
134mSRef->nref = 1;
[285]135if(data) mSRef->data = data;
136else {mSRef->data = new T[n]; memset(mSRef->data,0,n*sizeof(T));}
[245]137mSRef->bridge = br;
[268]138
139#ifdef DEBUG_NDATABLOCK
[275]140// Meme dans le cas data!=0 et br==0 (connexion d'un tableau
141// avec destruction geree par ~NDataBlock (cas 2-) on compte
142// comme si on avait fait une allocation du tableau (ce qui a ete
143// fait au niveau du dessus!).
144if(!br) NallocData++; NallocSRef++;
[289]145cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
[268]146 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
147 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp
148 <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
149#endif
[245]150}
151
152template <class T>
[268]153void NDataBlock<T>::Clone(const NDataBlock<T>& a)
[289]154// Clone: copie de donnees a partir de "a"
[245]155{
[275]156#ifdef DEBUG_NDATABLOCK
[289]157cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
[275]158 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
159 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
160#endif
161
[289]162if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
163if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n"));
164Alloc(a.mSz);
165memcpy(Data(),a.Data(),mSz*sizeof(T));
[245]166}
167
168template <class T>
[289]169void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
170// CloneOrShare: Share si "a" temporaire, Clone sinon.
171{
172#ifdef DEBUG_NDATABLOCK
173cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
174#endif
175
176if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
177if(a.IsTemp()) Share(a); else Clone(a);
178}
179
180template <class T>
[268]181void NDataBlock<T>::Share(const NDataBlock<T>& a)
[289]182// Share: Partage les donnees avec "a"
[245]183{
[268]184#ifdef DEBUG_NDATABLOCK
[289]185cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
[268]186if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
187 <<" IsTemp="<<a.IsTemp()<<")";
188cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
189#endif
190
[245]191if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
[289]192if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
[245]193if(mSRef) Delete();
194mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
[268]195
196#ifdef DEBUG_NDATABLOCK
[289]197cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
[268]198 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
199 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl;
200#endif
[245]201}
202
203template <class T>
204void NDataBlock<T>::Delete(void)
205// Pour detruire les pointeurs en tenant compte des references
206{
[268]207#ifdef DEBUG_NDATABLOCK
[289]208cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
[268]209 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
210if(mSRef)
211 cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
212 <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
213cout<<endl;
214#endif
215
[289]216if(mSRef==NULL) return;
[245]217mSRef->nref--;
218if(mSRef->nref != 0) {
[268]219
220#ifdef DEBUG_NDATABLOCK
[289]221cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
[275]222 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
[268]223#endif
224
[285]225 mSz = 0; mSRef=NULL;
[245]226 return;
227}
[268]228
229#ifdef DEBUG_NDATABLOCK
230if(!mSRef->bridge) NallocData--; NallocSRef--;
[289]231cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
[268]232 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
233#endif
234
[245]235// Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
[285]236// sinon, les donnees ont ete allouees par nos soins, on libere l'espace
237if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
[245]238mSRef->bridge=NULL; mSRef->data=NULL;
[285]239delete mSRef; mSRef=NULL; mSz = 0;
[245]240}
241
242template <class T>
[257]243void NDataBlock<T>::FillFrom(size_t n,T* data)
244// Remplissage par un tableau de donnees
245// - Si classe vide : creation de l'espace memoire
[289]246// - Si classe connectee : on ecrit selon la longueur minimale
247// (cad this->mSz ou "n")
[257]248{
249if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
250if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
[289]251if(mSRef==NULL) Alloc(n);
[285]252if(mSz<n) n = mSz;
[257]253memcpy(Data(),data,n*sizeof(T));
254}
255
[275]256////////////////////////////////////////////////////////////////
[245]257//**** Impression
258
259template <class T>
[268]260void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
[245]261// Impression de n elements a partir de i1
262{
263size_t nr = 0;
[267]264T* p = NULL; Bridge* br = NULL;
265if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
[268]266os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
267 <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
268 <<",bridge="<<br<<")"<<endl;
[249]269if(i1>=mSz || n<=0 || !p) return;
[245]270size_t i2 = i1+n; if(i2>mSz) i2=mSz;
[269]271size_t im = 1; bool enl=false;
[245]272while(i1<i2) {
[257]273 enl = false;
[268]274 os<<" "<<(*this)(i1); i1++;
275 if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
[245]276}
[268]277if(!enl) os<<endl;
[245]278}
279
[275]280////////////////////////////////////////////////////////////////
[289]281
282template <class T>
283T NDataBlock<T>::Sum(size_t i1,size_t n) const
284// Somme des elements de i1 a i1+n-1
285{
286if(i1>=mSz) return 0;
287if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
288T const *p=Begin()+i1, *pe=p+n;
289T val = 0;
290while (p<pe) val += *p++;
291return val;
292}
293
294template <class T>
295T NDataBlock<T>::Product(size_t i1,size_t n) const
296// Produit des elements de i1 a i1+n-1
297{
298if(i1>=mSz) return 0;
299if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
300T const *p=Begin()+i1, *pe=p+n;
301T val = 0;
302while (p<pe) val *= *p++;
303return val;
304}
305
306////////////////////////////////////////////////////////////////
[245]307//**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
308
309template <class T>
[268]310NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
[289]311// Affectation: partage des donnees si "a" temporaire, clone sinon.
[245]312{
[268]313#ifdef DEBUG_NDATABLOCK
[289]314cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
[268]315 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
316 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
317#endif
318
[245]319if(this == &a) return *this;
[289]320if(a.mSz==0)
321 throw(SzMismatchError("NDataBlock::operator=A null size\n"));
322CloneOrShare(a);
[245]323return *this;
324}
325
326template <class T>
[249]327NDataBlock<T>& NDataBlock<T>::operator = (T v)
[289]328// Affectation de tous les elements a une constante "v"
[245]329{
[268]330#ifdef DEBUG_NDATABLOCK
[289]331cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
[268]332 <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
333#endif
334
[245]335if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
[249]336T *p=Begin(), *pe=End();
[245]337while (p<pe) *p++ = v;
338return *this;
339}
340
[275]341////////////////////////////////////////////////////////////////
[245]342//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b;
343
344template <class T>
345NDataBlock<T>& NDataBlock<T>::operator += (T b)
346{
347if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
[249]348T *p=Begin(), *pe=End();
[245]349while (p<pe) *p++ += b;
350return *this;
351}
352
353template <class T>
354NDataBlock<T>& NDataBlock<T>::operator -= (T b)
355{
356if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
[249]357T *p=Begin(), *pe=End();
[245]358while (p<pe) *p++ -= b;
359return *this;
360}
361
362template <class T>
363NDataBlock<T>& NDataBlock<T>::operator *= (T b)
364{
365if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
[249]366T *p=Begin(), *pe=End();
[245]367while (p<pe) *p++ *= b;
368return *this;
369}
370
371template <class T>
372NDataBlock<T>& NDataBlock<T>::operator /= (T b)
373{
[249]374if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
[245]375if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
[249]376T *p=Begin(), *pe=End();
[245]377while (p<pe) *p++ /= b;
378return *this;
379}
380
[275]381////////////////////////////////////////////////////////////////
[245]382//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock;
383
384template <class T>
[268]385NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
[245]386{
[265]387if(mSz==0 || mSz!=a.mSz)
388 throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
[268]389T *p=Begin(), *pe=End();
390T const * pa=a.Begin();
[289]391while (p<pe) *p++ += *pa++;
[245]392return *this;
393}
394
395template <class T>
[268]396NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
[245]397{
[265]398if(mSz==0 || mSz!=a.mSz)
399 throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
[268]400T *p=Begin(), *pe=End();
401T const *pa=a.Begin();
[289]402while (p<pe) *p++ -= *pa++;
[245]403return *this;
404}
405
406template <class T>
[268]407NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
[245]408{
[265]409if(mSz==0 || mSz!=a.mSz)
410 throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
[268]411T *p=Begin(), *pe=End();
412T const *pa=a.Begin();
[289]413while (p<pe) *p++ *= *pa++;
[245]414return *this;
415}
416
417template <class T>
[268]418NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
[289]419// Attention, aucune protection si un element de "a" est nul.
[245]420{
[265]421if(mSz==0 || mSz!=a.mSz)
422 throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
[268]423T *p=Begin(), *pe=End();
424T const *pa=a.Begin();
[245]425while (p<pe) *p++ /= *pa++;
426return *this;
427}
428
[275]429////////////////////////////////////////////////////////////////
430//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b;
431// NDataBlock = <T>b+NDataBlock;
[245]432
433template <class T>
[268]434NDataBlock<T> NDataBlock<T>::Add(T b) const
[257]435// Pour A+b
[245]436{
[289]437NDataBlock<T> result(*this); result.SetTemp(true);
[268]438result += b;
439return result;
[245]440}
441
442template <class T>
[268]443NDataBlock<T> NDataBlock<T>::Sub(T b) const
[257]444// Pour A-b
[245]445{
[289]446NDataBlock<T> result(*this); result.SetTemp(true);
[259]447return result -= b;
[245]448}
449
450template <class T>
[268]451NDataBlock<T> NDataBlock<T>::SubInv(T b) const
[257]452// Pour b-A
[245]453{
[289]454NDataBlock<T> result(*this); result.SetTemp(true);
[268]455T *p=result.Begin(), *pe=result.End();
456T const *pa=this->Begin();
[245]457while(p<pe) {*p++ = b - *pa++;}
458return result;
459}
460
461template <class T>
[268]462NDataBlock<T> NDataBlock<T>::Mul(T b) const
[257]463// Pour A*b
[245]464{
[289]465NDataBlock<T> result(*this); result.SetTemp(true);
[259]466return result *= b;
[245]467}
468
469template <class T>
[268]470NDataBlock<T> NDataBlock<T>::Div(T b) const
[257]471// Pour A/b
[245]472{
[289]473NDataBlock<T> result(*this); result.SetTemp(true);
[259]474return result /= b;
[245]475}
476
477template <class T>
[268]478NDataBlock<T> NDataBlock<T>::DivInv(T b) const
[257]479// Pour b/A
[245]480{
[289]481NDataBlock<T> result(*this); result.SetTemp(true);
[268]482T *p=result.Begin(), *pe=result.End();
483T const *pa = this->Begin();
[245]484while(p<pe) {*p++ = b / *pa++;}
485return result;
486}
487
[275]488////////////////////////////////////////////////////////////////
[245]489//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock;
490
491template <class T>
[268]492NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
[257]493// Pour A+B
[245]494{
[285]495if(mSz!=b.mSz)
[265]496 throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
[268]497NDataBlock<T> result; result.SetTemp(true);
[289]498if(b.IsTemp()) {result.Share(b); result += *this;}
499 else {result.CloneOrShare(*this); result += b;}
[268]500return result;
[245]501}
502
503template <class T>
[268]504NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
[257]505// Pour A*B
[245]506{
[285]507if(mSz!=b.mSz)
[265]508 throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
[268]509NDataBlock<T> result; result.SetTemp(true);
[289]510if(b.IsTemp()) {result.Share(b); result *= *this;}
511 else {result.CloneOrShare(*this); result *= b;}
[268]512return result;
[245]513}
514
515template <class T>
[268]516NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
[257]517// Pour A-B
[245]518{
[285]519if(mSz!=b.mSz)
[265]520 throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
[268]521NDataBlock<T> result; result.SetTemp(true);
[245]522if(b.IsTemp()) {
[268]523 result.Share(b);
[289]524 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
[245]525 while(p<pe) {*p = *pa++ - *p; p++;}
[289]526} else {result.CloneOrShare(*this); result -= b;}
[268]527return result;
[245]528}
529
530template <class T>
[268]531NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
[257]532// Pour A/B
[245]533{
[285]534if(mSz!=b.mSz)
[265]535 throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
[268]536NDataBlock<T> result; result.SetTemp(true);
[245]537if(b.IsTemp()) {
[268]538 result.Share(b);
[289]539 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
[245]540 while(p<pe) {*p = *pa++ / *p; p++;}
[289]541} else {result.CloneOrShare(*this); result /= b;}
[268]542return result;
[245]543}
544
[275]545////////////////////////////////////////////////////////////////
[269]546// -------------------------------------------------------------------------
547// Les objets delegues pour la gestion de persistance
548// -------------------------------------------------------------------------
549
550/*
551template <class T>
552void ObjFileIO< NDataBlock<T> >::ReadSelf(PInPersist& is)
553template <class T>
554void ObjFileIO< NDataBlock<T> >::WriteSelf(POutPersist& os)
555*/
556
[277]557// Pour pouvoir ecrire des tableaux de complex, en attendant
558// PIn/POutPersist::Get/Put(complex<>)
[441]559#include "piocmplx.h"
[277]560
[269]561template <class T>
562FIO_NDataBlock<T>::FIO_NDataBlock()
563{
564dobj=new NDataBlock<T>;
[277]565ownobj=true;
[269]566}
567
568template <class T>
569FIO_NDataBlock<T>::FIO_NDataBlock(string const & filename)
570{
[277]571dobj=new NDataBlock<T>;
572ownobj=true;
[269]573Read(filename);
574}
575
576template <class T>
577FIO_NDataBlock<T>::FIO_NDataBlock(const NDataBlock<T> & obj)
578{
[277]579dobj = new NDataBlock<T>(obj);
580ownobj=true;
[269]581}
582
583template <class T>
[277]584FIO_NDataBlock<T>::FIO_NDataBlock(NDataBlock<T> * obj)
[269]585{
[277]586dobj = obj;
587ownobj=false;
[269]588}
589
590template <class T>
591FIO_NDataBlock<T>::~FIO_NDataBlock()
592{
[277]593if (ownobj && dobj) delete dobj;
[269]594}
595
596template <class T>
597AnyDataObj* FIO_NDataBlock<T>::DataObj()
598{
599return(dobj);
600}
601
602
603template <class T>
604void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)
605{
606// On lit les 3 premiers uint_8
607uint_8 itab[3];
608is.Get(itab, 3);
609if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);
[290]610else if (itab[1] != dobj->Size()) dobj->ReSize(itab[1]);
[269]611// On lit le tableau de nombres
612PIOSReadArray(is, dobj->Data(), dobj->Size());
613}
614
615
616template <class T>
617void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const
618{
619if (dobj == NULL) return; // Attention - $CHECK$ Reza 26/04/99
[305]620// On ecrit 3 uint_8
[269]621// 0 : Numero de version, 1 : Taille, 2 reserve a l
622uint_8 itab[3];
623itab[0] = 1;
624itab[1] = dobj->Size();
625itab[2] = 0;
626os.Put(itab, 3);
627// On ecrit le tableau de nombres
628PIOSWriteArray(os, dobj->Data(), dobj->Size());
629}
630
[275]631///////////////////////////////////////////////////////////////
[245]632#ifdef __CXX_PRAGMA_TEMPLATES__
633#pragma define_template NDataBlock<uint_1>
634#pragma define_template NDataBlock<uint_2>
635#pragma define_template NDataBlock<int_2>
636#pragma define_template NDataBlock<int_4>
637#pragma define_template NDataBlock<int_8>
638#pragma define_template NDataBlock<uint_4>
639#pragma define_template NDataBlock<uint_8>
640#pragma define_template NDataBlock<r_4>
641#pragma define_template NDataBlock<r_8>
[268]642#pragma define_template NDataBlock< complex<float> >
643#pragma define_template NDataBlock< complex<double> >
[269]644// Instances des delegues FileIO (PPersist)
[273]645#pragma define_template FIO_NDataBlock<uint_1>
646#pragma define_template FIO_NDataBlock<uint_2>
647#pragma define_template FIO_NDataBlock<int_2>
648#pragma define_template FIO_NDataBlock<int_4>
649#pragma define_template FIO_NDataBlock<int_8>
650#pragma define_template FIO_NDataBlock<uint_4>
651#pragma define_template FIO_NDataBlock<uint_8>
652#pragma define_template FIO_NDataBlock<r_8>
653#pragma define_template FIO_NDataBlock<r_4>
654#pragma define_template FIO_NDataBlock< complex<float> >
655#pragma define_template FIO_NDataBlock< complex<double> >
[245]656#endif
657
[269]658#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
[245]659template class NDataBlock<uint_1>;
660template class NDataBlock<uint_2>;
661template class NDataBlock<int_2>;
662template class NDataBlock<int_4>;
663template class NDataBlock<int_8>;
664template class NDataBlock<uint_4>;
665template class NDataBlock<uint_8>;
666template class NDataBlock<r_4>;
667template class NDataBlock<r_8>;
[249]668template class NDataBlock< complex<float> >;
669template class NDataBlock< complex<double> >;
[269]670// Instances des delegues FileIO (PPersist)
671template class FIO_NDataBlock<uint_1>;
672template class FIO_NDataBlock<uint_2>;
673template class FIO_NDataBlock<int_2>;
674template class FIO_NDataBlock<int_4>;
675template class FIO_NDataBlock<int_8>;
676template class FIO_NDataBlock<uint_4>;
677template class FIO_NDataBlock<uint_8>;
678template class FIO_NDataBlock<r_8>;
679template class FIO_NDataBlock<r_4>;
680template class FIO_NDataBlock< complex<float> >;
681template class FIO_NDataBlock< complex<double> >;
[245]682#endif
Note: See TracBrowser for help on using the repository browser.