From f8775b64f67cc464e024cf79cd98eed30c659d25 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Wed, 12 Mar 2014 12:35:14 +0100 Subject: implemented magic __compare() method --- include/base.h | 14 ++++++++++++++ include/class.h | 16 ++++++++++++++++ include/classbase.h | 16 ++++++++++++++++ include/interface.h | 12 ++++++++++++ include/type.h | 2 +- 5 files changed, 59 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/base.h b/include/base.h index 6541549..e99023b 100644 --- a/include/base.h +++ b/include/base.h @@ -218,6 +218,20 @@ public: * @return bool */ virtual bool __toBool(); + + /** + * Compare the object with a different object of the same type + * + * This method should return 0 if both objects are equal, a negative value + * if the 'this' object is smaller, and a positive value if the 'this' + * object is bigger. + * + * The passed in object is an instance of base + * + * @param that Object to compare with + * @return int + */ + virtual bool __compare(const Base &that) const; private: diff --git a/include/class.h b/include/class.h index 6bcefda..eabcec6 100644 --- a/include/class.h +++ b/include/class.h @@ -184,6 +184,22 @@ private: return std::is_base_of::value; } + /** + * Compare two objects + * @param object1 + * @param object2 + * @return int + */ + virtual int compare(Base *object1, Base *object2) const override + { + // cast to the actual implementation type + T *t1 = (T *)object1; + T *t2 = (T *)object2; + + // compare the two objects + return t1->__compare(*t2); + } + /** * Namespaces have access to the private base class */ diff --git a/include/classbase.h b/include/classbase.h index da027ab..01f1d55 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -107,6 +107,14 @@ public: */ virtual Base *clone(Base *orig) const = 0; + /** + * Compare two objects + * @param object1 + * @param object2 + * @return int + */ + virtual int compare(Base *object1, Base *object2) const = 0; + /** * Is this a traversable class? * @return bool @@ -382,6 +390,14 @@ private: */ static int cast(struct _zval_struct *object, struct _zval_struct *retval, int type); + /** + * Function to compare two objects + * @param object1 + * @param object2 + * @return int + */ + static int compare(struct _zval_struct *object1, struct _zval_struct *object2); + /** * Name of the class * @var string diff --git a/include/interface.h b/include/interface.h index bdff75d..52d1a97 100644 --- a/include/interface.h +++ b/include/interface.h @@ -70,6 +70,18 @@ private: return false; } + /** + * Compare two objects + * @param object1 + * @param object2 + * @return int + */ + virtual int compare(Base *object1, Base *object2) const override + { + // this is never called for interfaces + return 0; + } + /** * Namespaces have access to the private base class */ diff --git a/include/type.h b/include/type.h index e5ebeda..bec0fd5 100644 --- a/include/type.h +++ b/include/type.h @@ -21,7 +21,7 @@ enum class Type : unsigned char { Null = 0, Numeric = 1, Float = 2, - Boolean = 3, + Bool = 3, Array = 4, Object = 5, String = 6, -- cgit v1.2.3