From 1f1a0fa9349d37e623ae763b48c7ea21681cd45b Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 14 Mar 2014 12:42:35 +0100 Subject: implementation of properties using callback methods --- include/classbase.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'include/classbase.h') diff --git a/include/classbase.h b/include/classbase.h index 1979f94..3ae8568 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -51,11 +51,18 @@ typedef void (Base::*method_callback_5)(Parameters &) const; typedef Value (Base::*method_callback_6)() const; typedef Value (Base::*method_callback_7)(Parameters &) const; +/** + * Signatures for getters and setters + */ +typedef Value (Base::*getter_callback)(); +typedef void (Base::*setter_callback)(const Php::Value &value); + /** * Forward declarations */ class Method; class Member; +class Property; /** * Class definition @@ -87,6 +94,7 @@ public: _type(that._type), _methods(that._methods), _members(that._members), + _properties(that._properties), _entry(nullptr) {} /** @@ -98,6 +106,7 @@ public: _type(that._type), _methods(std::move(that._methods)), _members(std::move(that._members)), + _properties(std::move(that._properties)), _entry(that._entry) { // other entry are invalid now (not that it is used..., class objects are @@ -269,6 +278,14 @@ protected: void property(const char *name, const char *value, int flags = Php::Public); void property(const char *name, double value, int flags = Php::Public); + /** + * Set property with callbacks + * @param name Name of the property + * @param getter Getter method + * @param setter Setter method + */ + void property(const char *name, const getter_callback &getter, const setter_callback &setter); + private: /** * Retrieve an array of zend_function_entry objects that hold the @@ -280,6 +297,14 @@ private: */ const struct _zend_function_entry *entries(); + /** + * Helper method to turn a property into a zval + * @param value + * @param type + * @return Value + */ + static struct _zval_struct *toZval(Value &&value, int type); + /** * Static member functions to create or clone objects based on this class * @param entry Pointer to class information @@ -503,6 +528,12 @@ private: */ std::list> _members; + /** + * Map of dynamically accessible properties + * @var std::map + */ + std::map> _properties; + /** * Base object has access to the members * This is needed by the Base::store() method -- cgit v1.2.3