diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-14 12:42:35 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-14 12:42:35 +0100 |
commit | 1f1a0fa9349d37e623ae763b48c7ea21681cd45b (patch) | |
tree | d5bc49690b2bb1f85f3560c28845dcc02566d330 /include | |
parent | 91e1175a467cb9e2f90e7421a1398430d075f776 (diff) |
implementation of properties using callback methods
Diffstat (limited to 'include')
-rw-r--r-- | include/class.h | 22 | ||||
-rw-r--r-- | include/classbase.h | 31 |
2 files changed, 52 insertions, 1 deletions
diff --git a/include/class.h b/include/class.h index 008ff8a..d867699 100644 --- a/include/class.h +++ b/include/class.h @@ -156,7 +156,27 @@ public: void property(const char *name, const std::string &value, int flags = Public) { ClassBase::property(name, value, flags); } void property(const char *name, bool value, int flags = Public) { ClassBase::property(name, value, flags); } void property(const char *name, double value, int flags = Public) { ClassBase::property(name, value, flags); } - + + /** + * Properties as methods + * + * This is a smarter way for adding properties to a class. You can define + * a property and a method that gets called every time the property is + * set or unset. + * + * If you do not set a setter method, your property will be read-only. + * + * @param name Name of the property + * @param getter The getter method + * @param setter The setter method + */ + void property(const char *name, Value (T::*getter)() ) { ClassBase::property(name, static_cast<getter_callback>(getter), nullptr); } + void property(const char *name, Value (T::*getter)() const ) { ClassBase::property(name, static_cast<getter_callback>(getter), nullptr); } + void property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) ) { ClassBase::property(name, static_cast<getter_callback>(getter), static_cast<setter_callback>(setter)); } + void property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) ) { ClassBase::property(name, static_cast<getter_callback>(getter), static_cast<setter_callback>(setter)); } + void property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) const) { ClassBase::property(name, static_cast<getter_callback>(getter), static_cast<setter_callback>(setter)); } + void property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) const) { ClassBase::property(name, static_cast<getter_callback>(getter), static_cast<setter_callback>(setter)); } + private: /** * Construct a new instance of the object diff --git a/include/classbase.h b/include/classbase.h index 1979f94..3ae8568 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -52,10 +52,17 @@ 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 @@ -281,6 +298,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 * @param val The object to be cloned @@ -504,6 +529,12 @@ private: std::list<std::shared_ptr<Member>> _members; /** + * Map of dynamically accessible properties + * @var std::map + */ + std::map<std::string,std::shared_ptr<Property>> _properties; + + /** * Base object has access to the members * This is needed by the Base::store() method */ |