Skip to content

Commit

Permalink
Added method to retrieve the implementation class from a Value object
Browse files Browse the repository at this point in the history
  • Loading branch information
EmielBruijntjes committed Feb 24, 2014
1 parent 9c2c6bb commit 22a4772
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Php {
/**
* Forward definitions
*/
class Base;
template <class Type> class HashMember;

/**
Expand Down Expand Up @@ -599,6 +600,35 @@ class Value
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 <typename T>
T *implementation() const
{
// retrieve the implementation
Base *base = implementation();
if (!base) return nullptr;

// try casting it
return dynamic_cast<T*>(base);
}

private:
/**
* Call function with a number of parameters
Expand Down
21 changes: 21 additions & 0 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,27 @@ HashMember<std::string> Value::operator[](const char *key)
return HashMember<std::string>(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
Expand Down

0 comments on commit 22a4772

Please sign in to comment.