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_LongType.hxx"
38 #include "Batch_StringType.hxx"
39 #include "Batch_Versatile.hxx"
40 #include "Batch_TypeMismatchException.hxx"
41 #include "Batch_ListIsFullException.hxx"
47 Versatile::Versatile(DiscriminatorType discriminator, size_type maxsize, std::string name)
48 : _discriminator(discriminator),
54 Versatile::Versatile(const Versatile & V)
55 : _discriminator(V._discriminator),
59 Versatile::const_iterator it;
60 for(it=V.begin(); it!=V.end(); it++)
61 push_back( (*it)->clone() );
64 Versatile::~Versatile()
69 Versatile & Versatile::operator = (const long l) throw(TypeMismatchException)
73 push_back(new LongType(l));
77 Versatile & Versatile::operator = (const string & ch) throw(TypeMismatchException)
81 push_back(new StringType(ch));
85 Versatile & Versatile::operator +=(const string & ch) throw(TypeMismatchException,ListIsFullException)
89 // If max size is reached, throw a ListIsFullException
90 if (_maxsize == 0 || size() < _maxsize)
91 push_back(new StringType(ch));
94 msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
95 throw(ListIsFullException(msg.str()));
100 Versatile & Versatile::operator , (const string & ch) throw(TypeMismatchException,ListIsFullException)
106 Versatile & Versatile::operator = (const char * ch) throw(TypeMismatchException)
108 return operator=(string(ch));
111 Versatile & Versatile::operator +=(const char * ch) throw(TypeMismatchException,ListIsFullException)
113 return operator+=(string(ch));
116 Versatile & Versatile::operator , (const char * ch) throw(TypeMismatchException,ListIsFullException)
118 return operator,(string(ch));
121 Versatile & Versatile::operator = (const Couple & cp) throw(TypeMismatchException)
125 push_back(new CoupleType(cp));
129 Versatile & Versatile::operator +=(const Couple & cp) throw(TypeMismatchException,ListIsFullException)
132 // If max size is reached, throw a ListIsFullException
133 if (_maxsize == 0 || size() < _maxsize)
134 push_back(new CoupleType(cp));
137 msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
138 throw(ListIsFullException(msg.str()));
143 Versatile & Versatile::operator , (const Couple & cp) throw(TypeMismatchException,ListIsFullException)
149 ostream & operator << (ostream & os, const Versatile & V)
151 Versatile::const_iterator it;
152 const char * sep = "";
154 for(it=V.begin(); it!=V.end(); it++, sep=" ") {
155 string s = (*it)->affiche();
161 Versatile & Versatile::operator = (const int i) throw(TypeMismatchException)
165 push_back(new LongType((long)i));
169 Versatile & Versatile::operator = (const bool b) throw(TypeMismatchException)
173 push_back(new BoolType(b));
177 void Versatile::checkType(DiscriminatorType t) const throw(TypeMismatchException)
179 if (_discriminator != t)
180 throw (TypeMismatchException("Trying to change type of Versatile object \"" + _name + "\""));
183 Versatile::operator long() const throw(TypeMismatchException)
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 ) {
189 sst << "Cannot cast Versatile object \"" << _name << "\" to long";
190 throw (TypeMismatchException(sst.str()));
192 return *( static_cast<LongType *>(this->front()) );
195 Versatile::operator bool() const throw(TypeMismatchException)
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 ) {
201 sst << "Cannot cast Versatile object \"" << _name << "\" to bool";
202 throw(TypeMismatchException(sst.str()));
204 return *( static_cast<BoolType *>(this->front()) );
207 Versatile::operator int() const throw(TypeMismatchException)
209 return operator long();
212 Versatile::operator Couple() const throw(TypeMismatchException)
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 ) {
218 sst << "Cannot cast Versatile object \"" << _name << "\" to Couple";
219 throw(TypeMismatchException(sst.str()));
221 return *( static_cast<CoupleType *>(this->front()) );
224 string Versatile::str() const throw(TypeMismatchException)
226 // If the type does not correspond, throw a TypeMismatchException
227 if ( _discriminator != STRING || size() == 0 ) {
229 sst << "Cannot cast Versatile object \"" << _name << "\" to string";
230 throw(TypeMismatchException(sst.str()));
233 // The returned string is the concatenation of internal strings
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));
243 Versatile::operator string () const throw(TypeMismatchException)
248 void Versatile::eraseAll()
256 DiscriminatorType Versatile::getType() const
258 return _discriminator;
261 Versatile::size_type Versatile::getMaxSize() const
266 const string & Versatile::getName() const