import source from 1.3.40
[external/swig.git] / Lib / typemaps / traits.swg
1 //
2 // Use the following macro with modern STL implementations
3 //
4 //#define SWIG_STD_MODERN_STL
5 //
6 // Use this to deactive the previous definition, when using gcc-2.95
7 // or similar old compilers.
8 //
9 //#define SWIG_STD_NOMODERN_STL
10
11 // Here, we identify compilers we now have problems with STL.
12 %{
13 #if defined(__GNUC__)
14 #  if __GNUC__ == 2 && __GNUC_MINOR <= 96
15 #     define SWIG_STD_NOMODERN_STL
16 #  endif
17 #endif
18 %}
19
20 //
21 // Common code for supporting the STD C++ namespace
22 //
23
24 %{
25 #include <string>
26 #include <stdexcept>
27 %}
28
29 %fragment("Traits","header") 
30 {
31 namespace swig {  
32   /*
33     type categories
34   */
35   struct pointer_category { };  
36   struct value_category { };
37
38   /*
39     General traits that provides type_name and type_info
40   */
41   template <class Type> struct traits { };
42
43   template <class Type>
44   inline const char* type_name() {
45     return traits<Type>::type_name();
46   }
47
48   template <class Type> 
49   struct traits_info {
50     static swig_type_info *type_query(std::string name) {
51       name += " *";
52       return SWIG_TypeQuery(name.c_str());
53     }    
54     static swig_type_info *type_info() {
55       static swig_type_info *info = type_query(type_name<Type>());
56       return info;
57     }
58   };
59
60   template <class Type>
61   inline swig_type_info *type_info() {
62     return traits_info<Type>::type_info();
63   }
64
65   /*
66     Partial specialization for pointers
67   */
68   template <class Type> struct traits <Type *> {
69     typedef pointer_category category;
70     static std::string make_ptr_name(const char* name) {
71       std::string ptrname = name;
72       ptrname += " *";
73       return ptrname;
74     }    
75     static const char* type_name() {
76       static std::string name = make_ptr_name(swig::type_name<Type>());
77       return name.c_str();
78     }
79   };
80
81  
82   template <class Type, class Category = typename traits<Type>::category > 
83   struct traits_check { };
84
85   /*
86     Traits that provides the from method for an unknown type
87   */
88   template <int flags, class Type> struct traits_from_ptr {
89     static SWIG_Object from SWIG_FROM_DECL_ARGS(Type *val) {
90       return SWIG_NewPointerObj(val, type_info<Type>(), flags);
91     }
92   };
93
94   template <class Type> struct traits_from {
95     static SWIG_Object from SWIG_FROM_DECL_ARGS(const Type& val) {
96       return traits_from_ptr<SWIG_POINTER_OWN, Type>::from(new Type(val));
97     }
98   };
99
100   template <class Type> struct traits_from<Type *> {
101     static SWIG_Object from SWIG_FROM_DECL_ARGS(Type* val) {
102       return traits_from_ptr<0, Type>::from(val);
103     }
104   };
105
106   template <class Type>
107   inline SWIG_Object from SWIG_FROM_DECL_ARGS(const Type& val) {
108     return traits_from<Type>::from(val);
109   }
110
111   /*
112     Traits that provides the asptr/asval method for an unknown type
113   */
114   template <class Type>
115   struct traits_asptr {   
116     static int asptr SWIG_AS_DECL_ARGS (SWIG_Object obj, Type **val) {
117       Type *p;
118       int res = SWIG_ConvertPtr(obj, %as_voidptrptr(&p), type_info<Type>(), 0);
119       if (SWIG_IsOK(res) && val) *val = p;
120       return res;
121     }
122   }; 
123
124   template <class Type>
125   inline int asptr SWIG_AS_DECL_ARGS(SWIG_Object obj, Type **vptr) {
126     return traits_asptr<Type>::asptr SWIG_AS_CALL_ARGS(obj, vptr);
127   }
128
129   template <class Type> 
130   struct traits_asval {
131     static int asval SWIG_AS_DECL_ARGS(SWIG_Object obj, Type *val) {
132       if (val) {
133         Type *p = 0;
134         int res = traits_asptr<Type>::asptr SWIG_AS_CALL_ARGS(obj, &p);
135         if (SWIG_IsOK(res) && p) {
136           *val = *p;
137           if (SWIG_IsNewObj(res)) {
138             %delete(p);
139             res = SWIG_DelNewMask(res);
140           }
141         }
142         return res;
143       } else {
144         return traits_asptr<Type>::asptr SWIG_AS_CALL_ARGS(obj, (Type **)(0));
145       }
146     }
147   };
148   
149   template <class Type>
150   inline int asval SWIG_AS_DECL_ARGS (SWIG_Object obj, Type *val) {
151     return traits_asval<Type>::asval SWIG_AS_CALL_ARGS(obj, val);
152   }
153
154   /*
155     Traits that provides the check method for an unknown type
156   */
157 #define SWIG_CHECK_DECL_ARGS(obj) SWIG_AS_DECL_ARGS(obj, void * = 0)
158 #define SWIG_CHECK_CALL_ARGS(obj) SWIG_AS_CALL_ARGS(obj, 0)
159
160   template <class Type> 
161   struct traits_checkval {
162     static int check SWIG_CHECK_DECL_ARGS(SWIG_Object obj) {
163       if (obj) {
164         int res = asval SWIG_AS_CALL_ARGS(obj, (Type *)(0));
165         return SWIG_CheckState(res);
166       } else {
167         return 0;
168       }
169     }
170   };
171
172   template <class Type> 
173   struct traits_checkptr {
174     static int check SWIG_CHECK_DECL_ARGS(SWIG_Object obj) {
175       if (obj) {
176         int res = asptr SWIG_AS_CALL_ARGS(obj, (Type **)(0));
177         return SWIG_CheckState(res);
178       } else {
179         return 0;
180       }
181     }
182   };
183
184   template <class Type> 
185   struct traits_check<Type, value_category> : traits_checkval<Type> {
186   };
187
188   template <class Type> 
189   struct traits_check<Type, pointer_category> : traits_checkptr<Type> {
190   };
191
192   template <class Type>
193   inline int check SWIG_CHECK_DECL_ARGS(SWIG_Object obj) {
194     return traits_check<Type>::check SWIG_CHECK_CALL_ARGS(obj);
195   }
196
197 }
198 }
199  
200 /*
201   Generate the traits for an unknown SWIGTYPE
202 */
203
204 %define %traits_swigtype(Type...)
205 %fragment(SWIG_Traits_frag(Type),"header",fragment="Traits") {
206   namespace swig {
207     template <>  struct traits<Type > {
208       typedef pointer_category category;
209       static const char* type_name() { return  #Type; }
210     };
211   }
212 }
213 %enddef
214
215
216 /*
217   Generate the traits for a 'value' type, such as 'double',
218   for which the SWIG_AsVal and SWIG_From methods are already defined.
219 */
220
221 %define %traits_value(Type...)
222 %fragment(SWIG_Traits_frag(Type),"header",
223           fragment=SWIG_AsVal_frag(Type),
224           fragment=SWIG_From_frag(Type),
225           fragment="Traits") {
226 namespace swig {
227   template <> struct traits<Type > {
228     typedef value_category category;
229     static const char* type_name() { return  #Type; }
230   };  
231
232   template <>  struct traits_asval<Type > {   
233     typedef Type value_type;
234     static int asval SWIG_AS_DECL_ARGS (SWIG_Object obj, value_type *val) {
235       return SWIG_AsVal(Type)(obj, val);
236     }
237   };
238
239   template <>  struct traits_from<Type > {
240     typedef Type value_type;
241     static SWIG_Object from SWIG_FROM_DECL_ARGS (const value_type& val) {
242       return SWIG_From(Type)(val);
243     }
244   };
245 }
246 }
247 %enddef
248
249 /*
250   Generate the traits for a 'pointer' type, such as 'std::string',
251   for which the SWIG_AsPtr and SWIG_From methods are already defined.
252 */
253
254 %define %traits_pointer(Type...)
255 %fragment(SWIG_Traits_frag(Type),"header",
256           fragment=SWIG_AsVal_frag(Type),
257           fragment=SWIG_From_frag(Type),
258           fragment="Traits") {
259 namespace swig {
260   template <> struct traits<Type > {
261     typedef pointer_category category;
262     static const char* type_name() { return  #Type; }
263   };  
264     
265   template <>  struct traits_asptr<Type > {   
266     typedef Type value_type;
267     static int asptr SWIG_AS_DECL_ARGS (SWIG_Object obj, value_type **val) {
268       return SWIG_AsPtr(Type)(obj, val);
269     }
270   };
271
272   template <>  struct traits_from<Type > {
273     typedef Type value_type;
274     static SWIG_Object from SWIG_FROM_DECL_ARGS (const value_type& val) {
275       return SWIG_From(Type)(val);
276     }
277   };
278 }
279 }
280 %enddef
281
282 /*
283   Generate the typemaps for a class that has 'value' traits
284 */
285
286 %define %typemap_traits_value(Code,Type...)
287   %typemaps_asvalfrom(%arg(Code),
288                       %arg(swig::asval),
289                       %arg(swig::from),
290                       %arg(SWIG_Traits_frag(Type)),
291                       %arg(SWIG_Traits_frag(Type)),
292                       Type);
293 %enddef
294
295 /*
296   Generate the typemaps for a class that has 'pointer' traits
297 */
298
299 %define %typemap_traits_pointer(Code,Type...)
300   %typemaps_asptrfrom(%arg(Code),
301                       %arg(swig::asptr),
302                       %arg(swig::from),
303                       %arg(SWIG_Traits_frag(Type)),
304                       %arg(SWIG_Traits_frag(Type)),
305                       Type);
306 %enddef
307