#include #include #include #include #include #include "JLang/JComparable.hh" #include "JLang/JMultiComparable.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; struct __A__ : public JComparable<__A__> { __A__() : value(0) {} bool less(const __A__& object) const { return this->value < object.value; } friend std::istream& operator>>(std::istream& in, __A__& object) { return in >> object.value; } friend std::ostream& operator<<(std::ostream& out, const __A__& object) { return out << object.value; } int value; }; struct __B__ : public JComparable<__B__> { __B__() : value(0) {} bool less(const __B__& object) const { return this->value < object.value; } friend std::istream& operator>>(std::istream& in, __B__& object) { return in >> object.value; } friend std::ostream& operator<<(std::ostream& out, const __B__& object) { return out << object.value; } int value; }; struct __C__ : public __A__, public __B__, public JMultiComparable<__C__, JTYPELIST<__A__, __B__>::typelist> { __C__() {} friend std::istream& operator>>(std::istream& in, __C__& object) { return in >> static_cast<__A__&>(object) >> static_cast<__B__&>(object); } friend std::ostream& operator<<(std::ostream& out, const __C__& object) { return out << static_cast(object) << ' ' << static_cast(object); } }; } /** * \file * * Example program to test JLANG::JComparible class. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef vector<__C__> buffer_type; buffer_type buffer; int debug; try { JParser<> zap("Example program to test hierarchical comparisons of objects."); zap['i'] = make_field(buffer); zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } DEBUG("before:" << endl); for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) { DEBUG(*i << endl); } sort(buffer.begin(), buffer.end()); DEBUG("after:" << endl); for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) { DEBUG(*i << endl); } ASSERT(buffer.size() > 1); for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) { ASSERT((static_cast(*p) < static_cast(*q)) || (static_cast(*p) == static_cast(*q) && static_cast(*p) < static_cast(*q))); } }