Salome HOME
update after merging trhe branches CEA_V3_0_x, OCC_V3_1_0_a1_x, and the main
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.hxx
1 #ifndef DRIVERFACTORY_HXX
2 #define DRIVERFACTORY_HXX
3
4 #include "MEDMEM_MedVersion.hxx"
5 #include "MEDMEM_GenDriver.hxx"
6 #include "MEDMEM_define.hxx"
7
8 #include <string>
9
10 namespace MEDMEM {
11
12   class MESH;
13   template<class T> class FIELD;
14   class MED;
15   class GENDRIVER;
16
17   namespace DRIVERFACTORY {
18
19     /*
20       definition of static variable across the Med Memory of a Med File version
21       for the writing of Med File set to V22
22     */
23
24     extern MED_EN::medFileVersion globalMedFileVersionForWriting;
25
26     MED_EN::medFileVersion getMedFileVersionForWriting();
27
28     void setMedFileVersionForWriting(MED_EN::medFileVersion version);
29
30     GENDRIVER * buildDriverForMesh(driverTypes driverType,
31                                    const std::string & fileName,
32                                    MESH *mesh,const string &  driverName,
33                                    MED_EN::med_mode_acces access);
34     template<class T>
35     GENDRIVER * buildDriverForField(driverTypes driverType,
36                                     const std::string & fileName,
37                                     FIELD<T> *fielde,
38                                     MED_EN::med_mode_acces access);
39     GENDRIVER * buildDriverForMed(driverTypes driverType,
40                                   const std::string & fileName,
41                                   MED *mede,
42                                   MED_EN::med_mode_acces access);
43     GENDRIVER * buildMedDriverFromFile(const string & fileName,
44                                        MED * const ptrMed,
45                                        MED_EN::med_mode_acces access);
46     GENDRIVER * buildMeshDriverFromFile(const string & fileName,
47                                         MESH * ptrMesh,
48                                         MED_EN::med_mode_acces access);
49     template<class T>
50     GENDRIVER * buildFieldDriverFromFile(const string & fileName,
51                                          FIELD<T> * ptrField,
52                                          MED_EN::med_mode_acces access);
53     GENDRIVER * buildConcreteMedDriverForMesh(const std::string & fileName,
54                                               MESH *mesh,
55                                               const string & driverName,
56                                               MED_EN::med_mode_acces access,
57                                               MED_EN::medFileVersion version);
58     template<class T>
59     GENDRIVER * buildConcreteMedDriverForField(const std::string & fileName,
60                                                FIELD<T> *fielde,
61                                                MED_EN::med_mode_acces access,
62                                                MED_EN::medFileVersion version);
63   }
64 }
65
66 #include "MEDMEM_VtkFieldDriver.hxx"
67 #include "MEDMEM_MedFieldDriver.hxx"
68 #include "MEDMEM_MedFieldDriver21.hxx"
69 #include "MEDMEM_MedFieldDriver22.hxx"
70 #include "MEDMEM_AsciiFieldDriver.hxx"
71
72 namespace MEDMEM {
73   template<class T>
74   GENDRIVER * DRIVERFACTORY::buildDriverForField(driverTypes driverType,
75                                                  const std::string & fileName,
76                                                  FIELD<T> *field,
77                                                  MED_EN::med_mode_acces access)
78   {
79     GENDRIVER *ret;
80     switch(driverType)
81       {
82       case MED_DRIVER : {
83         switch(access)
84           {
85           case MED_EN::MED_LECT : {
86             ret = new MED_FIELD_RDONLY_DRIVER<T>(fileName,field);
87             break;
88           }
89           case MED_EN::MED_ECRI : {
90             ret= new MED_FIELD_WRONLY_DRIVER<T>(fileName,field);
91             break;
92           }
93           case MED_EN::MED_REMP : {
94             ret = new MED_FIELD_RDWR_DRIVER<T>(fileName,field);
95             break;
96           }
97           default:
98             throw MED_EXCEPTION ("access type has not been properly specified to the method");
99           }
100         break;
101       }
102
103       case VTK_DRIVER : {
104         switch(access)
105           {
106           case MED_EN::MED_LECT : {
107             throw MED_EXCEPTION ("access mode other than MED_ECRI and MED_REMP has been specified with the VTK_DRIVER type which is not allowed because VTK_DRIVER is only a write access driver");
108             break;
109           }
110           case MED_EN::MED_ECRI : {
111             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
112             break;
113           }
114           case MED_EN::MED_REMP : {
115             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
116             break ;
117           }
118           default:
119             throw MED_EXCEPTION ("access type has not been properly specified to the method");
120           }
121         break;
122       }
123
124       case GIBI_DRIVER : {
125         throw MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
126         break;
127       }
128
129       case PORFLOW_DRIVER : {
130         throw MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
131         break;
132       }
133
134       case ASCII_DRIVER : {
135         switch(access)
136           {
137           case MED_EN::MED_ECRI : {
138             ret=new ASCII_FIELD_DRIVER<T>(fileName,field);
139             break;
140           }
141           default:
142             throw MED_EXCEPTION ("driver ASCII_DRIVER on FIELD only in write mod");
143           }
144         break;
145       }
146
147       case NO_DRIVER : {
148         throw MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
149         break;
150       }
151       default:
152         MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
153       }
154     return ret;
155   }
156
157   template<class T>
158   GENDRIVER * DRIVERFACTORY::buildFieldDriverFromFile(const string & fileName,
159                                                       FIELD<T> * ptrField,
160                                                       MED_EN::med_mode_acces access)
161   {
162     MED_EN::medFileVersion version;
163
164     try
165       {
166         version = getMedFileVersion(fileName);
167       }
168     catch (MEDEXCEPTION & ex)
169       {
170         version = DRIVERFACTORY::globalMedFileVersionForWriting;
171       }
172
173     MESSAGE("buildFieldDriverFromFile version of the file " << version);
174
175     GENDRIVER * driver;
176
177     switch(access)
178       {
179       case MED_EN::MED_LECT : {
180         if (version == MED_EN::V21)
181           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
182         else if (version == MED_EN::V22)
183           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
184         return driver;
185       }
186       case MED_EN::MED_ECRI : {
187         if (version == MED_EN::V21)
188           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
189         else if (version == MED_EN::V22)
190           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
191         return driver;
192       }
193       case MED_EN::MED_REMP : {
194         if (version == MED_EN::V21)
195           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
196         else if (version == MED_EN::V22)
197           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
198         return driver;
199       }
200       default:
201         throw MED_EXCEPTION ("access type has not been properly specified to the method");
202       }
203   }
204
205   template<class T>
206   GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForField(const std::string & fileName,
207                                                             FIELD<T> *ptrField,
208                                                             MED_EN::med_mode_acces access,
209                                                             MED_EN::medFileVersion version)
210   {
211
212     MESSAGE("buildConcreteMedDriverForField version of the file " << version);
213
214     GENDRIVER * driver;
215
216     switch(access)
217       {
218       case MED_EN::MED_LECT : {
219         if (version == MED_EN::V21)
220           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
221         else if (version == MED_EN::V22)
222           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
223         return driver;
224       }
225       case MED_EN::MED_ECRI : {
226         if (version == MED_EN::V21)
227           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
228         else if (version == MED_EN::V22)
229           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
230         return driver;
231       }
232       case MED_EN::MED_REMP : {
233         if (version == MED_EN::V21)
234           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
235         else if (version == MED_EN::V22)
236           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
237         return driver;
238       }
239       default:
240         throw MED_EXCEPTION ("access type has not been properly specified to the method");
241       }
242   }
243 }
244
245 #endif