| 1 | /* ----------------------------------------------------------------------------- |
| 2 | * Special user directives |
| 3 | * ----------------------------------------------------------------------------- */ |
| 4 | |
| 5 | /* shadow code */ |
| 6 | #define %shadow %insert("shadow") |
| 7 | #define %pythoncode %insert("python") |
| 8 | |
| 9 | |
| 10 | /* |
| 11 | Use the "nondynamic" feature to make a wrapped class behaves as a "nondynamic" |
| 12 | one, ie, a python class that doesn't dynamically add new attributes. |
| 13 | |
| 14 | For example, for the class |
| 15 | |
| 16 | %pythonnondynamic A; |
| 17 | struct A |
| 18 | { |
| 19 | int a; |
| 20 | int b; |
| 21 | }; |
| 22 | |
| 23 | you will get: |
| 24 | |
| 25 | aa = A() |
| 26 | aa.a = 1 # Ok |
| 27 | aa.b = 1 # Ok |
| 28 | aa.c = 3 # error |
| 29 | |
| 30 | Since nondynamic is a feature, if you use it like |
| 31 | |
| 32 | %pythonnondynamic; |
| 33 | |
| 34 | it will make all the wrapped classes nondynamic ones. |
| 35 | |
| 36 | The implementation is based on the recipe: |
| 37 | |
| 38 | http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158 |
| 39 | |
| 40 | and works for modern (-modern) and plain python. We don not use __slots__, |
| 41 | so, it works with old python versions. |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | #define %pythonnondynamic %feature("python:nondynamic", "1") |
| 46 | #define %nopythonnondynamic %feature("python:nondynamic", "0") |
| 47 | #define %clearpythonnondynamic %feature("python:nondynamic", "") |
| 48 | #define %pythondynamic %nopythonnondynamic |
| 49 | |
| 50 | /* |
| 51 | |
| 52 | Use %pythonmaybecall to flag a method like __add__ or __radd__, which |
| 53 | don't produce an error when called, they just return NotImplemented. |
| 54 | |
| 55 | These methods "may be called" if needed. |
| 56 | |
| 57 | */ |
| 58 | |
| 59 | #define %pythonmaybecall %feature("python:maybecall", "1") |
| 60 | #define %nopythonmaybecall %feature("python:maybecall", "0") |
| 61 | #define %clearpythonmaybecall %feature("python:maybecall", "") |
| 62 | |
| 63 | /* |
| 64 | The %pythoncallback feature produce a more natural callback wrap |
| 65 | than the %callback mechanism, ie, it use the original name for |
| 66 | the callback and callable objects. |
| 67 | |
| 68 | Just use it as |
| 69 | |
| 70 | %pythoncallback(1) foo; |
| 71 | int foo(int a); |
| 72 | |
| 73 | %pythoncallback(1) A::foo; |
| 74 | struct A { |
| 75 | static int foo(int a); |
| 76 | }; |
| 77 | |
| 78 | int bar(int, int (*pf)(int)); |
| 79 | |
| 80 | then, you can use it as: |
| 81 | |
| 82 | a = foo(1) |
| 83 | b = bar(2, foo) |
| 84 | |
| 85 | c = A.foo(3) |
| 86 | d = bar(4, A.foo) |
| 87 | |
| 88 | |
| 89 | If you use it with a member method |
| 90 | %pythoncallback(1) A::foom; |
| 91 | struct A { |
| 92 | int foom(int a); |
| 93 | }; |
| 94 | |
| 95 | then you can use it as |
| 96 | |
| 97 | r = a.foom(3) # eval the method |
| 98 | mptr = A.foom_cb_ptr # returns the callback pointer |
| 99 | |
| 100 | where the '_cb_ptr' termination is added for the callback pointer. |
| 101 | |
| 102 | */ |
| 103 | |
| 104 | #define %pythoncallback %feature("python:callback") |
| 105 | #define %nopythoncallback %feature("python:callback","0") |
| 106 | #define %clearpythoncallback %feature("python:callback","") |
| 107 | |
| 108 | |
| 109 | /* Support for the old %callback directive name */ |
| 110 | #ifdef %callback |
| 111 | #undef %callback |
| 112 | #endif |
| 113 | |
| 114 | #ifdef %nocallback |
| 115 | #undef %nocallback |
| 116 | #endif |
| 117 | |
| 118 | #ifdef %clearcallback |
| 119 | #undef %clearcallback |
| 120 | #endif |
| 121 | |
| 122 | #define %callback(x) %feature("python:callback",`x`) |
| 123 | #define %nocallback %nopythoncallback |
| 124 | #define %clearcallback %clearpythoncallback |
| 125 | |