Salome HOME
Remove unused job parameters, unused types and deprecated doc
[tools/libbatch.git] / src / Core / Batch_Versatile.cxx
1 //  Copyright (C) 2007-2012  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  * Versatile.cxx : 
24  *
25  * Author : Ivan DUTKA-MALEN - EDF R&D
26  * Date   : September 2003
27  *
28  */
29
30 #include <iostream>
31 #include <list>
32 #include <string>
33 #include <sstream>
34
35 #include "Batch_GenericType.hxx"
36 #include "Batch_BoolType.hxx"
37 #include "Batch_LongType.hxx"
38 #include "Batch_StringType.hxx"
39 #include "Batch_Versatile.hxx"
40 #include "Batch_TypeMismatchException.hxx"
41 #include "Batch_ListIsFullException.hxx"
42
43 using namespace std;
44
45 namespace Batch {
46
47   Versatile::Versatile(DiscriminatorType discriminator, size_type maxsize, std::string name)
48     : _discriminator(discriminator),
49       _maxsize(maxsize),
50       _name(name)
51   {
52   }
53
54   Versatile::Versatile(const Versatile & V)
55    : _discriminator(V._discriminator),
56      _maxsize(V._maxsize),
57      _name(V._name)
58   {
59     Versatile::const_iterator it;
60     for(it=V.begin(); it!=V.end(); it++)
61       push_back( (*it)->clone() );
62   }
63
64   Versatile::~Versatile()
65   {
66     eraseAll();
67   }
68
69   Versatile & Versatile::operator = (const long l) throw(TypeMismatchException)
70   {
71     checkType(LONG);
72     eraseAll();
73     push_back(new LongType(l));
74     return *this;
75   }
76
77   Versatile & Versatile::operator = (const string & ch) throw(TypeMismatchException)
78   {
79     checkType(STRING);
80     eraseAll();
81     push_back(new StringType(ch));
82     return *this;
83   }
84
85   Versatile & Versatile::operator +=(const string & ch) throw(TypeMismatchException,ListIsFullException)
86   {
87     checkType(STRING);
88
89         // If max size is reached, throw a ListIsFullException
90     if (_maxsize == 0 || size() < _maxsize)
91       push_back(new StringType(ch));
92     else {
93       ostringstream msg;
94       msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
95       throw(ListIsFullException(msg.str()));
96     }
97     return *this;
98   }
99
100   Versatile & Versatile::operator , (const string & ch) throw(TypeMismatchException,ListIsFullException)
101   {
102     *this += ch;
103     return *this;
104   }
105
106   Versatile & Versatile::operator = (const char * ch) throw(TypeMismatchException)
107   {
108     return operator=(string(ch));
109   }
110
111   Versatile & Versatile::operator +=(const char * ch) throw(TypeMismatchException,ListIsFullException)
112   {
113     return operator+=(string(ch));
114   }
115
116   Versatile & Versatile::operator , (const char * ch) throw(TypeMismatchException,ListIsFullException)
117   {
118     return operator,(string(ch));
119   }
120
121   Versatile & Versatile::operator = (const Couple & cp) throw(TypeMismatchException)
122   {
123     checkType(COUPLE);
124     eraseAll();
125     push_back(new CoupleType(cp));
126     return *this;
127   }
128
129   Versatile & Versatile::operator +=(const Couple & cp) throw(TypeMismatchException,ListIsFullException)
130   {
131     checkType(COUPLE);
132     // If max size is reached, throw a ListIsFullException
133     if (_maxsize == 0 || size() < _maxsize)
134       push_back(new CoupleType(cp));
135     else {
136       ostringstream msg;
137       msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
138       throw(ListIsFullException(msg.str()));
139     }
140     return *this;
141   }
142
143   Versatile & Versatile::operator , (const Couple & cp) throw(TypeMismatchException,ListIsFullException)
144   {
145     *this += cp;
146     return *this;
147   }
148
149   ostream & operator << (ostream & os, const Versatile & V)
150   {
151     Versatile::const_iterator it;
152     const char * sep = "";
153
154     for(it=V.begin(); it!=V.end(); it++, sep=" ") {
155       string s = (*it)->affiche();
156       os << sep << s;
157     }
158     return os;
159   }
160
161   Versatile & Versatile::operator = (const int i) throw(TypeMismatchException)
162   {
163     checkType(LONG);
164     eraseAll();
165     push_back(new LongType((long)i));
166     return *this;
167   }
168
169   Versatile & Versatile::operator = (const bool b) throw(TypeMismatchException)
170   {
171     checkType(BOOL);
172     eraseAll();
173     push_back(new BoolType(b));
174     return *this;
175   }
176
177   void Versatile::checkType(DiscriminatorType t) const throw(TypeMismatchException)
178   {
179     if (_discriminator != t)
180       throw (TypeMismatchException("Trying to change type of Versatile object \"" + _name + "\""));
181   }
182
183   Versatile::operator long() const throw(TypeMismatchException)
184   {
185     // If the type does not correspond or if the list has more than one element,
186     // throw a TypeMismatchException
187     if ( _maxsize != 1 || _discriminator != LONG || size() == 0 ) {
188       ostringstream sst;
189       sst << "Cannot cast Versatile object \"" << _name << "\" to long";
190       throw (TypeMismatchException(sst.str()));
191     }
192         return *( static_cast<LongType *>(this->front()) );
193   }
194
195   Versatile::operator bool() const throw(TypeMismatchException)
196   {
197     // If the type does not correspond or if the list has more than one element,
198     // throw a TypeMismatchException
199     if ( _maxsize != 1 || _discriminator != BOOL || size() == 0 ) {
200       ostringstream sst;
201       sst << "Cannot cast Versatile object \"" << _name << "\" to bool";
202       throw(TypeMismatchException(sst.str()));
203     }
204     return *( static_cast<BoolType *>(this->front()) );
205   }
206
207   Versatile::operator int() const throw(TypeMismatchException)
208   {
209     return operator long();
210   }
211
212   Versatile::operator Couple() const throw(TypeMismatchException)
213   {
214     // If the type does not correspond or if the list has more than one element,
215     // throw a TypeMismatchException
216     if ( _maxsize != 1 || _discriminator != COUPLE || size() == 0 ) {
217       ostringstream sst;
218       sst << "Cannot cast Versatile object \"" << _name << "\" to Couple";
219       throw(TypeMismatchException(sst.str()));
220     }
221     return *( static_cast<CoupleType *>(this->front()) );
222   }
223
224   string Versatile::str() const throw(TypeMismatchException)
225   {
226     // If the type does not correspond, throw a TypeMismatchException
227     if ( _discriminator != STRING || size() == 0 ) {
228       ostringstream sst;
229       sst << "Cannot cast Versatile object \"" << _name << "\" to string";
230       throw(TypeMismatchException(sst.str()));
231     }
232
233         // The returned string is the concatenation of internal strings
234     string s;
235     Versatile::const_iterator it;
236     const char * sep = "";
237     for(it=begin(); it!=end(); it++, s+=sep, sep=" ")
238       s += *( static_cast<StringType *>(*it));
239
240     return s;
241   }
242
243   Versatile::operator string () const throw(TypeMismatchException)
244   {
245     return str();
246   }
247
248   void Versatile::eraseAll()
249   {
250     while(!empty()) {
251       delete back();
252       pop_back();
253     }
254   }
255
256   DiscriminatorType Versatile::getType() const
257   {
258     return _discriminator;
259   }
260
261   Versatile::size_type Versatile::getMaxSize() const
262   {
263     return _maxsize;
264   }
265
266   const string & Versatile::getName() const
267   {
268     return _name;
269   }
270
271 }