Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | %include <std_common.i> |
2 | ||
3 | /* | |
4 | The %implict macro allows a SwigType to be accepted | |
5 | as an input parameter and use its implicit constructors when needed. | |
6 | ||
7 | ||
8 | %implicit(A, int, double, B); | |
9 | ||
10 | %inline | |
11 | { | |
12 | struct B { }; | |
13 | struct A | |
14 | { | |
15 | int ii; | |
16 | A(int i) { ii = 1; } | |
17 | A(double d) { ii = 2; } | |
18 | A(const B& b) { ii = 3; } | |
19 | }; | |
20 | ||
21 | int get(A a) { return a.ii; } | |
22 | } | |
23 | ||
24 | Here, you can call 'get' as | |
25 | ||
26 | get(1) ==> get(A(1)) | |
27 | get(2.0) ==> get(A(2.0)) | |
28 | get(B()) ==> get(A(B())) | |
29 | ||
30 | and swig will construct an 'A' temporal variable using the | |
31 | corresponding implicit constructor. | |
32 | ||
33 | ||
34 | The plain implicit macro takes care of simple type list. If it doesn't | |
35 | work because you are passing template types with commas, then use | |
36 | the %implicit_{1,2,3} versions, and the SWIG_arg macro. | |
37 | ||
38 | */ | |
39 | ||
40 | ||
41 | %define %implicit_type(...) | |
42 | %traits_swigtype(__VA_ARGS__); | |
43 | %enddef | |
44 | ||
45 | %define %implicit_frag(...) ,fragment=SWIG_Traits_frag(__VA_ARGS__) %enddef | |
46 | ||
47 | %define %implicit_code(...) | |
48 | if (swig::check<__VA_ARGS__ >(obj)) { | |
49 | if (val) *val = new value_type(swig::as<__VA_ARGS__ >(obj)); | |
50 | return SWIG_NEWOBJ; | |
51 | } | |
52 | %enddef | |
53 | ||
54 | /* implicit */ | |
55 | ||
56 | %define %implicit(Type, ...) | |
57 | ||
58 | %formacro_1(%implicit_type,__VA_ARGS__); | |
59 | ||
60 | %fragment(SWIG_Traits_frag(Type),"header", | |
61 | fragment="StdTraits" | |
62 | %formacro_1(%implicit_frag,__VA_ARGS__)) %{ | |
63 | namespace swig { | |
64 | template <> struct traits<Type > { | |
65 | typedef pointer_category category; | |
66 | static const char* type_name() { return "Type"; } | |
67 | }; | |
68 | ||
69 | template <> struct traits_asptr< Type > { | |
70 | typedef Type value_type; | |
71 | static int asptr(PyObject *obj, value_type **val) { | |
72 | Type *vptr; | |
73 | static swig_type_info* desc = SWIG_TypeQuery("Type *"); | |
74 | if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { | |
75 | if (val) *val = vptr; | |
76 | return SWIG_OLDOBJ; | |
77 | } else { | |
78 | if (PyErr_Occurred()) PyErr_Clear(); | |
79 | %formacro_1(%implicit_code,__VA_ARGS__) | |
80 | } | |
81 | if (val) { | |
82 | PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); | |
83 | } | |
84 | return 0; | |
85 | } | |
86 | }; | |
87 | } | |
88 | %} | |
89 | ||
90 | %typemap_traits_ptr(SWIG_CCode(POINTER),Type); | |
91 | %enddef | |
92 | ||
93 | /* implicit_1 */ | |
94 | ||
95 | ||
96 | %define %implicit_1(Type, Imp1) | |
97 | %traits_swigtype(Imp1); | |
98 | ||
99 | %fragment(SWIG_Traits_frag(Type),"header", | |
100 | fragment="StdTraits", | |
101 | fragment=SWIG_Traits_frag(Imp1)) %{ | |
102 | namespace swig { | |
103 | template <> struct traits< Type > { | |
104 | typedef pointer_category category; | |
105 | static const char* type_name() { return "Type"; } | |
106 | }; | |
107 | ||
108 | template <> struct traits_asptr< Type > { | |
109 | typedef Type value_type; | |
110 | static int asptr(PyObject *obj, value_type **val) { | |
111 | Type *vptr; | |
112 | static swig_type_info* desc = SWIG_TypeQuery("Type *"); | |
113 | if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { | |
114 | if (val) *val = vptr; | |
115 | return SWIG_OLDOBJ; | |
116 | } else { | |
117 | if (PyErr_Occurred()) PyErr_Clear(); | |
118 | %implicit_code(Imp1); | |
119 | } | |
120 | if (val) { | |
121 | PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); | |
122 | } | |
123 | return 0; | |
124 | } | |
125 | }; | |
126 | } | |
127 | %} | |
128 | ||
129 | %typemap_traits_ptr(SWIG_CCode(POINTER),Type); | |
130 | ||
131 | %enddef | |
132 | ||
133 | /* implicit_2 */ | |
134 | ||
135 | %define %implicit_2(Type, Imp1, Imp2) | |
136 | %traits_swigtype(Imp1); | |
137 | %traits_swigtype(Imp2); | |
138 | ||
139 | %fragment(SWIG_Traits_frag(Type),"header", | |
140 | fragment="StdTraits", | |
141 | fragment=SWIG_Traits_frag(Imp1), | |
142 | fragment=SWIG_Traits_frag(Imp2)) %{ | |
143 | namespace swig { | |
144 | template <> struct traits< Type > { | |
145 | typedef pointer_category category; | |
146 | static const char* type_name() { return "Type"; } | |
147 | }; | |
148 | ||
149 | template <> struct traits_asptr< Type > { | |
150 | typedef Type value_type; | |
151 | static int asptr(PyObject *obj, value_type **val) { | |
152 | Type *vptr; | |
153 | static swig_type_info* desc = SWIG_TypeQuery("Type *"); | |
154 | if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { | |
155 | if (val) *val = vptr; | |
156 | return SWIG_OLDOBJ; | |
157 | } else { | |
158 | if (PyErr_Occurred()) PyErr_Clear(); | |
159 | %implicit_code(Imp1); | |
160 | %implicit_code(Imp2); | |
161 | } | |
162 | if (val) { | |
163 | PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); | |
164 | } | |
165 | return 0; | |
166 | } | |
167 | }; | |
168 | } | |
169 | %} | |
170 | ||
171 | %typemap_traits_ptr(SWIG_CCode(POINTER),Type); | |
172 | %enddef | |
173 | ||
174 | ||
175 | /* implicit_3 */ | |
176 | ||
177 | %define %implicit_3(Type, Imp1, Imp2, Imp3) | |
178 | %traits_swigtype(Imp1); | |
179 | %traits_swigtype(Imp2); | |
180 | %traits_swigtype(Imp3); | |
181 | ||
182 | %fragment(SWIG_Traits_frag(Type),"header", | |
183 | fragment="StdTraits", | |
184 | fragment=SWIG_Traits_frag(Imp1), | |
185 | fragment=SWIG_Traits_frag(Imp2), | |
186 | fragment=SWIG_Traits_frag(Imp3)) %{ | |
187 | namespace swig { | |
188 | template <> struct traits< Type > { | |
189 | typedef pointer_category category; | |
190 | static const char* type_name() { return "Type"; } | |
191 | }; | |
192 | ||
193 | template <> struct traits_asptr< Type > { | |
194 | typedef Type value_type; | |
195 | static int asptr(PyObject *obj, value_type **val) { | |
196 | Type *vptr; | |
197 | static swig_type_info* desc = SWIG_TypeQuery("Type *"); | |
198 | if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { | |
199 | if (val) *val = vptr; | |
200 | return SWIG_OLDOBJ; | |
201 | } else { | |
202 | if (PyErr_Occurred()) PyErr_Clear(); | |
203 | %implicit_code(Imp1); | |
204 | %implicit_code(Imp2); | |
205 | %implicit_code(Imp3); | |
206 | } | |
207 | if (val) { | |
208 | PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); | |
209 | } | |
210 | return 0; | |
211 | } | |
212 | }; | |
213 | } | |
214 | %} | |
215 | ||
216 | %typemap_traits_ptr(SWIG_CCode(POINTER),Type); | |
217 | %enddef |