1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 * Author : Ivan DUTKA-MALEN - EDF R&D
26 * Date : September 2003
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"
48 Versatile::Versatile(DiscriminatorType discriminator, size_type maxsize, std::string name)
49 : _discriminator(discriminator),
55 Versatile::Versatile(const Versatile & V)
56 : _discriminator(V._discriminator),
60 Versatile::const_iterator it;
61 for(it=V.begin(); it!=V.end(); it++)
62 push_back( (*it)->clone() );
65 Versatile::~Versatile()
70 Versatile & Versatile::operator = (const long l) throw(TypeMismatchException)
74 push_back(new LongType(l));
78 Versatile & Versatile::operator = (const string & ch) throw(TypeMismatchException)
82 push_back(new StringType(ch));
86 Versatile & Versatile::operator +=(const string & ch) throw(TypeMismatchException,ListIsFullException)
90 // If max size is reached, throw a ListIsFullException
91 if (_maxsize == 0 || size() < _maxsize)
92 push_back(new StringType(ch));
95 msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
96 throw(ListIsFullException(msg.str()));
101 Versatile & Versatile::operator , (const string & ch) throw(TypeMismatchException,ListIsFullException)
107 Versatile & Versatile::operator = (const char * ch) throw(TypeMismatchException)
109 return operator=(string(ch));
112 Versatile & Versatile::operator +=(const char * ch) throw(TypeMismatchException,ListIsFullException)
114 return operator+=(string(ch));
117 Versatile & Versatile::operator , (const char * ch) throw(TypeMismatchException,ListIsFullException)
119 return operator,(string(ch));
122 Versatile & Versatile::operator = (const Couple & cp) throw(TypeMismatchException)
126 push_back(new CoupleType(cp));
130 Versatile & Versatile::operator +=(const Couple & cp) throw(TypeMismatchException,ListIsFullException)
133 // If max size is reached, throw a ListIsFullException
134 if (_maxsize == 0 || size() < _maxsize)
135 push_back(new CoupleType(cp));
138 msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
139 throw(ListIsFullException(msg.str()));
144 Versatile & Versatile::operator , (const Couple & cp) throw(TypeMismatchException,ListIsFullException)
150 ostream & operator << (ostream & os, const Versatile & V)
152 Versatile::const_iterator it;
153 const char * sep = "";
155 for(it=V.begin(); it!=V.end(); it++, sep=" ") {
156 string s = (*it)->affiche();
162 Versatile & Versatile::operator = (const int i) throw(TypeMismatchException)
166 push_back(new LongType((long)i));
170 Versatile & Versatile::operator = (const bool b) throw(TypeMismatchException)
174 push_back(new BoolType(b));
178 void Versatile::checkType(DiscriminatorType t) const throw(TypeMismatchException)
180 if (_discriminator != t)
181 throw (TypeMismatchException("Trying to change type of Versatile object \"" + _name + "\""));
184 Versatile::operator long() const throw(TypeMismatchException)
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 ) {
190 sst << "Cannot cast Versatile object \"" << _name << "\" to long";
191 throw (TypeMismatchException(sst.str()));
193 return *( static_cast<LongType *>(this->front()) );
196 Versatile::operator bool() const throw(TypeMismatchException)
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 ) {
202 sst << "Cannot cast Versatile object \"" << _name << "\" to bool";
203 throw(TypeMismatchException(sst.str()));
205 return *( static_cast<BoolType *>(this->front()) );
208 Versatile::operator int() const throw(TypeMismatchException)
210 return operator long();
213 Versatile::operator Couple() const throw(TypeMismatchException)
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 ) {
219 sst << "Cannot cast Versatile object \"" << _name << "\" to Couple";
220 throw(TypeMismatchException(sst.str()));
222 return *( static_cast<CoupleType *>(this->front()) );
225 string Versatile::str() const throw(TypeMismatchException)
227 // If the type does not correspond, throw a TypeMismatchException
228 if ( _discriminator != STRING || size() == 0 ) {
230 sst << "Cannot cast Versatile object \"" << _name << "\" to string";
231 throw(TypeMismatchException(sst.str()));
234 // The returned string is the concatenation of internal strings
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));
244 Versatile::operator string () const throw(TypeMismatchException)
249 void Versatile::eraseAll()
257 DiscriminatorType Versatile::getType() const
259 return _discriminator;
262 Versatile::size_type Versatile::getMaxSize() const
267 const string & Versatile::getName() const