#ifndef AGDDATTRIBUTEHH #define AGDDATTRIBUTEHH #include #include "ExpatInterface/Element.h" namespace agdd { class Attribute { public: typedef enum { required, implied, hasDefault } Status; private: string _name; bool _set; Status _status; string _default; string _value; public: explicit Attribute(const string & name, const Status status) : _name(name), _set(false), _status(status), _default() { assert(status != hasDefault); assert(name!=static_cast("")); }; explicit Attribute(const string & name, const Status status, const string & defaultt) : _name(name), _set(false), _status(status), _default(defaultt) { // Check not silly assert(status==hasDefault); assert(name!=static_cast("")); }; explicit Attribute(const string & name, const string & defaultt) : _name(name), _set(false), _status(hasDefault), _default(defaultt) { assert(name!=static_cast("")); }; //Attribute & void setValue(const string & value) { _value = value; _set = true; // return *this; }; const string getSetValue() const { assert(_set); return _value; }; const string getSetOrDefaultValue() const { if (_set) { return _value; } else { if (_status!=hasDefault) { cerr << "ERROR: The method agddAttribute::getSetOrDefaultValue" << endl << "is being used from an attribute called <"<<_name<<"> which was never set" << endl << "and which lacks a default. This is illegal." << endl; } assert(_status==hasDefault); return _default; } }; const string getDefaultValue() const { assert(_status==hasDefault); return _default; }; const Status getStatus() const { return _status; }; private: void addAttributeTo(xml::Element & elt) { if (_set) { elt.addAttribute(_name, _value); return; } // Was not set. Therefore if (_status==required) { cerr << "Error: An attribute called <" << _name << "> is required, but has not been set." << endl; } assert(_status!=required); if (_status==implied) { // Do nothing. return; } // Only one possibility left assert(_status==hasDefault); elt.addAttribute(_name, _default); return; }; public: void addAttributeTo(xml::Element * elt) { assert(elt); addAttributeTo(*elt); }; }; } // end of namespace #endif