diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-13 23:35:54 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-13 23:35:54 +0100 |
commit | 2381b65c0bf4fc8a78e9040ffb4a1674bab4e2ad (patch) | |
tree | dc163b88edcd0f5c3e0fc06481dd66273a59ecc4 /include | |
parent | 4fea3363bafc0c19f610fa3bff10b488733a4eb1 (diff) |
classes without a copy constructor can now also be used from PHP, and they automatically become unclonable
Diffstat (limited to 'include')
-rw-r--r-- | include/class.h | 42 | ||||
-rw-r--r-- | include/classbase.h | 6 |
2 files changed, 41 insertions, 7 deletions
diff --git a/include/class.h b/include/class.h index 0bfa745..9fa14a7 100644 --- a/include/class.h +++ b/include/class.h @@ -169,17 +169,49 @@ private: } /** + * Method to clone the object if it is copy constructable + * @param orig + * @return Base* + */ + template <typename X = T> + typename std::enable_if<std::is_copy_constructible<X>::value, Base*>::type + static maybeClone(X *orig) + { + // create a new instance + return new X(*orig); + } + + /** + * Method to clone the object if it is copy constructable + * @param orig + * @return Base* + */ + template <typename X = T> + typename std::enable_if<!std::is_copy_constructible<X>::value, Base*>::type + static maybeClone(X *orig) + { + // impossible return null + return nullptr; + } + + /** + * Is this a clonable class? + * @return bool + */ + virtual bool clonable() const + { + return std::is_copy_constructible<T>::value; + } + + /** * Construct a clone * @param orig * @return Base */ virtual Base *clone(Base *orig) const override { - // cast to the original object - T *t = (T *)orig; - - // construct a new base by calling the copy constructor - return new T(*t); + // maybe clone it (if the class has a copy constructor) + return maybeClone<T>((T*)orig); } /** diff --git a/include/classbase.h b/include/classbase.h index 77f7948..0ea6ea4 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -133,11 +133,13 @@ protected: virtual Base *clone(Base *orig) const { return nullptr; } /** - * Methods to check if a certain interface is overridden + * Methods to check if a certain interface is overridden, or a copy + * constructor is available * @return bool */ virtual bool traversable() const { return false; } virtual bool serializable() const { return false; } + virtual bool clonable() const { return false; } /** * Compare two objects @@ -341,7 +343,7 @@ private: * Retrieve pointer to our own object handlers * @return zend_object_handlers */ - static struct _zend_object_handlers *objectHandlers(); + struct _zend_object_handlers *objectHandlers(); /** * Function to create a new iterator to iterate over an object |