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