#ifndef __ATTRIBUT__ #define __ATTRIBUT__ #include "base_attribut.hpp" using XMLIOSERVER::BaseAttribut; using XMLIOSERVER::XMLIOUndefinedValueException; using std::ostringstream; using namespace blitz ; namespace XMLIOSERVER { template class Attribut : public BaseAttribut { public : Attribut(void) : BaseAttribut(), _hasValue(false) { /* Ne rien faire de plus */ } Attribut(const Ctype& value_) : BaseAttribut(), _hasValue(true), value(value_) { /* Ne rien faire de plus */ } Attribut(const Attribut& attr) : BaseAttribut(attr), _hasValue(attr._hasValue) { if (_hasValue) value = attr.value ; } Attribut& operator = (const Attribut & attr) { _hasValue = attr._hasValue ; if (_hasValue) this->setValue(attr.value) ; return (*this) ; } operator Ctype() const { if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !"); return (value) ; } Ctype* operator ->(void) { return (&value); } Ctype& operator * (void) { return (value) ; } public : /* virtual */ virtual bool hasValue(void) const { return (_hasValue); } virtual const char * getType(void) const = 0; virtual void setFromString(const std::string& str) { istringstream iss(str); Ctype val; iss >> val; this->setValue(val); } virtual void assignValue(const BaseAttribut* _ba) { value = ((Attribut*)_ba) -> value; _hasValue = true; } virtual ostream& print(std::ostream & _out) const { if (_hasValue) _out << " " << getName() << "=\"" << boolalpha << value << "\"" ; return (_out) ; } virtual void setValue(const Ctype & value_) { _hasValue = true ; value = value_ ;} virtual void getValue(Ctype & value_) const { if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !"); value_ = value ; } virtual ~Attribut() { /* Ne rien faire de plus */ } private : bool _hasValue ; Ctype value ; }; // class Attribut #define SET_ARRAY_DIM(type, dim) \ template<> \ void Attribut >::setValue (const Array& val) \ { _hasValue = true ; value.resize(val.shape()); value = val ; } #define SET_ARRAY_TYPE(type) \ SET_ARRAY_DIM(type,1) \ SET_ARRAY_DIM(type,2) \ SET_ARRAY_DIM(type,3) \ SET_ARRAY_DIM(type,4) SET_ARRAY_TYPE(double) SET_ARRAY_TYPE(int) SET_ARRAY_TYPE(bool) template <> ostream& Attribut >::print(ostream & o) const { if (_hasValue) o << " " << getName() << "=\"" << value(0); /*for (int i = 1; i < value.size(); i++) { o << "," << value(i); }*/ o << value(0) << "..." << value(value.size()-1); o << "\"" ; return (o) ; } template <> void Attribut >::setFromString(const std::string& _str) { istringstream iss(_str) ; char c = '\0'; int size = 0; double d = 0.,valsup = 0., valinf = 0.; std::vector vect; Array arr; iss >> d; vect.push_back(d); if (!iss.eof ()) { iss >> c; switch (c) { case ',' : // Le tableau est généré valeur par valeur. iss.unget(); while(!iss.eof ()) { // On récupère chacune des valeurs une par une jusqu'à ce que le buffer soit vide. iss >> c >> d; if (c != ',') throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !"); vect.push_back(d); } size = vect.size(); break; case '(' : // Le tableau est généré automatiquement. if (!iss.eof ()) { // on récupère la borne supérieure valinf = d; iss >> size >> c >> d; if ((c != ')') || (size <= 0)) throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !"); valsup = d; } d = (valsup - valinf) / (double)(size - 1); for (int j = 1; j <= size; j++) vect.push_back(valinf + j * d); break; default : throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !"); } } arr.resize(size); for (int i = 0; i < size; i++) arr(i) = vect[i]; this->setValue(arr); } template <> void Attribut::setFromString(const std::string& str) { istringstream iss(str) ; const bool val = (! iss.str().compare(string(".TRUE."))) ? true : false; this->setValue(val); } template <> void Attribut::setFromString(const std::string& str) { this->setValue(str); } } // namespace XMLIOSERVER #endif //__ATTRIBUT__