#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 : operator Ctype() const { if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !"); return (value) ; } Ctype* operator ->() { return (&value) ; } Ctype& operator *() { return (value) ; } virtual bool hasValue() const { return (_hasValue); } virtual const char * getType(void) const = 0; Attribut(void) : _hasValue(false) {} ; Attribut(const Ctype& value_) : _hasValue(true), value(value_) {} ; Attribut(const Attribut& attr) : _hasValue(attr._hasValue) { if (_hasValue) value = attr.value ; } Attribut& operator = (const Attribut & attr) { _hasValue = attr._hasValue ; if (_hasValue) setValue(attr.value) ; return (*this) ; } 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(ostream & o) const { if (_hasValue) o << " " << getName() << "=\"" << boolalpha << value << "\"" ; return o ; } 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 ; } 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 <> void Attribut::setFromString(const std::string& str) { istringstream iss(str) ; 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__