diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-08 10:54:44 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-08 10:54:44 +0100 |
commit | 8b94e46c8f044a21c18435525da84bee28594d1a (patch) | |
tree | 4eb1165b907939d9fafc55ccf1215134debcfb84 /src | |
parent | 7c32658df2fe777ba3ac20030cfce9d2f424caef (diff) |
implemented the Countable spl without using the actual spl
Diffstat (limited to 'src')
-rw-r--r-- | src/classbase.cpp | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/classbase.cpp b/src/classbase.cpp index 14c43d3..c9576f9 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -38,6 +38,16 @@ static ClassBase *cpp_class(zend_class_entry *entry) } /** + * Retrieve our C++ implementation object + * @param val + * @return ClassBase + */ +static ClassBase *cpp_class(const zval *val) +{ + return cpp_class(zend_get_class_entry(val)); +} + +/** * Retrieve pointer to our own object handlers * @return zend_object_handlers */ @@ -53,10 +63,11 @@ zend_object_handlers *ClassBase::objectHandlers() if (initialized) return &handlers; // initialize the handlers - memcpy(&handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + memcpy(&handlers, &std_object_handlers, sizeof(zend_object_handlers)); // install custom clone function handlers.clone_obj = &ClassBase::cloneObject; + handlers.count_elements = &ClassBase::countElements; // remember that object is now initialized initialized = true; @@ -107,6 +118,34 @@ zend_object_value ClassBase::cloneObject(zval *val TSRMLS_DC) } /** + * Function that is used to count the number of elements in the object + * + * If the user has implemented the Countable interface, this method will + * call the count() method + * + * @param val + * @param count + * @return int + */ +int ClassBase::countElements(zval *object, long *count TSRMLS_DC) +{ + // we need the C++ class meta-information object + ClassBase *meta = cpp_class(object); + + // does it implement the countable interface? + Countable *countable = dynamic_cast<Countable*>(meta); + + // if it does not implement the Countable interface, we rely on the default implementation + if (!countable) return std_object_handlers.count_elements(object, count); + + // call the count function + *count = countable->count(); + + // done + return SUCCESS; +} + +/** * Function that is called when an instance of the class needs to be created. * This function will create the C++ class, and the PHP object * @param entry Pointer to the class information |