diff options
-rw-r--r-- | documentation/classes-and-objects.html | 4 | ||||
-rw-r--r-- | documentation/magic-interfaces.html | 2 | ||||
-rw-r--r-- | include/class.h | 12 | ||||
-rw-r--r-- | include/classbase.h | 20 | ||||
-rw-r--r-- | include/interface.h | 10 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/classbase.cpp | 41 | ||||
-rw-r--r-- | src/includes.h | 1 |
8 files changed, 85 insertions, 6 deletions
diff --git a/documentation/classes-and-objects.html b/documentation/classes-and-objects.html index 6bb804d..5b7c063 100644 --- a/documentation/classes-and-objects.html +++ b/documentation/classes-and-objects.html @@ -409,10 +409,10 @@ MyClass::static1(); // this will call PrivateClass::staticMethod() MyClass::static2(); -// this will call regularFunction +// this will call regularFunction() MyClass::static3(); -// this will call PrivateClass::staticMethod +// this will also call PrivateClass::staticMethod() myFunction(); ?> </code></pre> diff --git a/documentation/magic-interfaces.html b/documentation/magic-interfaces.html index 454d93e..d793856 100644 --- a/documentation/magic-interfaces.html +++ b/documentation/magic-interfaces.html @@ -67,7 +67,7 @@ private: public: /** - * C++ constructor and C++ destructpr + * C++ constructor and C++ destructor */ Counter() {} virtual ~Counter() {} diff --git a/include/class.h b/include/class.h index d1afab2..bb05fdd 100644 --- a/include/class.h +++ b/include/class.h @@ -204,9 +204,19 @@ private: */ virtual bool traversable() const override { - // check if the templated class overrides from the base + // check if the templated class overrides from the Traversable class return std::is_base_of<Traversable,T>::value; } + + /** + * Is this a serializable class? + * @return bool + */ + virtual bool serializable() const override + { + // check if the templated class overrides from the Serializable class + return std::is_base_of<Serializable,T>::value; + } /** * Compare two objects diff --git a/include/classbase.h b/include/classbase.h index 814feaf..cc69a91 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -20,6 +20,8 @@ struct _zend_object_value; struct _zend_object_handlers; struct _zend_class_entry; +struct _zend_serialize_data; +struct _zend_unserialize_data; union _zend_function; /** @@ -132,6 +134,12 @@ public: virtual bool traversable() const = 0; /** + * Is this a serializable class? + * @return bool + */ + virtual bool serializable() const = 0; + + /** * Initialize the class, given its name * * The module functions are registered on module startup, but classes are @@ -426,6 +434,18 @@ private: static int compare(struct _zval_struct *object1, struct _zval_struct *object2); /** + * Methods that are called to serialize/unserialize an object + * @param object The object to be serialized + * @param entry The class entry to which the object belongs + * @param buffer Buffer in which to store the data + * @param buf_len Size of the bufffer + * @param data Structure describing the serialize/unserialize data + * @return int + */ + static int serialize(struct _zval_struct *object, unsigned char **buffer, zend_uint *buf_len, struct _zend_serialize_data *data); + static int unserialize(struct _zval_struct **object, struct _zend_class_entry *entry, const unsigned char *buffer, zend_uint buf_len, struct _zend_unserialize_data *data); + + /** * Name of the class * @var string */ diff --git a/include/interface.h b/include/interface.h index 52d1a97..f7ef387 100644 --- a/include/interface.h +++ b/include/interface.h @@ -83,6 +83,16 @@ private: } /** + * Is this a serializable class? + * @return bool + */ + virtual bool serializable() const override + { + // not called for interfaces + return false; + } + + /** * Namespaces have access to the private base class */ friend class Namespace; @@ -44,6 +44,7 @@ #include <phpcpp/arrayaccess.h> #include <phpcpp/iterator.h> #include <phpcpp/traversable.h> +#include <phpcpp/serializable.h> #include <phpcpp/classtype.h> #include <phpcpp/classbase.h> #include <phpcpp/class.h> diff --git a/src/classbase.cpp b/src/classbase.cpp index 03c5512..0e39ae1 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -1004,6 +1004,36 @@ zend_object_iterator *ClassBase::getIterator(zend_class_entry *entry, zval *obje } /** + * Method that is called to serialize an object + * @param object The object to be serialized + * @param buffer Buffer in which to store the data + * @param buf_len Size of the bufffer + * @param data ?? + * @return int + */ +int ClassBase::serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data) +{ + std::cout << "serialize is called" << std::endl; + + return SUCCESS; +} + +/** + * Method that is called to unserialize an object + * @param object The object to be unserialized + * @param entry The class entry to which is belongs + * @param buffer Buffer holding the unserialized data + * @param data All the unserialize data + * @return int + */ +int ClassBase::unserialize(zval **object, zend_class_entry *entry, const unsigned char *buffer, zend_uint buf_len, zend_unserialize_data *data) +{ + std::cout << "unserialize is called" << std::endl; + + return SUCCESS; +} + +/** * Destructor */ ClassBase::~ClassBase() @@ -1081,10 +1111,17 @@ void ClassBase::initialize(const std::string &prefix) // we need a special constructor entry.create_object = &ClassBase::createObject; - // and a special function for retrieving the iterator (but only if this is - // a traversable class) + // for traversable classes we install a special method to get the iterator if (traversable()) entry.get_iterator = &ClassBase::getIterator; + // for serializable classes, we install callbacks for serializing and unserializing + if (serializable()) + { + // add handlers to serialize and unserialize + entry.serialize = &ClassBase::serialize; + entry.unserialize = &ClassBase::unserialize; + } + // register the class _entry = zend_register_internal_class(&entry TSRMLS_CC); diff --git a/src/includes.h b/src/includes.h index 021c943..5f273bd 100644 --- a/src/includes.h +++ b/src/includes.h @@ -60,6 +60,7 @@ #include "../include/base.h" #include "../include/countable.h" #include "../include/arrayaccess.h" +#include "../include/serializable.h" #include "../include/iterator.h" #include "../include/traversable.h" #include "../include/classtype.h" |