/*
* Versatile.cxx :
*
- * Auteur : Ivan DUTKA-MALEN - EDF R&D
- * Date : Septembre 2003
- * Projet : SALOME 2
+ * Author : Ivan DUTKA-MALEN - EDF R&D
+ * Date : September 2003
*
*/
#include <list>
#include <string>
#include <sstream>
-#include <assert.h>
-//#include "MEDMEM_STRING.hxx"
+
#include "Batch_GenericType.hxx"
-#include "Batch_IntType.hxx"
#include "Batch_BoolType.hxx"
#include "Batch_CharType.hxx"
#include "Batch_LongType.hxx"
#include "Batch_Versatile.hxx"
#include "Batch_TypeMismatchException.hxx"
#include "Batch_ListIsFullException.hxx"
+
using namespace std;
namespace Batch {
- // Constructeur par recopie
- Versatile::Versatile(const Versatile & V) : _discriminator(V._discriminator), _maxsize(V._maxsize), _name(V._name) // , _str_value(0)
+ Versatile::Versatile(DiscriminatorType discriminator, size_type maxsize, std::string name)
+ : _discriminator(discriminator),
+ _maxsize(maxsize),
+ _name(name)
{
- Versatile::const_iterator it;
-
- // On prend un a un les elements de l'objet passe en argument qu'on duplique
- for(it=V.begin(); it!=V.end(); it++)
- push_back( (*it)->clone() ); // Attention, la methode clone fait un new implicite
}
- // Destructeur
- Versatile::~Versatile()
+ Versatile::Versatile(const Versatile & V)
+ : _discriminator(V._discriminator),
+ _maxsize(V._maxsize),
+ _name(V._name)
{
- eraseAll();
+ Versatile::const_iterator it;
+ for(it=V.begin(); it!=V.end(); it++)
+ push_back( (*it)->clone() );
}
- // Operateur d'affectation entre objets
- Versatile & Versatile::operator = (const Versatile & Vrhs) throw(TypeMismatchException)
+ Versatile::~Versatile()
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(Vrhs._discriminator);
- setMaxSize(Vrhs._maxsize);
- _name = Vrhs._name;
-
- // On efface les donnees precedentes
eraseAll();
-
- // On copie les donnees de Vrhs
- Versatile::const_iterator it;
-
- for(it=Vrhs.begin(); it!=Vrhs.end(); it++)
- push_back( (*it)->clone() ); // Attention, la methode clone fait un new implicite
-
- return *this;
}
- // Operateur d'affectation a partir d'un long
- Versatile & Versatile::operator = (const long l) throw(TypeMismatchException)
+ Versatile & Versatile::operator = (const long l) throw(TypeMismatchException)
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(LONG);
-
- // On efface les donnees precedentes
+ checkType(LONG);
eraseAll();
-
- // On ajoute un element interne de type long a l'objet
- LongType * pL = new LongType(l);
- assert(pL != 0);
- push_back(pL);
+ push_back(new LongType(l));
return *this;
}
- // Operateur d'affectation a partir d'une string
Versatile & Versatile::operator = (const string & ch) throw(TypeMismatchException)
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(STRING);
-
- // On efface les donnees precedentes
+ checkType(STRING);
eraseAll();
-
- // On ajoute un element interne de type string a l'objet
- StringType * pS = new StringType(ch);
- assert(pS != 0);
- push_back(pS);
-
+ push_back(new StringType(ch));
return *this;
}
- // Operateur de concatenation a partir d'une string
Versatile & Versatile::operator +=(const string & ch) throw(TypeMismatchException,ListIsFullException)
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(STRING);
+ checkType(STRING);
- // Si la taille maximale est atteinte, on leve une exception ListIsFullException
- if (_maxsize == 0) push_back(new StringType(ch));
- else if ((_maxsize > 0) && (size() < _maxsize)) push_back(new StringType(ch));
+ // If max size is reached, throw a ListIsFullException
+ if (_maxsize == 0 || size() < _maxsize)
+ push_back(new StringType(ch));
else {
- //MEDMEM::STRING msg;
ostringstream msg;
- msg << "Taille maximum : " << _maxsize;
+ msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
throw(ListIsFullException(msg.str()));
}
return *this;
}
- // Operateur de concatenation a partir d'une string
Versatile & Versatile::operator , (const string & ch) throw(TypeMismatchException,ListIsFullException)
{
*this += ch;
return *this;
}
- // Operateur d'affectation a partir d'un Couple
- Versatile & Versatile::operator = (const Couple & cp) throw(TypeMismatchException)
+ Versatile & Versatile::operator = (const char * ch) throw(TypeMismatchException)
+ {
+ return operator=(string(ch));
+ }
+
+ Versatile & Versatile::operator +=(const char * ch) throw(TypeMismatchException,ListIsFullException)
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(COUPLE);
+ return operator+=(string(ch));
+ }
- // On efface les donnees precedentes
- eraseAll();
-
- // On ajoute un element interne de type Couple a l'objet
- CoupleType * pC = new CoupleType(cp);
- assert(pC != 0);
- push_back(pC);
+ Versatile & Versatile::operator , (const char * ch) throw(TypeMismatchException,ListIsFullException)
+ {
+ return operator,(string(ch));
+ }
+ Versatile & Versatile::operator = (const Couple & cp) throw(TypeMismatchException)
+ {
+ checkType(COUPLE);
+ eraseAll();
+ push_back(new CoupleType(cp));
return *this;
}
- // Operateur de concatenation a partir d'un Couple
Versatile & Versatile::operator +=(const Couple & cp) throw(TypeMismatchException,ListIsFullException)
{
- // ATTENTION : le forçage de type leve une exception TypeMismatchException entre cas de conflit
- setType(COUPLE);
-
- // Si la taille maximale est atteinte, on leve une exception ListIsFullException
- if (_maxsize == 0) push_back(new CoupleType(cp));
- else if ((_maxsize > 0) && (size() < _maxsize)) push_back(new CoupleType(cp));
+ checkType(COUPLE);
+ // If max size is reached, throw a ListIsFullException
+ if (_maxsize == 0 || size() < _maxsize)
+ push_back(new CoupleType(cp));
else {
- //MEDMEM::STRING msg;
ostringstream msg;
- msg << "Taille maximum : " << _maxsize;
+ msg << "Maximum size for \"" << _name << "\" is reached: " << _maxsize;
throw(ListIsFullException(msg.str()));
}
return *this;
}
- // Operateur de concatenation a partir d'un Couple
Versatile & Versatile::operator , (const Couple & cp) throw(TypeMismatchException,ListIsFullException)
{
*this += cp;
return os;
}
- // Positionnement du type de l'element interne
- void Versatile::setType(DiscriminatorType t) throw(TypeMismatchException)
+ Versatile & Versatile::operator = (const int i) throw(TypeMismatchException)
{
- // Si le type est deja defini et ne correspond pas au type en argument
- // une exception TypeMismatchException est levee
- if ( (_discriminator == UNDEFINED) || (_discriminator == t) )
- _discriminator = t;
- else {
- //MEDMEM::STRING sst;
- ostringstream sst;
- sst << "Trying to change type of Versatile object \""
- << _name << "\"";
- throw(TypeMismatchException(sst.str()));
- }
+ checkType(LONG);
+ eraseAll();
+ push_back(new LongType((long)i));
+ return *this;
}
-
- // Positionnement du nombre d'elements internes
- void Versatile::setMaxSize(int i)
+
+ Versatile & Versatile::operator = (const bool b) throw(TypeMismatchException)
{
- _maxsize = i;
- if (i <= 0) return;
- // Si la nouvelle taille est inferieure au nombre d'elements deja
- // presents, les elements en surplus sont effaces (troncature)
- if (size() > _maxsize)
- {
- int reste = size() - _maxsize;
- Versatile::iterator it;
- for(it=end(); (it!=begin()) && reste; it--, reste--)
- {
- delete back();
- pop_back();
- }
- }
+ checkType(BOOL);
+ eraseAll();
+ push_back(new BoolType(b));
+ return *this;
}
+ void Versatile::checkType(DiscriminatorType t) const throw(TypeMismatchException)
+ {
+ if (_discriminator != t)
+ throw (TypeMismatchException("Trying to change type of Versatile object \"" + _name + "\""));
+ }
- // Conversion de type vers un long
Versatile::operator long() const throw(TypeMismatchException)
{
- // Si le type ne correspond pas ou si la liste contient plus d'un element,
- // la conversion est impossible et une exception TypeMismatchException
- // est levee
- if ( (_maxsize != 1) || (_discriminator != LONG) || (size() == 0) ) {
- //MEDMEM::STRING sst;
+ // If the type does not correspond or if the list has more than one element,
+ // throw a TypeMismatchException
+ if ( _maxsize != 1 || _discriminator != LONG || size() == 0 ) {
ostringstream sst;
- sst << "Cannot cast Versatile object \""
- << _name << "\" to long";
+ sst << "Cannot cast Versatile object \"" << _name << "\" to long";
+ throw (TypeMismatchException(sst.str()));
+ }
+ return *( static_cast<LongType *>(this->front()) );
+ }
+
+ Versatile::operator bool() const throw(TypeMismatchException)
+ {
+ // If the type does not correspond or if the list has more than one element,
+ // throw a TypeMismatchException
+ if ( _maxsize != 1 || _discriminator != BOOL || size() == 0 ) {
+ ostringstream sst;
+ sst << "Cannot cast Versatile object \"" << _name << "\" to bool";
throw(TypeMismatchException(sst.str()));
}
- return *( static_cast<LongType *>(this->front()) );
+ return *( static_cast<BoolType *>(this->front()) );
+ }
+
+ Versatile::operator int() const throw(TypeMismatchException)
+ {
+ return operator long();
}
- // Conversion de type vers un Couple
Versatile::operator Couple() const throw(TypeMismatchException)
{
- // Si le type ne correspond pas ou si la liste contient plus d'un element,
- // la conversion est impossible et une exception TypeMismatchException
- // est levee
- if ( (_maxsize != 1) || (_discriminator != COUPLE) || (size() == 0) ) {
- //MEDMEM::STRING sst;
+ // If the type does not correspond or if the list has more than one element,
+ // throw a TypeMismatchException
+ if ( _maxsize != 1 || _discriminator != COUPLE || size() == 0 ) {
ostringstream sst;
- sst << "Cannot cast Versatile object \""
- << _name << "\" to Couple";
+ sst << "Cannot cast Versatile object \"" << _name << "\" to Couple";
throw(TypeMismatchException(sst.str()));
}
- return *( static_cast<CoupleType *>(this->front()) );
+ return *( static_cast<CoupleType *>(this->front()) );
}
- // Conversion de type vers une string
string Versatile::str() const throw(TypeMismatchException)
{
- // Si le type ne correspond pas, la conversion est impossible et
- // une exception TypeMismatchException est levee
- if ( (_discriminator != STRING) || (size() == 0) ) {
- //MEDMEM::STRING sst;
+ // If the type does not correspond, throw a TypeMismatchException
+ if ( _discriminator != STRING || size() == 0 ) {
ostringstream sst;
- sst << "Cannot cast Versatile object \""
- << _name << "\" to string";
+ sst << "Cannot cast Versatile object \"" << _name << "\" to string";
throw(TypeMismatchException(sst.str()));
}
- // La chaine renvoyee est la concatenation des chaines internes
+ // The returned string is the concatenation of internal strings
string s;
Versatile::const_iterator it;
const char * sep = "";
return s;
}
- // Conversion de type vers une string
Versatile::operator string () const throw(TypeMismatchException)
{
return str();
}
- // Efface tous les elements internes de l'objet
void Versatile::eraseAll()
{
- while(!empty())
- {
- delete back();
- pop_back();
- }
+ while(!empty()) {
+ delete back();
+ pop_back();
+ }
}
-
- // Recuperation du type de l'element interne
DiscriminatorType Versatile::getType() const
{
return _discriminator;
}
- // Recuperation du nom de l'objet
- string Versatile::getName() const
+ Versatile::size_type Versatile::getMaxSize() const
{
- return _name;
+ return _maxsize;
}
- // Positionnement du nom de l'objet
- void Versatile::setName(const string & name)
+ const string & Versatile::getName() const
{
- _name = name;
+ return _name;
}
+
}