Salome HOME
Added constants to represent job status uniformly. Fixed some errors under Windows...
[tools/libbatch.git] / src / Core / Batch_Parametre.cxx
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 /*
23  * Parametre.cxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Date   : Septembre 2003
27  * Projet : SALOME 2
28  *
29  */
30
31 #include "Batch_Versatile.hxx"
32 #include "Batch_InvalidKeyException.hxx"
33 #include "Batch_Parametre.hxx"
34
35 using namespace std;
36
37 // TODO: These definitions in global namespace are here only to avoid breaking Kernel compilation for now.
38 // They must be removed as soon as possible.
39 def_static_MapKey(ACCOUNT)
40 def_static_MapKey(ARGUMENTS)
41 def_static_MapKey(CHECKPOINT)
42 def_static_MapKey(CKPTINTERVAL)
43 def_static_MapKey(CREATIONTIME)
44 def_static_MapKey(EGROUP)
45 def_static_MapKey(ELIGIBLETIME)
46 def_static_MapKey(ENDTIME)
47 def_static_MapKey(EUSER)
48 def_static_MapKey(EXECUTABLE)
49 def_static_MapKey(EXECUTIONHOST)
50 def_static_MapKey(EXITCODE)
51 def_static_MapKey(HOLD)
52 def_static_MapKey(ID)
53 def_static_MapKey(INFILE)
54 def_static_MapKey(MAIL)
55 def_static_MapKey(MAXCPUTIME)
56 def_static_MapKey(MAXDISKSIZE)
57 def_static_MapKey(MAXRAMSIZE)
58 def_static_MapKey(MAXWALLTIME)
59 def_static_MapKey(MODIFICATIONTIME)
60 def_static_MapKey(NAME)
61 def_static_MapKey(NBPROC)
62 def_static_MapKey(OUTFILE)
63 def_static_MapKey(PID)
64 def_static_MapKey(QUEUE)
65 def_static_MapKey(QUEUEDTIME)
66 def_static_MapKey(SERVER)
67 def_static_MapKey(STARTTIME)
68 def_static_MapKey(STATE)
69 def_static_MapKey(TEXT)
70 def_static_MapKey(TMPDIR)
71 def_static_MapKey(USEDCPUTIME)
72 def_static_MapKey(USEDDISKSIZE)
73 def_static_MapKey(USEDRAMSIZE)
74 def_static_MapKey(USEDWALLTIME)
75 def_static_MapKey(USER)
76 def_static_MapKey(WORKDIR)
77 def_static_MapKey(HOMEDIR)
78
79 namespace Batch {
80
81   // Constructeur standard
82   // La map interne TypeMap possede les memes clefs que la map principale, mais les
83   // valeurs associees contiennent le type des clefs de la map principale ainsi que
84   // le nombre de valeurs autorisees dans l'objet Versatile (0=nombre quelconque,
85   // sinon valeur precisee)
86   Parametre::Parametre() : map< string, Versatile >()
87   {
88     TypeMap[ACCOUNT].type = STRING;
89     TypeMap[ACCOUNT].maxelem = 1;
90
91     TypeMap[ARGUMENTS].type = STRING;
92     TypeMap[ARGUMENTS].maxelem = 0;
93
94     TypeMap[CHECKPOINT].type = LONG;
95     TypeMap[CHECKPOINT].maxelem = 1;
96
97     TypeMap[CKPTINTERVAL].type = LONG;
98     TypeMap[CKPTINTERVAL].maxelem = 1;
99
100     TypeMap[CREATIONTIME].type = LONG;
101     TypeMap[CREATIONTIME].maxelem = 1;
102
103     TypeMap[EGROUP].type = STRING;
104     TypeMap[EGROUP].maxelem = 1;
105
106     TypeMap[ELIGIBLETIME].type = LONG;
107     TypeMap[ELIGIBLETIME].maxelem = 1;
108
109     TypeMap[ENDTIME].type = LONG;
110     TypeMap[ENDTIME].maxelem = 1;
111
112     TypeMap[EUSER].type = STRING;
113     TypeMap[EUSER].maxelem = 1;
114
115     TypeMap[EXECUTABLE].type = STRING;
116     TypeMap[EXECUTABLE].maxelem = 1;
117
118     TypeMap[EXECUTIONHOST].type = STRING;
119     TypeMap[EXECUTIONHOST].maxelem = 0;
120
121     TypeMap[EXITCODE].type = LONG;
122     TypeMap[EXITCODE].maxelem = 1;
123
124     TypeMap[HOLD].type = LONG;
125     TypeMap[HOLD].maxelem = 1;
126
127     TypeMap[ID].type = STRING;
128     TypeMap[ID].maxelem = 1;
129
130     TypeMap[INFILE].type = COUPLE;
131     TypeMap[INFILE].maxelem = 0;
132
133     TypeMap[MAIL].type = STRING;
134     TypeMap[MAIL].maxelem = 1;
135
136     TypeMap[MAXCPUTIME].type = LONG;
137     TypeMap[MAXCPUTIME].maxelem = 1;
138
139     TypeMap[MAXDISKSIZE].type = LONG;
140     TypeMap[MAXDISKSIZE].maxelem = 1;
141
142     TypeMap[MAXRAMSIZE].type = LONG;
143     TypeMap[MAXRAMSIZE].maxelem = 1;
144
145     TypeMap[MAXWALLTIME].type = LONG;
146     TypeMap[MAXWALLTIME].maxelem = 1;
147
148     TypeMap[MODIFICATIONTIME].type = LONG;
149     TypeMap[MODIFICATIONTIME].maxelem = 1;
150
151     TypeMap[NAME].type = STRING;
152     TypeMap[NAME].maxelem = 1;
153
154     TypeMap[NBPROC].type = LONG;
155     TypeMap[NBPROC].maxelem = 1;
156
157     TypeMap[OUTFILE].type = COUPLE;
158     TypeMap[OUTFILE].maxelem = 0;
159
160     TypeMap[PID].type = LONG;
161     TypeMap[PID].maxelem = 1;
162
163     TypeMap[QUEUE].type = STRING;
164     TypeMap[QUEUE].maxelem = 1;
165
166     TypeMap[QUEUEDTIME].type = LONG;
167     TypeMap[QUEUEDTIME].maxelem = 1;
168
169     TypeMap[SERVER].type = STRING;
170     TypeMap[SERVER].maxelem = 1;
171
172     TypeMap[STARTTIME].type = LONG;
173     TypeMap[STARTTIME].maxelem = 1;
174
175     TypeMap[STATE].type = STRING;
176     TypeMap[STATE].maxelem = 1;
177
178     TypeMap[TEXT].type = STRING;
179     TypeMap[TEXT].maxelem = 1;
180
181     TypeMap[TMPDIR].type = STRING;
182     TypeMap[TMPDIR].maxelem = 1;
183
184     TypeMap[USEDCPUTIME].type = LONG;
185     TypeMap[USEDCPUTIME].maxelem = 1;
186
187     TypeMap[USEDDISKSIZE].type = LONG;
188     TypeMap[USEDDISKSIZE].maxelem = 1;
189
190     TypeMap[USEDRAMSIZE].type = LONG;
191     TypeMap[USEDRAMSIZE].maxelem = 1;
192
193     TypeMap[USEDWALLTIME].type = LONG;
194     TypeMap[USEDWALLTIME].maxelem = 1;
195
196     TypeMap[USER].type = STRING;
197     TypeMap[USER].maxelem = 1;
198
199     TypeMap[WORKDIR].type = STRING;
200     TypeMap[WORKDIR].maxelem = 1;
201
202     TypeMap[HOMEDIR].type = STRING;
203     TypeMap[HOMEDIR].maxelem = 1;
204   }
205
206   // Operateur de recherche dans la map
207   // Cet operateur agit sur les objets NON CONSTANTS, il autorise la modification de
208   // la valeur associ�e � la clef car il retourne une reference non constante
209   Versatile & Parametre::operator [] (const string & mk)
210   {
211     // On controle que la clef est valide
212     if (TypeMap.find(mk) == TypeMap.end()) throw InvalidKeyException(mk.c_str());
213
214     // On recherche la valeur associee...
215     Versatile & V = map< string, Versatile >::operator [] (mk);
216
217     // ... et on l'initialise systematiquement
218     // ATTENTION : si un probleme de type survient (ie, on stocke une valeur d'un type
219     // different de celui inscrit dans TypeMap) une exception TypeMismatchException est
220     // levee
221     V.setName(mk);
222     V.setType(TypeMap[mk].type);
223     V.setMaxSize(TypeMap[mk].maxelem);
224
225     return V;
226   }
227
228   // Operateur de recherche dans la map
229   // Cet operateur agit sur les objets CONSTANTS
230   const Versatile & Parametre::operator [] (const string & mk) const
231   {
232     // On controle que la clef est valide
233     if (TypeMap.find(mk) == TypeMap.end()) throw InvalidKeyException(mk.c_str());
234
235     // On recherche la valeur associee
236     Parametre::const_iterator it = find(mk);
237     if (it == end()) throw InvalidKeyException(mk.c_str());
238     const Versatile & V = (*it).second;
239
240     return V;
241   }
242
243   // Operateur d'affectation
244   Parametre & Parametre::operator =(const Parametre & PM)
245   {
246     // On ne reaffecte pas l'objet a lui-meme, sinon aie, aie, aie
247     if (this == &PM) return *this;
248
249     // On efface toute la map
250     erase(begin(), end());
251
252     // On recopie la map interne
253     // Meme si cela ne sert a rien pour le moment car les maps internes sont identiques,
254     // il n'est pas exclu que dans un avenir proche elles puissent etre differentes
255     (*this).TypeMap = PM.TypeMap;
256
257     // On recree la structure interne de la map avec les valeurs de celle passee en argument
258     Parametre::const_iterator it;
259     for(it=PM.begin(); it!=PM.end(); it++)
260       insert(make_pair( (*it).first ,
261                         Versatile( (*it).second)
262                         ) );
263
264     return *this;
265   }
266
267   // Constructeur par recopie
268   Parametre::Parametre(const Parametre & PM) : map< string, Versatile >()
269   {
270     // inutile car l'objet est vierge : il vient d'etre cree
271     // On efface toute la map
272     // erase(begin(), end());
273
274     // On recopie la map interne
275     (*this).TypeMap = PM.TypeMap;
276
277     // On cree la structure interne de la map avec les valeurs de celle passee en argument
278     Parametre::const_iterator it;
279     for(it=PM.begin();
280         it!=PM.end();
281         it++)
282       insert(
283              make_pair(
284                        (*it).first ,
285                        Versatile( (*it).second)
286                        ) );
287   }
288
289   //   map< string, TypeParam > Parametre::getTypeMap() const
290   //   {
291   //     return TypeMap;
292   //   }
293
294 }