]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_DriverFactory.hxx
Salome HOME
90e524d74e4b6454f0e811936f3e4908677142cb
[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     GENDRIVER * buildDriverForMesh(driverTypes driverType,
51                                    const std::string & fileName,
52                                    MESH *mesh,const string &  driverName,
53                                    MED_EN::med_mode_acces access);
54
55     template<class T, class INTERLACING_TAG>
56     GENDRIVER * buildDriverForField(driverTypes driverType,
57                                     const std::string & fileName,
58                                     FIELD<T,INTERLACING_TAG> *fielde,
59                                     MED_EN::med_mode_acces access);
60     GENDRIVER * buildDriverForMed(driverTypes driverType,
61                                   const std::string & fileName,
62                                   MED *mede,
63                                   MED_EN::med_mode_acces access);
64     GENDRIVER * buildMedDriverFromFile(const string & fileName,
65                                        MED * const ptrMed,
66                                        MED_EN::med_mode_acces access);
67     GENDRIVER * buildMeshDriverFromFile(const string & fileName,
68                                         MESH * ptrMesh,
69                                         MED_EN::med_mode_acces access);
70     template<class T, class INTERLACING_TAG>
71     GENDRIVER * buildFieldDriverFromFile(const string & fileName,
72                                          FIELD<T,INTERLACING_TAG> * ptrField,
73                                          MED_EN::med_mode_acces access);
74     GENDRIVER * buildConcreteMedDriverForMesh(const std::string & fileName,
75                                               MESH *mesh,
76                                               const string & driverName,
77                                               MED_EN::med_mode_acces access,
78                                               MED_EN::medFileVersion version);
79     template<class T, class INTERLACING_TAG>
80     GENDRIVER * buildConcreteMedDriverForField(const std::string & fileName,
81                                                FIELD<T,INTERLACING_TAG> *fielde,
82                                                MED_EN::med_mode_acces access,
83                                                MED_EN::medFileVersion version);
84   }
85 }
86
87 #include "MEDMEM_VtkFieldDriver.hxx"
88 #include "MEDMEM_MedFieldDriver.hxx"
89 #include "MEDMEM_MedFieldDriver21.hxx"
90 #include "MEDMEM_MedFieldDriver22.hxx"
91 #include "MEDMEM_AsciiFieldDriver.hxx"
92
93 namespace MEDMEM {
94   template<class T, class INTERLACING_TAG>
95   GENDRIVER * DRIVERFACTORY::buildDriverForField(driverTypes driverType,
96                                                  const std::string & fileName,
97                                                  FIELD<T,INTERLACING_TAG> *field,
98                                                  MED_EN::med_mode_acces access)
99   {
100     GENDRIVER *ret;
101     switch(driverType)
102       {
103       case MED_DRIVER : {
104         switch(access)
105           {
106           case MED_EN::MED_LECT : {
107             ret = new MED_FIELD_RDONLY_DRIVER<T>(fileName,field);
108             break;
109           }
110           case MED_EN::MED_ECRI : {
111             ret= new MED_FIELD_WRONLY_DRIVER<T>(fileName,field);
112             break;
113           }
114           case MED_EN::MED_REMP : {
115             ret = new MED_FIELD_RDWR_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 VTK_DRIVER : {
125         switch(access)
126           {
127           case MED_EN::MED_LECT : {
128             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");
129             break;
130           }
131           case MED_EN::MED_ECRI : {
132             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
133             break;
134           }
135           case MED_EN::MED_REMP : {
136             ret=new VTK_FIELD_DRIVER<T>(fileName,field);
137             break ;
138           }
139           default:
140             throw MED_EXCEPTION ("access type has not been properly specified to the method");
141           }
142         break;
143       }
144
145       case GIBI_DRIVER : {
146         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");
147         break;
148       }
149
150       case PORFLOW_DRIVER : {
151         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");
152         break;
153       }
154
155       case ASCII_DRIVER : {
156         switch(access)
157           {
158           case MED_EN::MED_ECRI : {
159             ret=new ASCII_FIELD_DRIVER<T>(fileName,field);
160             break;
161           }
162           default:
163             throw MED_EXCEPTION ("driver ASCII_DRIVER on FIELD only in write mod");
164           }
165         break;
166       }
167
168       case NO_DRIVER : {
169         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");
170         break;
171       }
172       default:
173         MED_EXCEPTION ("driverType other than MED_DRIVER and VTK_DRIVER has been specified to the method which is not allowed for the object FIELD");
174       }
175     return ret;
176   }
177
178   template<class T, class INTERLACING_TAG>
179   GENDRIVER * DRIVERFACTORY::buildFieldDriverFromFile(const string & fileName,
180                                                       FIELD<T,INTERLACING_TAG> * ptrField,
181                                                       MED_EN::med_mode_acces access)
182   {
183     MED_EN::medFileVersion version;
184
185     try
186       {
187         version = getMedFileVersion(fileName);
188       }
189     catch (MEDEXCEPTION & ex)
190       {
191         version = DRIVERFACTORY::globalMedFileVersionForWriting;
192       }
193
194     MESSAGE("buildFieldDriverFromFile version of the file " << version);
195
196     GENDRIVER * driver;
197
198     switch(access)
199       {
200       case MED_EN::MED_LECT : {
201         if (version == MED_EN::V21)
202           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
203         else if (version == MED_EN::V22)
204           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
205         return driver;
206       }
207       case MED_EN::MED_ECRI : {
208         if (version == MED_EN::V21)
209           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
210         else if (version == MED_EN::V22)
211           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
212         return driver;
213       }
214       case MED_EN::MED_REMP : {
215         if (version == MED_EN::V21)
216           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
217         else if (version == MED_EN::V22)
218           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
219         return driver;
220       }
221       default:
222         throw MED_EXCEPTION ("access type has not been properly specified to the method");
223       }
224   }
225
226   template<class T, class INTERLACING_TAG>
227   GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForField(const std::string & fileName,
228                                                             FIELD<T,INTERLACING_TAG> *ptrField,
229                                                             MED_EN::med_mode_acces access,
230                                                             MED_EN::medFileVersion version)
231   {
232
233     MESSAGE("buildConcreteMedDriverForField version of the file " << version);
234
235     GENDRIVER * driver;
236
237     switch(access)
238       {
239       case MED_EN::MED_LECT : {
240         if (version == MED_EN::V21)
241           driver = new MED_FIELD_RDONLY_DRIVER21<T>(fileName,ptrField);
242         else if (version == MED_EN::V22)
243           driver = new MED_FIELD_RDONLY_DRIVER22<T>(fileName,ptrField);
244         return driver;
245       }
246       case MED_EN::MED_ECRI : {
247         if (version == MED_EN::V21)
248           driver = new MED_FIELD_WRONLY_DRIVER21<T>(fileName,ptrField);
249         else if (version == MED_EN::V22)
250           driver = new MED_FIELD_WRONLY_DRIVER22<T>(fileName,ptrField);
251         return driver;
252       }
253       case MED_EN::MED_REMP : {
254         if (version == MED_EN::V21)
255           driver = new MED_FIELD_RDWR_DRIVER21<T>(fileName,ptrField);
256         else if (version == MED_EN::V22)
257           driver = new MED_FIELD_RDWR_DRIVER22<T>(fileName,ptrField);
258         return driver;
259       }
260       default:
261         throw MED_EXCEPTION ("access type has not been properly specified to the method");
262       }
263   }
264 }
265
266 #endif