]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_DriverFactory.hxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #ifndef DRIVERFACTORY_HXX
23 #define DRIVERFACTORY_HXX
24
25 #include <MEDMEM.hxx>
26
27 #include "MEDMEM_MedVersion.hxx"
28 #include "MEDMEM_GenDriver.hxx"
29 #include "MEDMEM_define.hxx"
30
31 #include "MEDMEM_FieldForward.hxx"
32
33 #include <string>
34
35 namespace MEDMEM {
36
37   class MESH;
38   class MED;
39   class GENDRIVER;
40
41   namespace DRIVERFACTORY {
42
43     /*
44       definition of static variable across the Med Memory of a Med File version
45       for the writing of Med File set to V22
46     */
47
48     MEDMEM_EXPORT extern MED_EN::medFileVersion globalMedFileVersionForWriting;
49
50     MEDMEM_EXPORT MED_EN::medFileVersion getMedFileVersionForWriting();
51
52     MEDMEM_EXPORT void setMedFileVersionForWriting(MED_EN::medFileVersion version);
53
54     MEDMEM_EXPORT driverTypes deduceDriverTypeFromFileName(const std::string & fileName);
55
56     driverTypes deduceDriverTypeFromFileName(const std::string & fileName);
57
58     GENDRIVER * buildDriverForMesh(driverTypes driverType,
59                                    const std::string & fileName,
60                                    MESH *mesh,const string &  driverName,
61                                    MED_EN::med_mode_acces access);
62
63     template<class T, class INTERLACING_TAG>
64     GENDRIVER * buildDriverForField(driverTypes driverType,
65                                     const std::string & fileName,
66                                     FIELD<T,INTERLACING_TAG> *fielde,
67                                     MED_EN::med_mode_acces access);
68     GENDRIVER * buildDriverForMed(driverTypes driverType,
69                                   const std::string & fileName,
70                                   MED *mede,
71                                   MED_EN::med_mode_acces access);
72     GENDRIVER * buildMedDriverFromFile(const string & fileName,
73                                        MED * const ptrMed,
74                                        MED_EN::med_mode_acces access);
75     GENDRIVER * buildMeshDriverFromFile(const string & fileName,
76                                         MESH * ptrMesh,
77                                         MED_EN::med_mode_acces access);
78     template<class T, class INTERLACING_TAG>
79     GENDRIVER * buildFieldDriverFromFile(const string & fileName,
80                                          FIELD<T,INTERLACING_TAG> * ptrField,
81                                          MED_EN::med_mode_acces access);
82     GENDRIVER * buildConcreteMedDriverForMesh(const std::string & fileName,
83                                               MESH *mesh,
84                                               const string & driverName,
85                                               MED_EN::med_mode_acces access,
86                                               MED_EN::medFileVersion version);
87     template<class T, class INTERLACING_TAG>
88     GENDRIVER * buildConcreteMedDriverForField(const std::string & fileName,
89                                                FIELD<T,INTERLACING_TAG> *fielde,
90                                                MED_EN::med_mode_acces access,
91                                                MED_EN::medFileVersion version);
92   }
93 }
94
95 #include "MEDMEM_VtkFieldDriver.hxx"
96 #include "MEDMEM_MedFieldDriver.hxx"
97 #include "MEDMEM_MedFieldDriver21.hxx"
98 #include "MEDMEM_MedFieldDriver22.hxx"
99 #include "MEDMEM_AsciiFieldDriver.hxx"
100 #include "MEDMEM_EnsightFieldDriver.hxx"
101
102 namespace MEDMEM {
103   template<class T, class INTERLACING_TAG>
104   GENDRIVER * DRIVERFACTORY::buildDriverForField(driverTypes driverType,
105                                                  const std::string & fileName,
106                                                  FIELD<T,INTERLACING_TAG> *field,
107                                                  MED_EN::med_mode_acces access)
108   {
109     GENDRIVER *ret;
110     switch(driverType)
111       {
112       case MED_DRIVER : {
113         switch(access)
114           {
115           case MED_EN::RDONLY : {
116             ret = new MED_FIELD_RDONLY_DRIVER<T>(fileName,field);
117             break;
118           }
119           case MED_EN::WRONLY : {
120             ret= new MED_FIELD_WRONLY_DRIVER<T>(fileName,field);
121             break;
122           }
123           case MED_EN::RDWR : {
124             ret = new MED_FIELD_RDWR_DRIVER<T>(fileName,field);
125             break;
126           }
127           default:
128             throw MED_EXCEPTION ("access type has not been properly specified to the method");
129           }
130         break;
131       }
132
133       case ENSIGHT_DRIVER : {
134         switch(access)
135           {
136           case MED_EN::RDONLY : {
137             ret = new ENSIGHT_FIELD_RDONLY_DRIVER(fileName,field);
138             break;
139           }
140           case MED_EN::WRONLY : {
141             ret=new ENSIGHT_FIELD_WRONLY_DRIVER(fileName,field);
142             break;
143           }
144           case MED_EN::RDWR : {
145             throw MED_EXCEPTION ("not yet implemented");
146             break ;
147           }
148           default:
149             throw MED_EXCEPTION ("access type has not been properly specified to the method");
150           }
151         break;
152       }
153
154       case VTK_DRIVER : {
155         switch(access)
156           {
157           case MED_EN::RDONLY : {
158             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");
159             break;
160           }
161           case MED_EN::WRONLY : {
162             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
163             break;
164           }
165           case MED_EN::RDWR : {
166             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
167             break ;
168           }
169           default:
170             throw MED_EXCEPTION ("access type has not been properly specified to the method");
171           }
172         break;
173       }
174
175       case GIBI_DRIVER : {
176         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");
177         break;
178       }
179
180       case PORFLOW_DRIVER : {
181         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");
182         break;
183       }
184
185       case ASCII_DRIVER : {
186         switch(access)
187           {
188           case MED_EN::WRONLY : {
189             ret=new ASCII_FIELD_DRIVER<T>(fileName,field);
190             break;
191           }
192           default:
193             throw MED_EXCEPTION ("driver ASCII_DRIVER on FIELD only in write mod");
194           }
195         break;
196       }
197
198       case NO_DRIVER : {
199         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");
200         break;
201       }
202       default:
203         MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
204       }
205     return ret;
206   }
207
208   template<class T, class INTERLACING_TAG>
209   GENDRIVER * DRIVERFACTORY::buildFieldDriverFromFile(const string & fileName,
210                                                       FIELD<T,INTERLACING_TAG> * ptrField,
211                                                       MED_EN::med_mode_acces access)
212   {
213     MED_EN::medFileVersion version;
214
215     try
216       {
217         version = getMedFileVersion(fileName);
218       }
219     catch (MEDEXCEPTION & )
220       {
221         version = DRIVERFACTORY::globalMedFileVersionForWriting;
222       }
223
224     MESSAGE_MED("buildFieldDriverFromFile version of the file " << version);
225
226     GENDRIVER * driver;
227
228     switch(access)
229       {
230       case MED_EN::RDONLY : {
231         if (version == MED_EN::V21)
232           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
233         else if (version == MED_EN::V22)
234           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
235         return driver;
236       }
237       case MED_EN::WRONLY : {
238         if (version == MED_EN::V21)
239           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
240         else if (version == MED_EN::V22)
241           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
242         return driver;
243       }
244       case MED_EN::RDWR : {
245         if (version == MED_EN::V21)
246           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
247         else if (version == MED_EN::V22)
248           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
249         return driver;
250       }
251       default:
252         throw MED_EXCEPTION ("access type has not been properly specified to the method");
253       }
254   }
255
256   template<class T, class INTERLACING_TAG>
257   GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForField(const std::string & fileName,
258                                                             FIELD<T,INTERLACING_TAG> *ptrField,
259                                                             MED_EN::med_mode_acces access,
260                                                             MED_EN::medFileVersion version)
261   {
262
263     MESSAGE_MED("buildConcreteMedDriverForField version of the file " << version);
264
265     GENDRIVER * driver;
266
267     switch(access)
268       {
269       case MED_EN::RDONLY : {
270         if (version == MED_EN::V21)
271           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
272         else if (version == MED_EN::V22)
273           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
274         return driver;
275       }
276       case MED_EN::WRONLY : {
277         if (version == MED_EN::V21)
278           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
279         else if (version == MED_EN::V22)
280           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
281         return driver;
282       }
283       case MED_EN::RDWR : {
284         if (version == MED_EN::V21)
285           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
286         else if (version == MED_EN::V22)
287           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
288         return driver;
289       }
290       default:
291         throw MED_EXCEPTION ("access type has not been properly specified to the method");
292       }
293   }
294 }
295
296 #endif