pyuserdir.swg 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* -------------------------------------------------------------------------
  2. * Special user directives
  3. * ------------------------------------------------------------------------- */
  4. /* ------------------------------------------------------------------------- */
  5. /* shadow code */
  6. #define %shadow %insert("shadow")
  7. #define %pythoncode %insert("python")
  8. #define %pythonbegin %insert("pythonbegin")
  9. /* ------------------------------------------------------------------------- */
  10. /*
  11. Use the "nondynamic" feature to make a wrapped class behave as a "nondynamic"
  12. one, ie, a python class that doesn't dynamically add new attributes.
  13. For example, for the class
  14. %pythonnondynamic A;
  15. struct A
  16. {
  17. int a;
  18. int b;
  19. };
  20. you will get:
  21. aa = A()
  22. aa.a = 1 # Ok
  23. aa.b = 1 # Ok
  24. aa.c = 3 # error
  25. Since nondynamic is a feature, if you use it like
  26. %pythonnondynamic;
  27. it will make all the wrapped classes nondynamic ones.
  28. The implementation is based on this recipe:
  29. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158
  30. and works for modern (-modern) and plain python. We do not use __slots__,
  31. so, it works with old python versions.
  32. */
  33. #define %pythonnondynamic %feature("python:nondynamic", "1")
  34. #define %nopythonnondynamic %feature("python:nondynamic", "0")
  35. #define %clearpythonnondynamic %feature("python:nondynamic", "")
  36. #define %pythondynamic %nopythonnondynamic
  37. /* ------------------------------------------------------------------------- */
  38. /*
  39. Use %pythonmaybecall to flag a method like __add__ or __radd__. These
  40. don't produce an error when called, they just return NotImplemented.
  41. These methods "may be called" if needed.
  42. */
  43. #define %pythonmaybecall %feature("python:maybecall", "1")
  44. #define %nopythonmaybecall %feature("python:maybecall", "0")
  45. #define %clearpythonmaybecall %feature("python:maybecall", "")
  46. /* ------------------------------------------------------------------------- */
  47. /*
  48. The %pythoncallback feature produce a more natural callback wrapper
  49. than the %callback mechanism, ie, it uses the original name for
  50. the callback and callable objects.
  51. Just use it as
  52. %pythoncallback(1) foo;
  53. int foo(int a);
  54. %pythoncallback(1) A::foo;
  55. struct A {
  56. static int foo(int a);
  57. };
  58. int bar(int, int (*pf)(int));
  59. then, you can use it as:
  60. a = foo(1)
  61. b = bar(2, foo)
  62. c = A.foo(3)
  63. d = bar(4, A.foo)
  64. If you use it with a member method
  65. %pythoncallback(1) A::foom;
  66. struct A {
  67. int foom(int a);
  68. };
  69. then you can use it as
  70. r = a.foom(3) # eval the method
  71. mptr = A.foom_cb_ptr # returns the callback pointer
  72. where the '_cb_ptr' suffix is added for the callback pointer.
  73. */
  74. #define %pythoncallback %feature("python:callback")
  75. #define %nopythoncallback %feature("python:callback","0")
  76. #define %clearpythoncallback %feature("python:callback","")
  77. /* ------------------------------------------------------------------------- */
  78. /*
  79. Support for the old %callback directive name
  80. */
  81. #ifdef %callback
  82. #undef %callback
  83. #endif
  84. #ifdef %nocallback
  85. #undef %nocallback
  86. #endif
  87. #ifdef %clearcallback
  88. #undef %clearcallback
  89. #endif
  90. #define %callback(x) %feature("python:callback",`x`)
  91. #define %nocallback %nopythoncallback
  92. #define %clearcallback %clearpythoncallback
  93. /* ------------------------------------------------------------------------- */
  94. /*
  95. Thread support - Advance control
  96. */
  97. #define %nothread %feature("nothread")
  98. #define %thread %feature("nothread","0")
  99. #define %clearnothread %feature("nothread","")
  100. #define %nothreadblock %feature("nothreadblock")
  101. #define %threadblock %feature("nothreadblock","0")
  102. #define %clearnothreadblock %feature("nothreadblock","")
  103. #define %nothreadallow %feature("nothreadallow")
  104. #define %threadallow %feature("nothreadallow","0")
  105. #define %clearnothreadallow %feature("nothreadallow","")
  106. /* ------------------------------------------------------------------------- */
  107. /*
  108. Implicit Conversion using the C++ constructor mechanism
  109. */
  110. #define %implicitconv %feature("implicitconv")
  111. #define %noimplicitconv %feature("implicitconv", "0")
  112. #define %clearimplicitconv %feature("implicitconv", "")
  113. /* ------------------------------------------------------------------------- */
  114. /*
  115. Enable keywords paramaters
  116. */
  117. #define %kwargs %feature("kwargs")
  118. #define %nokwargs %feature("kwargs", "0")
  119. #define %clearkwargs %feature("kwargs", "")
  120. /* ------------------------------------------------------------------------- */
  121. /*
  122. Add python code to the proxy/shadow code
  123. %pythonprepend - Add code before the C++ function is called
  124. %pythonappend - Add code after the C++ function is called
  125. */
  126. #define %pythonprepend %feature("pythonprepend")
  127. #define %clearpythonprepend %feature("pythonprepend","")
  128. #define %pythonappend %feature("pythonappend")
  129. #define %clearpythonappend %feature("pythonappend","")
  130. /* ------------------------------------------------------------------------- */
  131. /*
  132. %extend_smart_pointer extend the smart pointer support.
  133. For example, if you have a smart pointer as:
  134. template <class Type> class RCPtr {
  135. public:
  136. ...
  137. RCPtr(Type *p);
  138. Type * operator->() const;
  139. ...
  140. };
  141. you use the %extend_smart_pointer directive as:
  142. %extend_smart_pointer(RCPtr<A>);
  143. %template(RCPtr_A) RCPtr<A>;
  144. then, if you have something like:
  145. RCPtr<A> make_ptr();
  146. int foo(A *);
  147. you can do the following:
  148. a = make_ptr();
  149. b = foo(a);
  150. ie, swig will accept a RCPtr<A> object where a 'A *' is
  151. expected.
  152. Also, when using vectors
  153. %extend_smart_pointer(RCPtr<A>);
  154. %template(RCPtr_A) RCPtr<A>;
  155. %template(vector_A) std::vector<RCPtr<A> >;
  156. you can type
  157. a = A();
  158. v = vector_A(2)
  159. v[0] = a
  160. ie, an 'A *' object is accepted, via implicit conversion,
  161. where a RCPtr<A> object is expected. Additionally
  162. x = v[0]
  163. returns (and sets 'x' as) a copy of v[0], making reference
  164. counting possible and consistent.
  165. */
  166. %define %extend_smart_pointer(Type...)
  167. %implicitconv Type;
  168. %apply const SWIGTYPE& SMARTPOINTER { const Type& };
  169. %apply SWIGTYPE SMARTPOINTER { Type };
  170. %enddef