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