1 // 2 // Copyright (c) ZeroC, Inc. All rights reserved. 3 // 4 5 #ifndef ICEPHP_UTIL_H 6 #define ICEPHP_UTIL_H 7 8 #include <Config.h> 9 10 // 11 // Global functions. 12 // 13 extern "C" 14 { 15 ZEND_FUNCTION(Ice_stringVersion); 16 ZEND_FUNCTION(Ice_intVersion); 17 ZEND_FUNCTION(Ice_generateUUID); 18 ZEND_FUNCTION(Ice_currentProtocol); 19 ZEND_FUNCTION(Ice_currentProtocolEncoding); 20 ZEND_FUNCTION(Ice_currentEncoding); 21 ZEND_FUNCTION(Ice_protocolVersionToString); 22 ZEND_FUNCTION(Ice_stringToProtocolVersion); 23 ZEND_FUNCTION(Ice_encodingVersionToString); 24 ZEND_FUNCTION(Ice_stringToEncodingVersion); 25 } 26 27 namespace IcePHP 28 { 29 30 void* createWrapper(zend_class_entry*, size_t TSRMLS_DC); 31 void* extractWrapper(zval* TSRMLS_DC); 32 33 // 34 // Wraps a C++ pointer inside a PHP object. 35 // 36 template<typename T> 37 struct Wrapper 38 { 39 zend_object zobj; 40 T* ptr; 41 createWrapper42 static Wrapper<T>* create(zend_class_entry* ce TSRMLS_DC) 43 { 44 Wrapper<T>* w = static_cast<Wrapper<T>*>(createWrapper(ce, sizeof(Wrapper<T>) TSRMLS_CC)); 45 w->ptr = 0; 46 return w; 47 } 48 extractWrapper49 static Wrapper<T>* extract(zval* zv TSRMLS_DC) 50 { 51 return static_cast<Wrapper<T>*>(extractWrapper(zv TSRMLS_CC)); 52 } 53 valueWrapper54 static T value(zval* zv TSRMLS_DC) 55 { 56 Wrapper<T>* w = extract(zv TSRMLS_CC); 57 if(w) 58 { 59 return *w->ptr; 60 } 61 return 0; 62 } 63 }; 64 65 zend_class_entry* idToClass(const std::string& TSRMLS_DC); 66 zend_class_entry* nameToClass(const std::string& TSRMLS_DC); 67 68 bool createIdentity(zval*, const Ice::Identity& TSRMLS_DC); 69 bool extractIdentity(zval*, Ice::Identity& TSRMLS_DC); 70 71 bool createStringMap(zval*, const std::map<std::string, std::string>& TSRMLS_DC); 72 bool extractStringMap(zval*, std::map<std::string, std::string>& TSRMLS_DC); 73 74 bool createStringArray(zval*, const Ice::StringSeq& TSRMLS_DC); 75 bool extractStringArray(zval*, Ice::StringSeq& TSRMLS_DC); 76 77 // 78 // Create a PHP instance of Ice_ProtocolVersion. 79 // 80 bool createProtocolVersion(zval*, const Ice::ProtocolVersion& TSRMLS_DC); 81 82 // 83 // Create a PHP instance of Ice_EncodingVersion. 84 // 85 bool createEncodingVersion(zval*, const Ice::EncodingVersion& TSRMLS_DC); 86 87 // 88 // Extracts the members of an encoding version. 89 // 90 bool extractEncodingVersion(zval*, Ice::EncodingVersion& TSRMLS_DC); 91 92 // 93 // Convert the given exception into its PHP equivalent. 94 // 95 zval* convertException(const Ice::Exception& TSRMLS_DC); 96 97 // 98 // Convert the exception and "throw" it. 99 // 100 void throwException(const Ice::Exception& TSRMLS_DC); 101 102 // 103 // Convert a Zend type (e.g., IS_BOOL, etc.) to a string for use in error messages. 104 // 105 std::string zendTypeToString(int); 106 107 // 108 // Raise RuntimeException with the given message. 109 // 110 void runtimeError(const char* TSRMLS_DC, ...); 111 112 // 113 // Raise InvalidArgumentException with the given message. 114 // 115 void invalidArgument(const char* TSRMLS_DC, ...); 116 117 // 118 // Invoke a method on a PHP object. The method must not take any arguments. 119 // 120 bool invokeMethod(zval*, const std::string& TSRMLS_DC); 121 122 // 123 // Invoke a method on a PHP object. The method must take one string argument. 124 // 125 bool invokeMethod(zval*, const std::string&, const std::string& TSRMLS_DC); 126 127 // 128 // Check inheritance. 129 // 130 bool checkClass(zend_class_entry*, zend_class_entry*); 131 132 // 133 // Exception-safe efree. 134 // 135 class AutoEfree 136 { 137 public: AutoEfree(void * p)138 AutoEfree(void* p) : _p(p) {} ~AutoEfree()139 ~AutoEfree() { efree(_p); } 140 141 private: 142 void* _p; 143 }; 144 145 // 146 // Exception-safe zval destroy. 147 // 148 class AutoDestroy 149 { 150 public: AutoDestroy(zval * zv)151 AutoDestroy(zval* zv) : _zv(zv) {} ~AutoDestroy()152 ~AutoDestroy() { if(_zv) zval_ptr_dtor(&_zv); } 153 release()154 zval* release() { zval* z = _zv; _zv = 0; return z; } 155 156 private: 157 zval* _zv; 158 }; 159 160 } // End of namespace IcePHP 161 162 #endif 163