From 9c98b3c16041ebd41aabbb82e71d424a940a1d47 Mon Sep 17 00:00:00 2001 From: Sebastian Geerken Date: Tue, 5 Mar 2013 11:34:03 +0100 Subject: Comparable is now subclass of Object (not pseudo-interface anymore). The old solution would have made RTTI neccessary to work correctly. --- lout/container.cc | 2 +- lout/misc.cc | 26 -------------------------- lout/misc.hh | 29 ----------------------------- lout/object.cc | 23 +++++++++++++++++++++++ lout/object.hh | 30 ++++++++++++++++++++++++++++-- 5 files changed, 52 insertions(+), 58 deletions(-) (limited to 'lout') diff --git a/lout/container.cc b/lout/container.cc index 6a55fc2c..9e71ba77 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -190,7 +190,7 @@ void Vector::remove(int pos) */ void Vector::sort() { - qsort(array, numElements, sizeof(Object*), misc::Comparable::compareFun); + qsort(array, numElements, sizeof(Object*), Comparable::compareFun); } Object *Vector::VectorIterator::getNext() diff --git a/lout/misc.cc b/lout/misc.cc index f45a3450..d4db609e 100644 --- a/lout/misc.cc +++ b/lout/misc.cc @@ -37,32 +37,6 @@ void init (int argc, char *argv[]) prgName = strdup (argv[0]); } -// ---------------- -// Comparable -// ---------------- - -Comparable::~Comparable() -{ -} - -/** - * \brief This static method may be used as compare function for qsort(3), for - * an array of Object* (Object*[] or Object**). - */ -int Comparable::compareFun(const void *p1, const void *p2) -{ - Comparable **c1 = (Comparable**)p1; - Comparable **c2 = (Comparable**)p2; - if (c1 && c2) - return ((*c1)->compareTo(*c2)); - else if (c1) - return 1; - else if (c2) - return -1; - else - return 0; -} - // ------------------ // StringBuffer diff --git a/lout/misc.hh b/lout/misc.hh index b0c0b2f8..cff8e05b 100644 --- a/lout/misc.hh +++ b/lout/misc.hh @@ -64,35 +64,6 @@ inline int AsciiStrcasecmp(const char *s1, const char *s2) return ret; } -/** - * \brief Instances of a sub class of this interface may be compared (less, - * greater). - * - * Used for sorting etc. - */ -class Comparable -{ -public: - virtual ~Comparable(); - - /** - * \brief Compare two objects c1 and c2. - * - * return a value < 0, when c1 is less than c2, a value > 0, when c1 - * is greater than c2, or 0, when c1 and c2 are equal. - * - * If also object::Object is implemented, and if c1.equals(c2), - * c1.compareTo(c2) must be 0, but, unlike you may expect, - * the reversed is not necessarily true. This method returns 0, if, - * according to the rules for sorting, there is no difference, but there - * may still be differences (not relevant for sorting), which "equals" will - * care about. - */ - virtual int compareTo(Comparable *other) = 0; - - static int compareFun(const void *p1, const void *p2); -}; - /** * \brief Simple (simpler than container::untyped::Vector and * container::typed::Vector) template based vector. diff --git a/lout/object.cc b/lout/object.cc index 85a908b9..059564c0 100644 --- a/lout/object.cc +++ b/lout/object.cc @@ -106,6 +106,29 @@ size_t Object::sizeOf() return sizeof(Object*); } +// ---------------- +// Comparable +// ---------------- + +/** + * \brief This static method may be used as compare function for qsort(3), for + * an array of Object* (Object*[] or Object**). + */ +int Comparable::compareFun(const void *p1, const void *p2) +{ + Comparable *c1 = *(Comparable**)p1; + Comparable *c2 = *(Comparable**)p2; + + if (c1 && c2) + return ((c1)->compareTo(c2)); + else if (c1) + return 1; + else if (c2) + return -1; + else + return 0; +} + // ------------- // Pointer // ------------- diff --git a/lout/object.hh b/lout/object.hh index 9df69987..fd612863 100644 --- a/lout/object.hh +++ b/lout/object.hh @@ -33,6 +33,32 @@ public: virtual size_t sizeOf(); }; +/** + * \brief Instances of a sub class of may be compared (less, greater). + * + * Used for sorting etc. + */ +class Comparable: public Object +{ +public: + /** + * \brief Compare two objects c1 and c2. + * + * Return a value < 0, when c1 is less than c2, a value > 0, when c1 + * is greater than c2, or 0, when c1 and c2 are equal. + * + * If c1.equals(c2) (as defined in Object), c1.compareTo(c2) must + * be 0, but, unlike you may expect, the reversed is not + * necessarily true. This method returns 0, if, according to the + * rules for sorting, there is no difference, but there may still + * be differences (not relevant for sorting), which "equals" will + * care about. + */ + virtual int compareTo(Comparable *other) = 0; + + static int compareFun(const void *p1, const void *p2); +}; + /** * \brief An object::Object wrapper for void pointers. */ @@ -63,7 +89,7 @@ public: /** * \brief An object::Object wrapper for int's. */ -class Integer: public Object, misc::Comparable +class Integer: public Comparable { int value; @@ -82,7 +108,7 @@ public: * * As opposed to object::String, the char array is not copied. */ -class ConstString: public Object, misc::Comparable +class ConstString: public Comparable { protected: const char *str; -- cgit v1.2.3