From 22a4772fa7ce98d9cda73f0a0c50e69b9ff8f8d2 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 24 Feb 2014 06:05:22 -0800 Subject: Added method to retrieve the implementation class from a Value object --- include/value.h | 30 ++++++++++++++++++++++++++++++ src/value.cpp | 21 +++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/include/value.h b/include/value.h index ff18d4e..9eddf40 100644 --- a/include/value.h +++ b/include/value.h @@ -31,6 +31,7 @@ namespace Php { /** * Forward definitions */ +class Base; template class HashMember; /** @@ -599,6 +600,35 @@ public: Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8); Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9); + /** + * Retrieve the original implementation + * + * This only works for classes that were implemented using PHP-CPP, + * it returns nullptr for all other classes + * + * @return Base* + */ + Base *implementation() const; + + /** + * Retrieve the original implementation + * + * This only works for classes that were implemented using PHP-CPP, + * it returns nullptr for all other classes + * + * @return mixed + */ + template + T *implementation() const + { + // retrieve the implementation + Base *base = implementation(); + if (!base) return nullptr; + + // try casting it + return dynamic_cast(base); + } + private: /** * Call function with a number of parameters diff --git a/src/value.cpp b/src/value.cpp index 4170404..fd300d5 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1553,6 +1553,27 @@ HashMember Value::operator[](const char *key) return HashMember(this, key); } +/** + * Retrieve the original implementation + * + * This only works for classes that were implemented using PHP-CPP, + * it returns nullptr for all other classes + * + * @return Base* + */ +Base *Value::implementation() const +{ + // must be an object + if (!isObject()) return nullptr; + + // retrieve the mixed object that contains the base + MixedObject *object = (MixedObject *)zend_object_store_get_object(_val); + if (!object) return nullptr; + + // retrieve the associated C++ class + return object->cpp; +} + /** * Custom output stream operator * @param stream -- cgit v1.2.3