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