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/class.h | |
parent | 4fea3363bafc0c19f610fa3bff10b488733a4eb1 (diff) |
classes without a copy constructor can now also be used from PHP, and they automatically become unclonable
Diffstat (limited to 'include/class.h')
-rw-r--r-- | include/class.h | 42 |
1 files changed, 37 insertions, 5 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); } /** |