11 #include "utilities.h"
12 #include "MEDMEM_Exception.hxx"
13 #include "MEDMEM_define.hxx"
15 #include "MEDMEM_Support.hxx"
16 #include "MEDMEM_Unit.hxx"
17 #include "MEDMEM_Array.hxx"
18 #include "MEDMEM_GenDriver.hxx"
20 #include "MEDMEM_MedFieldDriver.hxx"
22 // template <class T> class MED_FIELD_RDONLY_DRIVER;
23 // template <class T> class MED_FIELD_WRONLY_DRIVER;
24 // template <class T> class MED_FIELD_RDWR_DRIVER;
26 using namespace MED_EN;
29 class FIELD_ // GENERIC POINTER TO a template <class T> class FIELD
36 const SUPPORT * _support ;
37 int _numberOfComponents ;
38 int * _componentsTypes ; // array of size _numberOfComponents
39 // (constant, scalar, vector, tensor)
40 // we could use an array of integer to store
43 // space dimension for vector,
44 // space dimension square for tensor.
45 // so numbers of values per entities are
46 // sum of _componentsTypes array
47 // Do not use yet! All type are scalar !
48 string * _componentsNames; // array of size _numberOfComponents
49 string * _componentsDescriptions; // array of size _numberOfComponents
50 UNIT * _componentsUnits; // array of size _numberOfComponents
51 string * _MEDComponentsUnits; // array of size _numberOfComponents :
52 // get unit from med file
54 int _iterationNumber ;
58 /*MED_EN::*/med_type_champ _valueType ;
63 FIELD_(const SUPPORT * Support, const int NumberOfComponents);
64 FIELD_(const FIELD_ &m);
68 // virtual void setIterationNumber (int IterationNumber);
69 // virtual void setOrderNumber (int OrderNumber);
70 // virtual void setFieldName (string& fieldName);
72 virtual void rmDriver(int index);
73 virtual int addDriver(driverTypes driverType,
74 const string & fileName,
75 const string & driverFieldName) ;
76 virtual int addDriver(GENDRIVER & driver);
77 virtual void write(const GENDRIVER &);
79 // virtual void getValueType (MED_FR::med_type_champ ValueType) ;
80 // virtual void setValueType (/*MED_EN::*/med_type_champ ValueType) ;
81 // virtual med_type_champ getValueType () ;
83 inline void setName(string Name);
84 inline string getName() const;
85 inline void setDescription(string Description);
86 inline string getDescription()const;
87 inline const SUPPORT * getSupport() const;
88 inline void setSupport(SUPPORT * support);
89 inline void setNumberOfComponents(int NumberOfComponents);
90 inline int getNumberOfComponents() const;
91 // inline void setComponentType(int *ComponentType);
92 // inline int * getComponentType() const;
93 // inline int getComponentTypeI(int i) const;
94 inline void setComponentsNames(string * ComponentsNames);
95 inline void setComponentName(int i, string ComponentName);
96 inline string * getComponentsNames() const;
97 inline string getComponentName(int i) const;
98 inline void setComponentsDescriptions(string *ComponentsDescriptions);
99 inline void setComponentDescription(int i, string ComponentDescription);
100 inline string * getComponentsDescriptions() const;
101 inline string getComponentDescription(int i) const;
103 // provisoire : en attendant de regler le probleme des unites !
104 inline void setComponentsUnits(UNIT * ComponentsUnits);
105 inline UNIT * getComponentsUnits() const;
106 inline UNIT * getComponentUnit(int i) const;
107 inline void setMEDComponentsUnits(string * MEDComponentsUnits);
108 inline void setMEDComponentUnit(int i, string MEDComponentUnit);
109 inline string * getMEDComponentsUnits() const;
110 inline string getMEDComponentUnit(int i) const;
112 inline void setIterationNumber(int IterationNumber);
113 inline int getIterationNumber() const;
114 inline void setTime(double Time);
115 inline double getTime() const;
116 inline void setOrderNumber(int OrderNumber);
117 inline int getOrderNumber() const;
119 inline void setValueType (/*MED_EN::*/med_type_champ ValueType) ;
120 inline /*MED_EN::*/med_type_champ getValueType () ;
124 // ---------------------------------
125 // Implemented Methods : constructor
126 // ---------------------------------
132 inline void FIELD_::setName(string Name)
136 inline string FIELD_::getName() const
140 inline void FIELD_::setDescription(string Description)
142 _description=Description;
144 inline string FIELD_::getDescription() const
148 inline void FIELD_::setNumberOfComponents(int NumberOfComponents)
150 _numberOfComponents=NumberOfComponents;
152 inline int FIELD_::getNumberOfComponents() const
154 return _numberOfComponents ;
156 // inline void FIELD_::setComponentType(int *ComponentType)
158 // _componentsTypes=ComponentType ;
160 // inline int * FIELD_::getComponentType() const
162 // return _componentsTypes ;
164 // inline int FIELD_::getComponentTypeI(int i) const
166 // return _componentsTypes[i-1] ;
168 inline void FIELD_::setComponentsNames(string * ComponentsNames)
170 _componentsNames=ComponentsNames ;
172 inline void FIELD_::setComponentName(int i, string ComponentName)
174 _componentsNames[i-1]=ComponentName ;
176 inline string * FIELD_::getComponentsNames() const
178 return _componentsNames ;
180 inline string FIELD_::getComponentName(int i) const
182 return _componentsNames[i-1] ;
184 inline void FIELD_::setComponentsDescriptions(string *ComponentsDescriptions)
186 _componentsDescriptions=ComponentsDescriptions ;
188 inline void FIELD_::setComponentDescription(int i,string ComponentDescription)
190 _componentsDescriptions[i-1]=ComponentDescription ;
192 inline string * FIELD_::getComponentsDescriptions() const
194 return _componentsDescriptions ;
196 inline string FIELD_::getComponentDescription(int i) const
198 return _componentsDescriptions[i-1];
200 inline void FIELD_::setComponentsUnits(UNIT * ComponentsUnits)
202 _componentsUnits=ComponentsUnits ;
204 inline UNIT * FIELD_::getComponentsUnits() const
206 return _componentsUnits ;
208 inline UNIT * FIELD_::getComponentUnit(int i) const
210 return &_componentsUnits[i-1] ;
212 inline void FIELD_::setMEDComponentsUnits(string * MEDComponentsUnits)
214 _MEDComponentsUnits=MEDComponentsUnits ;
216 inline void FIELD_::setMEDComponentUnit(int i, string MEDComponentUnit)
218 _MEDComponentsUnits[i-1]=MEDComponentUnit ;
220 inline string * FIELD_::getMEDComponentsUnits() const
222 return _MEDComponentsUnits ;
224 inline string FIELD_::getMEDComponentUnit(int i) const
226 return _MEDComponentsUnits[i-1] ;
228 inline void FIELD_::setIterationNumber(int IterationNumber)
230 _iterationNumber=IterationNumber;
232 inline int FIELD_::getIterationNumber() const
234 return _iterationNumber ;
236 inline void FIELD_::setTime(double Time)
240 inline double FIELD_::getTime() const
244 inline void FIELD_::setOrderNumber(int OrderNumber)
246 _orderNumber=OrderNumber ;
248 inline int FIELD_::getOrderNumber() const
250 return _orderNumber ;
252 inline const SUPPORT * FIELD_::getSupport() const
257 inline void FIELD_::setSupport(SUPPORT * support)
262 inline /*MED_EN::*/med_type_champ FIELD_::getValueType ()
267 inline void FIELD_::setValueType (/*MED_EN::*/med_type_champ ValueType)
269 _valueType = ValueType ;
272 /////////////////////////
273 // END OF CLASS FIELD_ //
274 /////////////////////////
276 template <class T> class FIELD : public FIELD_
279 // ------- Drivers Management Part
282 //-----------------------//
284 //-----------------------//
287 virtual GENDRIVER * run(const string & fileName, FIELD<T> * ptrFIELD) const = 0 ;
290 //-------------------------------------------------------//
291 template <class T2> class INSTANCE_DE : public INSTANCE
292 //-------------------------------------------------------//
295 GENDRIVER * run(const string & fileName, FIELD<T> * ptrFIELD) const
297 return new T2(fileName,ptrFIELD);
301 // static INSTANCE_DE<MED_FIELD_RDONLY_DRIVER> inst_med_rdonly ;
302 static INSTANCE_DE<MED_FIELD_RDWR_DRIVER<T> > inst_med ;
303 static const INSTANCE * const instances[] ;
305 // ------ End of Drivers Management Part
307 vector<GENDRIVER *> _drivers; // Storage of the drivers currently in use
309 // array of value of type T
310 MEDARRAY<T> *_value ;
317 FIELD(const FIELD &m); // A FAIRE
318 FIELD & operator=(const FIELD &m); // A FAIRE
319 FIELD(const SUPPORT * Support, const int NumberOfComponents); // Ajout NB Constructeur FIELD avec allocation de memoire de tous ses attribut
320 FIELD(driverTypes driverType, const string & fileName="",
321 const string & fieldName="");
324 friend class MED_FIELD_RDONLY_DRIVER<T>;
325 friend class MED_FIELD_WRONLY_DRIVER<T>;
326 friend class MED_FIELD_RDWR_DRIVER <T>;
329 void rmDriver(int index=0);
330 int addDriver(driverTypes driverType,
331 const string & fileName="Default File Name.med",
332 const string & driverFieldName="Default Field Name") ;
333 int addDriver(GENDRIVER & driver);
335 void allocValue(const int NumberOfComponents);
336 void allocValue(const int NumberOfComponents, const int LengthValue);
340 inline void read(int index=0);
341 inline void write(int index=0, const string & driverName = "");
342 inline void write(const GENDRIVER &);
344 inline void setValue(MEDARRAY<T> *Value);
346 inline T* getValue(medModeSwitch Mode) const;
347 inline T* getValueI(medModeSwitch Mode,int i) const;
348 inline T getValueIJ(int i,int j) const;
350 inline void setValue(medModeSwitch mode, T* value);
351 inline void setValueI(medModeSwitch mode, int i, T* value);
352 inline void setValueIJ(int i, int j, T value);
355 // --------------------
356 // Implemented Methods
357 // --------------------
359 template <class T> FIELD<T>::FIELD():
360 _value((MEDARRAY<T>*)NULL)
362 MESSAGE("Constructeur FIELD sans parametre");
365 template <class T> FIELD<T>::FIELD(const SUPPORT * Support,
366 const int NumberOfComponents):
367 FIELD_(Support, NumberOfComponents)
369 BEGIN_OF("FIELD<T>::FIELD(const SUPPORT * Support, const int NumberOfComponents)");
373 length = Support->getNumberOfElements(MED_ALL_ELEMENTS);
375 catch (MEDEXCEPTION &ex) {
376 MESSAGE("No value defined ! ("<<ex.what()<<")");
377 _value = (MEDARRAY<T>*)NULL ;
379 MESSAGE("FIELD : constructeur : "<<length <<" et "<< NumberOfComponents);
381 _value = new MEDARRAY<T>::MEDARRAY(NumberOfComponents,length);
383 _value = (MEDARRAY<T>*)NULL ;
385 END_OF("FIELD<T>::FIELD(const SUPPORT * Support, const int NumberOfComponents)");
388 template <class T> void FIELD<T>::init ()
392 template <class T> FIELD<T>::FIELD(const FIELD &m):
396 _drivers = m._drivers;
399 template <class T> FIELD<T> & FIELD<T>::FIELD::operator=(const FIELD &m)
403 template <class T> FIELD<T>::FIELD(driverTypes driverType,
404 const string & fileName="",
405 const string & fieldName="")
409 template <class T> FIELD<T>::~FIELD()
411 BEGIN_OF(" Destructeur FIELD<T>::~FIELD()");
412 if (_value) delete _value;
413 END_OF(" Destructeur FIELD<T>::~FIELD()");
416 template <class T> void FIELD<T>::allocValue(const int NumberOfComponents)
418 BEGIN_OF("void FIELD<T>::allocValue(const int NumberOfComponents)");
420 _numberOfComponents = NumberOfComponents ;
421 if (_componentsTypes == NULL)
422 _componentsTypes = new int[NumberOfComponents] ;
423 if (_componentsNames == NULL)
424 _componentsNames = new string[NumberOfComponents];
425 if (_componentsDescriptions == NULL)
426 _componentsDescriptions = new string[NumberOfComponents];
427 if (_componentsUnits == NULL)
428 _componentsUnits = new UNIT[NumberOfComponents];
429 if (_MEDComponentsUnits == NULL)
430 _MEDComponentsUnits = new string[NumberOfComponents];
431 for (int i=0;i<NumberOfComponents;i++) {
432 _componentsTypes[i] = 0 ;
436 int length = _support->getNumberOfElements(MED_ALL_ELEMENTS);
437 MESSAGE("FIELD : constructeur : "<<length <<" et "<< NumberOfComponents);
439 _value = new MEDARRAY<T>::MEDARRAY(NumberOfComponents,length);
441 catch (MEDEXCEPTION &ex) {
442 MESSAGE("No value defined, problem with NumberOfComponents (and may be _support) size of MEDARRAY<T>::_value !");
443 _value = (MEDARRAY<T>*)NULL ;
447 END_OF("void FIELD<T>::allocValue(const int NumberOfComponents)");
450 template <class T> void FIELD<T>::allocValue(const int NumberOfComponents, const int LengthValue)
452 BEGIN_OF("void FIELD<T>::allocValue(const int NumberOfComponents,const int LengthValue)");
454 _numberOfComponents = NumberOfComponents ;
455 if (_componentsTypes == NULL)
456 _componentsTypes = new int[NumberOfComponents] ;
457 if (_componentsNames == NULL)
458 _componentsNames = new string[NumberOfComponents];
459 if (_componentsDescriptions == NULL)
460 _componentsDescriptions = new string[NumberOfComponents];
461 if (_componentsUnits == NULL)
462 _componentsUnits = new UNIT[NumberOfComponents];
463 if (_MEDComponentsUnits == NULL)
464 _MEDComponentsUnits = new string[NumberOfComponents];
465 for (int i=0;i<NumberOfComponents;i++) {
466 _componentsTypes[i] = 0 ;
469 MESSAGE("FIELD : constructeur : "<<LengthValue <<" et "<< NumberOfComponents);
471 _value = new MEDARRAY<T>::MEDARRAY(NumberOfComponents,LengthValue);
474 END_OF("void FIELD<T>::allocValue(const int NumberOfComponents,const int LengthValue)");
477 template <class T> void FIELD<T>::deallocValue()
479 BEGIN_OF("void FIELD<T>::deallocValue()");
483 END_OF("void FIELD<T>::deallocValue()");
491 template <class T> FIELD<T>::INSTANCE_DE<MED_FIELD_RDWR_DRIVER<T> > FIELD<T>::inst_med ;
492 template <class T> const FIELD<T>::INSTANCE * const FIELD<T>::instances[] = { &FIELD<T>::inst_med } ;
495 template <class T> int FIELD<T>::addDriver(driverTypes driverType,
496 const string & fileName="Default File Name.med",
497 const string & driverName="Default Field Name")
499 const char * LOC = "FIELD<T>::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\",const string & driverName=\"Default Field Name\") : ";
506 driver = instances[driverType]->run(fileName, this) ;
507 _drivers.push_back(driver);
508 current = _drivers.size()-1;
510 _drivers[current]->setFieldName(driverName);
517 template <class T> inline int FIELD<T>::addDriver (GENDRIVER & driver )
519 const char * LOC = "FIELD<T>::addDriver(GENDRIVER &) : ";
522 _drivers.push_back(&driver);
523 return _drivers.size() -1 ;
528 template <class T> void FIELD<T>::rmDriver (int index=0)
530 const char * LOC = "FIELD<T>::rmDriver (int index=0): ";
533 if ( _drivers[index] ) {
534 //_drivers.erase(&_drivers[index]);
536 MESSAGE ("detruire");
539 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
540 << "The index given is invalid, index must be between 0 and |"
548 template <class T> inline void FIELD<T>::read(int index=0)
550 const char * LOC = "FIELD<T>::read(int index=0) : ";
553 if ( _drivers[index] ) {
554 _drivers[index]->open();
555 _drivers[index]->read();
556 _drivers[index]->close();
559 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
560 << "The index given is invalid, index must be between 0 and |"
567 template <class T> inline void FIELD<T>::write(int index=0, const string & driverName = "")
569 const char * LOC = "FIELD<T>::write(int index=0, const string & driverName = \"\") : ";
572 if( _drivers[index] ) {
573 _drivers[index]->open();
574 if (driverName != "") _drivers[index]->setFieldName(driverName);
575 _drivers[index]->write();
576 _drivers[index]->close();
579 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
580 << "The index given is invalid, index must be between 0 and |"
587 template <class T> inline void FIELD<T>::write(const GENDRIVER & genDriver)
589 const char * LOC = " FIELD<T>::write(const GENDRIVER &) : ";
592 for (int index=0; index < _drivers.size(); index++ )
593 if ( *_drivers[index] == genDriver ) {
594 _drivers[index]->open();
595 _drivers[index]->write();
596 _drivers[index]->close();
603 template <class T> inline void FIELD<T>::setValue(MEDARRAY<T> *Value)
607 template <class T> inline T* FIELD<T>::getValue(medModeSwitch Mode) const
609 return _value->get(Mode) ;
611 template <class T> inline T* FIELD<T>::getValueI(medModeSwitch Mode,int i) const
613 return _value->getI(Mode,i) ;
615 template <class T> inline T FIELD<T>::getValueIJ(int i,int j) const
617 return _value->getIJ(i,j) ;
620 template <class T> inline void FIELD<T>::setValue(medModeSwitch mode, T* value)
622 _value->set(mode,value);
625 template <class T> inline void FIELD<T>::setValueI(medModeSwitch mode, int i, T* value)
627 _value->setI(mode,i,value);
630 template <class T> inline void FIELD<T>::setValueIJ(int i, int j, T value)
632 _value->setIJ(i,j,value);
635 #endif /* FIELD_HXX */