diff options
Diffstat (limited to 'php/src/IcePHP/Util.h')
-rw-r--r-- | php/src/IcePHP/Util.h | 121 |
1 files changed, 80 insertions, 41 deletions
diff --git a/php/src/IcePHP/Util.h b/php/src/IcePHP/Util.h index 2e20ee6cced..d71f31955ed 100644 --- a/php/src/IcePHP/Util.h +++ b/php/src/IcePHP/Util.h @@ -7,85 +7,122 @@ // // ********************************************************************** -#ifndef ICE_PHP_UTIL_H -#define ICE_PHP_UTIL_H +#ifndef ICEPHP_UTIL_H +#define ICEPHP_UTIL_H #include <Config.h> -namespace IcePHP +// +// Global functions. +// +extern "C" { +ZEND_FUNCTION(Ice_stringVersion); +ZEND_FUNCTION(Ice_intVersion); +ZEND_FUNCTION(Ice_generateUUID); +} + +#define ICEPHP_UTIL_FUNCTIONS \ + ZEND_FE(Ice_stringVersion, NULL) \ + ZEND_FE(Ice_intVersion, NULL) \ + ZEND_FE(Ice_generateUUID, NULL) + +#ifdef ICEPHP_USE_NAMESPACES +# define ICEPHP_UTIL_NS_FUNCTIONS \ + ZEND_NS_FALIAS("Ice", stringVersion, Ice_stringVersion, NULL) \ + ZEND_NS_FALIAS("Ice", intVersion, Ice_intVersion, NULL) \ + ZEND_NS_FALIAS("Ice", generateUUID, Ice_generateUUID, NULL) +#else +# define ICEPHP_UTIL_NS_FUNCTIONS +#endif -bool createIdentity(zval*, const Ice::Identity& TSRMLS_DC); -bool extractIdentity(zval*, Ice::Identity& TSRMLS_DC); +namespace IcePHP +{ -bool createContext(zval*, const Ice::Context& TSRMLS_DC); -bool extractContext(zval*, Ice::Context& TSRMLS_DC); +void* createWrapper(zend_class_entry*, size_t TSRMLS_DC); +void* extractWrapper(zval* TSRMLS_DC); // -// PHP wrapper for C++ objects. +// Wraps a C++ pointer inside a PHP object. // -struct ice_object +template<typename T> +struct Wrapper { zend_object zobj; - void* ptr; // For object data. + T* ptr; + + static Wrapper<T>* create(zend_class_entry* ce TSRMLS_DC) + { + Wrapper<T>* w = static_cast<Wrapper<T>*>(createWrapper(ce, sizeof(Wrapper<T>) TSRMLS_CC)); + w->ptr = 0; + return w; + } + + static Wrapper<T>* extract(zval* zv TSRMLS_DC) + { + return static_cast<Wrapper<T>*>(extractWrapper(zv TSRMLS_CC)); + } + + static T value(zval* zv TSRMLS_DC) + { + Wrapper<T>* w = extract(zv TSRMLS_CC); + if(w) + { + return *w->ptr; + } + return 0; + } }; -// -// Create a new ice_object for a class entry. The allocator registered for the -// class entry will be invoked, but the C++ object is not created here. -// -ice_object* newObject(zend_class_entry* TSRMLS_DC); +zend_class_entry* idToClass(const std::string& TSRMLS_DC); +zend_class_entry* nameToClass(const std::string& TSRMLS_DC); -// -// Retrieve the ice_object given a zval. -// -ice_object* getObject(zval* TSRMLS_DC); +bool createIdentity(zval*, const Ice::Identity& TSRMLS_DC); +bool extractIdentity(zval*, Ice::Identity& TSRMLS_DC); -// -// Convert the given exception into a PHP equivalent and "throw" it. -// -void throwException(const IceUtil::Exception& TSRMLS_DC); +bool createStringMap(zval*, const std::map<std::string, std::string>& TSRMLS_DC); +bool extractStringMap(zval*, std::map<std::string, std::string>& TSRMLS_DC); + +bool createStringArray(zval*, const Ice::StringSeq& TSRMLS_DC); +bool extractStringArray(zval*, Ice::StringSeq& TSRMLS_DC); // -// Find the class entry for a flattened type name. +// Convert the given exception into its PHP equivalent. // -zend_class_entry* findClass(const std::string& TSRMLS_DC); +zval* convertException(const Ice::Exception& TSRMLS_DC); // -// Find the class entry for a scoped type with suffix. +// Convert the exception and "throw" it. // -zend_class_entry* findClassScoped(const std::string& TSRMLS_DC); +void throwException(const Ice::Exception& TSRMLS_DC); // -// Convert a string to lowercase. +// Convert a Zend type (e.g., IS_BOOL, etc.) to a string for use in error messages. // -std::string lowerCase(const std::string&); +std::string zendTypeToString(int); // -// Flatten a scoped name. Leading "::" is removed, and all remaining "::" -// are replaced with underscores. The resulting string is then escaped if it -// conflicts with a PHP keyword. +// Raise RuntimeException with the given message. // -std::string flatten(const std::string&); +void runtimeError(const char* TSRMLS_DC, ...); // -// Check the given identifier against PHP's list of reserved words. If it matches -// a reserved word, then an escaped version is returned with a leading underscore. +// Raise InvalidArgumentException with the given message. // -std::string fixIdent(const std::string&); +void invalidArgument(const char* TSRMLS_DC, ...); // -// Convert a Zend type (e.g., IS_BOOL, etc.) to a string for use in error messages. +// Invoke a method on a PHP object. The method must not take any arguments. // -std::string zendTypeToString(int); +bool invokeMethod(zval*, const std::string& TSRMLS_DC); // -// Returns true if the given type is valid for use as a key in a native PHP associative array. +// Invoke a method on a PHP object. The method must take one string argument. // -bool isNativeKey(const Slice::TypePtr&); +bool invokeMethod(zval*, const std::string&, const std::string& TSRMLS_DC); // -// Determines whether a class (or interface) inherits from a base class (or interface). +// Check inheritance. // bool checkClass(zend_class_entry*, zend_class_entry*); @@ -111,6 +148,8 @@ public: AutoDestroy(zval* zv) : _zv(zv) {} ~AutoDestroy() { if(_zv) zval_ptr_dtor(&_zv); } + zval* release() { zval* z = _zv; _zv = 0; return z; } + private: zval* _zv; }; |