| 1 | %{ |
| 2 | #include <stdio.h> |
| 3 | %} |
| 4 | |
| 5 | %include <pymacros.swg> |
| 6 | |
| 7 | /* |
| 8 | Attribute implementation using JOHN E LENZ ideas. |
| 9 | |
| 10 | The following macros convert a pair of set/get methods |
| 11 | into a "native" python attribute. |
| 12 | |
| 13 | Use %attribute when you have a pair of get/set methods |
| 14 | like in: |
| 15 | |
| 16 | %attribute(A, int, a, get_a, set_a); |
| 17 | |
| 18 | struct A |
| 19 | { |
| 20 | int get_a() const |
| 21 | { |
| 22 | return _a; |
| 23 | } |
| 24 | |
| 25 | void set_a(int aa) |
| 26 | { |
| 27 | _a = aa; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | If you don't provide a 'set' method, a 'read-only' attribute |
| 32 | is generated, ie, like in: |
| 33 | |
| 34 | %attribute(A, int, c, get_c); |
| 35 | |
| 36 | |
| 37 | Use %attribute_ref when you have const/non-const reference |
| 38 | access methods, like in: |
| 39 | |
| 40 | %attribute_ref(A, int, b); |
| 41 | |
| 42 | struct A |
| 43 | { |
| 44 | const int& b() const |
| 45 | { |
| 46 | return _b; |
| 47 | } |
| 48 | |
| 49 | int& b() |
| 50 | { |
| 51 | return _b; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | You can also use |
| 56 | |
| 57 | %attribute_ref(class, type, refname, attr); |
| 58 | |
| 59 | if the internal C++ reference methods have a different name from the |
| 60 | attribute you want. |
| 61 | |
| 62 | Then you can use the instances like: |
| 63 | |
| 64 | x = A() |
| 65 | x.a = 3 # calls A::set_a |
| 66 | print x.a # calls A::get_a |
| 67 | |
| 68 | x.b = 3 # calls A::b() |
| 69 | print x.b # calls A::b() const |
| 70 | |
| 71 | NOTE: remember that if the type contains commas, such as |
| 72 | 'std::pair<int,int>', you need to use the macro like: |
| 73 | |
| 74 | %attribute_ref(A, SWIG_arg(std::pair<int,int>), pval); |
| 75 | |
| 76 | where SWIG_arg() 'normalize' the type to be understood as a single |
| 77 | argument, otherwise the macro will get confused (see the 'cpp' |
| 78 | documentation). |
| 79 | |
| 80 | */ |
| 81 | |
| 82 | #ifdef __cplusplus |
| 83 | %define %_attribute(Class, Wrap, type, attr, getcode, setcode) |
| 84 | %extend Class { |
| 85 | type attr; |
| 86 | } |
| 87 | %{ |
| 88 | template <class C> inline |
| 89 | type Wrap ##_## attr ## _get(const C* _t) |
| 90 | { return getcode; } |
| 91 | |
| 92 | template <class C> inline |
| 93 | void Wrap ##_## attr ## _set(C* _t, type _val) |
| 94 | { setcode; } |
| 95 | %} |
| 96 | %enddef |
| 97 | |
| 98 | #else |
| 99 | |
| 100 | %define %_attribute(Class, Wrap, type, attr, getcode, setcode) |
| 101 | %extend Class { |
| 102 | type attr; |
| 103 | } |
| 104 | %{ |
| 105 | #define Wrap ##_## attr ## _get(_t) getcode |
| 106 | #define Wrap ##_## attr ## _set(_t, _val) setcode |
| 107 | %} |
| 108 | %enddef |
| 109 | #endif |
| 110 | // |
| 111 | // Internal versions, need Wrap name |
| 112 | // |
| 113 | |
| 114 | %define %attribute_T(Class, Wrap, type, attr, get, ...) |
| 115 | %ignore Class::get; |
| 116 | #if #__VA_ARGS__ != "" |
| 117 | %ignore Class::__VA_ARGS__; |
| 118 | %_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type), |
| 119 | attr, _t->get(), _t->__VA_ARGS__(_val)) |
| 120 | #else |
| 121 | %_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type), |
| 122 | attr, _t->get(), |
| 123 | fprintf(stderr,"'attr' is a read-only attribute")) |
| 124 | #endif |
| 125 | %enddef |
| 126 | |
| 127 | %define %_attribute_ref_T(Class, Wrap, type, refname, attr) |
| 128 | %ignore Class::refname(); |
| 129 | %ignore Class::refname() const; |
| 130 | %_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type), |
| 131 | attr, _t->refname(), _t->refname() = _val) |
| 132 | %enddef |
| 133 | |
| 134 | %define %attribute_ref_T(Class, Wrap, type, refname, ...) |
| 135 | #if #__VA_ARGS__ == "" |
| 136 | %_attribute_ref_T(SWIG_arg(Class), Wrap, SWIG_arg(type), refname, refname) |
| 137 | #else |
| 138 | %_attribute_ref_T(SWIG_arg(Class), Wrap, SWIG_arg(type), refname, __VA_ARGS__) |
| 139 | #endif |
| 140 | %enddef |
| 141 | |
| 142 | // |
| 143 | // User versions |
| 144 | // |
| 145 | |
| 146 | %define %attribute(Class, type, attr, get, ...) |
| 147 | %attribute_T(SWIG_arg(Class), SWIG_Mangle(Class), SWIG_arg(type), attr, get, __VA_ARGS__) |
| 148 | %enddef |
| 149 | |
| 150 | %define %attribute_ref(Class, type, refname, ...) |
| 151 | %attribute_ref_T(SWIG_arg(Class), SWIG_Mangle(Class), SWIG_arg(type), refname, __VA_ARGS__) |
| 152 | %enddef |