[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
From the perspective of the user who writes a new class in the system and
wants to hook it in to the attribute system, there is mainly the matter
of writing
the conversions to/from strings and attribute values. Most of this can be
copy/pasted with macro-ized code. For instance, consider class
delcaration for Rectangle in the src/mobility/
directory:
/** * \brief a 2d rectangle */ class Rectangle { ... double xMin; double xMax; double yMin; double yMax; };
One macro call and two operators, must be added below the class declaration
in order to turn a Rectangle into a value usable by the Attribute
system:
std::ostream &operator << (std::ostream &os, const Rectangle &rectangle); std::istream &operator >> (std::istream &is, Rectangle &rectangle); ATTRIBUTE_HELPER_HEADER (Rectangle);
In the class definition (.cc
file), the code looks like this:
ATTRIBUTE_HELPER_CPP (Rectangle); std::ostream & operator << (std::ostream &os, const Rectangle &rectangle) { os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax; return os; } std::istream & operator >> (std::istream &is, Rectangle &rectangle) { char c1, c2, c3; is >> rectangle.xMin >> c1 >> rectangle.xMax >> c2 >> rectangle.yMin >> c3 >> rectangle.yMax; if (c1 != '|' || c2 != '|' || c3 != '|') { is.setstate (std::ios_base::failbit); } return is; }
These stream operators simply convert from a string representation of the Rectangle ("xMin|xMax|yMin|yMax") to the underlying Rectangle, and the modeler must specify these operators and the string syntactical representation of an instance of the new class.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated on July, 4 2009 using texi2html 1.78.