| 1 | {{{ |
| 2 | #!html |
| 3 | <!-- MAIN TABLE START --> |
| 4 | <table border=0 width="90%" align="center"><tr><td> |
| 5 | }}} |
| 6 | |
| 7 | = SWIG Quirks = |
| 8 | |
| 9 | [[TracNav(pjsua2/TOC)]] |
| 10 | |
| 11 | '''Table of Contents''' |
| 12 | [[PageOutline(2-3,,inline)]] |
| 13 | |
| 14 | |
| 15 | [[BR]] |
| 16 | |
| 17 | == SWIG Quirks == |
| 18 | |
| 19 | === Quirks with the use of namespace === |
| 20 | |
| 21 | my_array1 and my_array2 below are not recognized as vector of string: |
| 22 | |
| 23 | {{{ |
| 24 | // example.h |
| 25 | namespace pj |
| 26 | { |
| 27 | using std::vector; |
| 28 | using std::string; |
| 29 | |
| 30 | vector<string> my_array1; // this is recognized as pj::vector<pj::string> |
| 31 | vector<std::string> my_array2; // this is recognized as pj::vector<std::string> |
| 32 | } |
| 33 | }}} |
| 34 | |
| 35 | It works after adding ''std'': |
| 36 | {{{ |
| 37 | using std::vector; |
| 38 | using std::string; |
| 39 | string my_string; // ok |
| 40 | vector<int> my_array; // vector<int> is okay though! |
| 41 | std::vector<std::string> my_array; // now it's ok too |
| 42 | }}} |
| 43 | |
| 44 | === std::vector as attributes doesn't work as expected === |
| 45 | |
| 46 | Using std::vector as function arguments or return value works fine, e.g.: |
| 47 | |
| 48 | {{{ |
| 49 | // example.h |
| 50 | class A |
| 51 | { |
| 52 | public: |
| 53 | void assign_data( const std::vector<int> &data); |
| 54 | std::vector<int> retrieve_data() const; |
| 55 | }; |
| 56 | }}} |
| 57 | |
| 58 | {{{ |
| 59 | # Python |
| 60 | import example |
| 61 | |
| 62 | a = example.A() |
| 63 | a.assign_data( [0, 1, 2, 3] ) |
| 64 | data = a.retrieve_data() |
| 65 | print data |
| 66 | # will print: [0, 1, 2, 3] as expected |
| 67 | }}} |
| 68 | |
| 69 | But as class attributes, it doesn't work as expected: |
| 70 | |
| 71 | {{{ |
| 72 | // example.h |
| 73 | class A |
| 74 | { |
| 75 | public: |
| 76 | vector<int> my_values; |
| 77 | }; |
| 78 | }}} |
| 79 | |
| 80 | {{{ |
| 81 | // example.i |
| 82 | %module example |
| 83 | %{ |
| 84 | #include “example.h” |
| 85 | %} |
| 86 | |
| 87 | %include "std_string.i" |
| 88 | %include "std_vector.i" |
| 89 | |
| 90 | namespace std |
| 91 | { |
| 92 | %template(IntVector) std::vector<int>; |
| 93 | } |
| 94 | |
| 95 | %include “example.h” |
| 96 | }}} |
| 97 | |
| 98 | {{{ |
| 99 | # Python |
| 100 | a = example.A() |
| 101 | a.my_values = [0, 1, 2] # <-- ERROR |
| 102 | a.my_values = example.IntVector([0, 1, 2]) # <-- OK |
| 103 | }}} |
| 104 | |
| 105 | === "const" in parameter matters === |
| 106 | |
| 107 | Otherwise conversion from Python list to std::vector doesn't work. E.g.: |
| 108 | |
| 109 | {{{ |
| 110 | // example.h |
| 111 | class A |
| 112 | { |
| 113 | public: |
| 114 | void func_a( const vector<int> & param ); |
| 115 | void func_b( vector<int> & param ); |
| 116 | }; |
| 117 | }}} |
| 118 | |
| 119 | {{{ |
| 120 | a.func_a( [0, 1, 2] ) # <-- OK |
| 121 | a.func_b( [0, 1, 2] ) # <-- ERROR |
| 122 | }}} |
| 123 | |
| 124 | === Must use exception specification in C++ functions === |
| 125 | |
| 126 | Otherwise SWIG will terminate the app when it catches unspecified exception. E.g.: |
| 127 | |
| 128 | {{{ |
| 129 | // example.h |
| 130 | void raise_1() throw(Error) |
| 131 | { |
| 132 | throw Error(); |
| 133 | } |
| 134 | |
| 135 | void raise_2() |
| 136 | { |
| 137 | throw Error() |
| 138 | } |
| 139 | }}} |
| 140 | |
| 141 | {{{ |
| 142 | # Python |
| 143 | try: |
| 144 | raise_1() |
| 145 | except Error, e: |
| 146 | pass # <-- OK |
| 147 | |
| 148 | try: |
| 149 | raise_2() # <-- ERROR (SWIG terminates app), even with catch-anything |
| 150 | except: |
| 151 | pass |
| 152 | }}} |
| 153 | |
| 154 | === Undeclared types will be (silently) converted to pointer === |
| 155 | |
| 156 | Yes we know that, but it will catch you anyway, so I'll mention it again here. |
| 157 | |
| 158 | {{{ |
| 159 | // example.h |
| 160 | #include <pj/types.h> |
| 161 | |
| 162 | class Error |
| 163 | { |
| 164 | public: |
| 165 | pj_status_t status; |
| 166 | string reason; |
| 167 | |
| 168 | Error(pj_status_t err, const string &res) : status(err), reason(res) {} |
| 169 | }; |
| 170 | }}} |
| 171 | |
| 172 | {{{ |
| 173 | // example.i |
| 174 | %module example |
| 175 | %{ |
| 176 | #include “example.h” |
| 177 | %} |
| 178 | %include “example.h” |
| 179 | }}} |
| 180 | |
| 181 | {{{ |
| 182 | # test.py |
| 183 | err = pj.Error(-1, "Unknown error") |
| 184 | print "%d" % err.status |
| 185 | }}} |
| 186 | |
| 187 | The code above compiles fine, but pj_status_t will be treated as pointer to unknown structure by SWIG, because the declaration is in <pj/types.h> which is not included in example.i. Hence the Python code above will not print -1 but the pointer value of status. |
| 188 | |
| 189 | |
| 190 | === Java attributes: none of them! === #javaattr |
| 191 | |
| 192 | C++ attributes will be converted to get/set functions in Java! E.g.: |
| 193 | |
| 194 | {{{ |
| 195 | // example.h |
| 196 | class A |
| 197 | { |
| 198 | public: |
| 199 | int data; |
| 200 | }; |
| 201 | }}} |
| 202 | |
| 203 | The "data" member will be converted to "getData()" and "setData()" in Java. |
| 204 | |
| 205 | |
| 206 | {{{ |
| 207 | #!html |
| 208 | <!-- MAIN TABLE END --> |
| 209 | </td></tr></table> |
| 210 | }}} |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | {{{ |
| 216 | #!html |
| 217 | <!-- MAIN TABLE END --> |
| 218 | </td></tr></table> |
| 219 | }}} |
| 220 | |