PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/php/director.swg

#
Unknown | 197 lines | 167 code | 30 blank | 0 comment | 0 complexity | 1d39f6df069a0f71b527a07f116d5ca8 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * director.swg
  3. *
  4. * This file contains support for director classes that proxy
  5. * method calls from C++ to PHP extensions.
  6. * ----------------------------------------------------------------------------- */
  7. #ifndef SWIG_DIRECTOR_PHP_HEADER_
  8. #define SWIG_DIRECTOR_PHP_HEADER_
  9. #ifdef __cplusplus
  10. #include <string>
  11. #include <map>
  12. /*
  13. Use -DSWIG_DIRECTOR_STATIC if you prefer to avoid the use of the
  14. 'Swig' namespace. This could be useful for multi-modules projects.
  15. */
  16. #ifdef SWIG_DIRECTOR_STATIC
  17. /* Force anonymous (static) namespace */
  18. #define Swig
  19. #endif
  20. namespace Swig {
  21. /* memory handler */
  22. struct GCItem
  23. {
  24. virtual ~GCItem() {}
  25. virtual int get_own() const
  26. {
  27. return 0;
  28. }
  29. };
  30. struct GCItem_var
  31. {
  32. GCItem_var(GCItem *item = 0) : _item(item)
  33. {
  34. }
  35. GCItem_var& operator=(GCItem *item)
  36. {
  37. GCItem *tmp = _item;
  38. _item = item;
  39. delete tmp;
  40. return *this;
  41. }
  42. ~GCItem_var()
  43. {
  44. delete _item;
  45. }
  46. GCItem * operator->() const
  47. {
  48. return _item;
  49. }
  50. private:
  51. GCItem *_item;
  52. };
  53. struct GCItem_Object : GCItem
  54. {
  55. GCItem_Object(int own) : _own(own)
  56. {
  57. }
  58. virtual ~GCItem_Object()
  59. {
  60. }
  61. int get_own() const
  62. {
  63. return _own;
  64. }
  65. private:
  66. int _own;
  67. };
  68. template <typename Type>
  69. struct GCItem_T : GCItem
  70. {
  71. GCItem_T(Type *ptr) : _ptr(ptr)
  72. {
  73. }
  74. virtual ~GCItem_T()
  75. {
  76. delete _ptr;
  77. }
  78. private:
  79. Type *_ptr;
  80. };
  81. class Director {
  82. protected:
  83. zval *swig_self;
  84. typedef std::map<void*, GCItem_var> swig_ownership_map;
  85. mutable swig_ownership_map swig_owner;
  86. #ifdef ZTS
  87. // Store the ZTS context so it's available when C++ calls back to PHP.
  88. void *** swig_zts_ctx;
  89. #endif
  90. public:
  91. Director(zval* self TSRMLS_DC) : swig_self(self) {
  92. TSRMLS_SET_CTX(swig_zts_ctx);
  93. }
  94. bool swig_is_overridden_method(char *cname, char *lc_fname) {
  95. TSRMLS_FETCH_FROM_CTX(swig_zts_ctx);
  96. zend_class_entry **ce;
  97. zend_function *mptr;
  98. int name_len = strlen(lc_fname);
  99. if (zend_lookup_class(cname, strlen(cname), &ce TSRMLS_CC) != SUCCESS) {
  100. return false;
  101. }
  102. if (zend_hash_find(&(*ce)->function_table, lc_fname, name_len + 1, (void**) &mptr) != SUCCESS) {
  103. return false;
  104. }
  105. // common.scope points to the declaring class
  106. return strcmp(mptr->common.scope->name, cname);
  107. }
  108. template <typename Type>
  109. void swig_acquire_ownership(Type *vptr) const
  110. {
  111. if (vptr) {
  112. swig_owner[vptr] = new GCItem_T<Type>(vptr);
  113. }
  114. }
  115. };
  116. /* base class for director exceptions */
  117. class DirectorException {
  118. protected:
  119. std::string swig_msg;
  120. public:
  121. DirectorException(int code, const char *hdr, const char* msg TSRMLS_DC)
  122. : swig_msg(hdr)
  123. {
  124. if (strlen(msg)) {
  125. swig_msg += " ";
  126. swig_msg += msg;
  127. }
  128. SWIG_ErrorCode() = code;
  129. SWIG_ErrorMsg() = swig_msg.c_str();
  130. }
  131. static void raise(int code, const char *hdr, const char* msg TSRMLS_DC)
  132. {
  133. throw DirectorException(code, hdr, msg TSRMLS_CC);
  134. }
  135. };
  136. /* attempt to call a pure virtual method via a director method */
  137. class DirectorPureVirtualException : public Swig::DirectorException
  138. {
  139. public:
  140. DirectorPureVirtualException(const char* msg TSRMLS_DC)
  141. : DirectorException(E_ERROR, "SWIG director pure virtual method called", msg TSRMLS_CC)
  142. {
  143. }
  144. static void raise(const char *msg TSRMLS_DC)
  145. {
  146. throw DirectorPureVirtualException(msg TSRMLS_CC);
  147. }
  148. };
  149. /* any php exception that occurs during a director method call */
  150. class DirectorMethodException : public Swig::DirectorException
  151. {
  152. public:
  153. DirectorMethodException(const char* msg TSRMLS_DC)
  154. : DirectorException(E_ERROR, "SWIG director method error", msg TSRMLS_CC)
  155. {
  156. }
  157. static void raise(const char *msg TSRMLS_DC)
  158. {
  159. throw DirectorMethodException(msg TSRMLS_CC);
  160. }
  161. };
  162. }
  163. // DirectorMethodException() is documented to be callable with no parameters
  164. // so use a macro to insert TSRMLS_CC so any ZTS context gets passed.
  165. #define DirectorMethodException() DirectorMethodException("" TSRMLS_CC)
  166. #endif /* __cplusplus */
  167. #endif