Upstream version 1.3.40
[profile/ivi/swig.git] / Lib / r / rstdcommon.swg
1 %fragment("StdTraits","header",fragment="StdTraitsCommon")
2 {
3 namespace swig {  
4   /*
5     Traits that provides the from method
6   */
7
8   template <class Type> struct traits_from_ptr {
9     static SWIG_Object from(Type *val, int owner = 0) {
10       return SWIG_NewPointerObj(val, type_info<Type>(), owner);
11     }
12   };
13
14   template <class Type> struct traits_from {
15     static SWIG_Object from(const Type& val) {
16       return traits_from_ptr<Type>::from(new Type(val), 1);
17     }
18   };
19
20   template <class Type> struct traits_from<Type *> {
21     static SWIG_Object from(Type* val) {
22       return traits_from_ptr<Type>::from(val, 0);
23     }
24   };
25
26   template <class Type>
27   inline SWIG_Object from(const Type& val) {
28     return traits_from<Type>::from(val);
29   }
30
31   template <class Type>
32   inline SWIG_Object from_ptr(Type* val, int owner) {
33     return traits_from_ptr<Type>::from(val, owner);
34   }
35
36   /*
37     Traits that provides the asval/as/check method
38   */
39   template <class Type>
40   struct traits_asptr {   
41     static int asptr(SWIG_Object obj, Type **val) {
42       Type *p;
43       int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0);
44       if (SWIG_IsOK(res)) {
45         if (val) *val = p;
46       }
47       return res;
48     }
49   }; 
50
51   template <class Type>
52   inline int asptr(SWIG_Object obj, Type **vptr) {
53     return traits_asptr<Type>::asptr(obj, vptr);
54   }
55
56   template <class Type> 
57   struct traits_asval {
58     static int asval(SWIG_Object obj, Type *val) {
59       if (val) {
60         Type *p = 0;
61         int res = traits_asptr<Type>::asptr(obj, &p);
62         if (!SWIG_IsOK(res)) return res;        
63         if (p) {
64           typedef typename noconst_traits<Type>::noconst_type noconst_type;
65           *(const_cast<noconst_type*>(val)) = *p;
66           if (SWIG_IsNewObj(res)){
67             %delete(p);
68             res = SWIG_DelNewMask(res);
69           }
70           return res;
71         } else {
72           return SWIG_ERROR;
73         }
74       } else {
75         return traits_asptr<Type>::asptr(obj, (Type **)(0));
76       }
77     }
78   };
79
80   template <class Type> struct traits_asval<Type*> {
81     static int asval(SWIG_Object obj, Type **val) {
82       if (val) {
83         typedef typename noconst_traits<Type>::noconst_type noconst_type;
84         noconst_type *p = 0;
85         int res = traits_asptr<noconst_type>::asptr(obj,  &p);
86         if (SWIG_IsOK(res)) {
87           *(const_cast<noconst_type**>(val)) = p;
88         }
89         return res;
90       } else {
91         return traits_asptr<Type>::asptr(obj, (Type **)(0));
92       }
93     }
94   };
95   
96   template <class Type>
97   inline int asval(SWIG_Object obj, Type *val) {
98     return traits_asval<Type>::asval(obj, val);
99   }
100
101   template <class Type> 
102   struct traits_as<Type, value_category> {
103     static Type as(SWIG_Object obj, bool throw_error) {
104       Type v;
105       int res = asval(obj, &v);
106       if (!obj || !SWIG_IsOK(res)) {
107 //      if (!PyErr_Occurred()) {
108 //        %type_error(swig::type_name<Type>());
109 //      }
110         if (throw_error) throw std::invalid_argument("bad type");
111       }
112       return v;
113     }
114   };
115
116   template <class Type> 
117   struct traits_as<Type, pointer_category> {
118     static Type as(SWIG_Object obj, bool throw_error) {
119       Type *v = 0;      
120       int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
121       if (SWIG_IsOK(res) && v) {
122         if (SWIG_IsNewObj(res)) {
123           Type r(*v);
124           %delete(v);
125           return r;
126         } else {
127           return *v;
128         }
129       } else {
130         // Uninitialized return value, no Type() constructor required.
131         static Type *v_def = (Type*) malloc(sizeof(Type));
132 //      if (!PyErr_Occurred()) {
133 //        %type_error(swig::type_name<Type>());
134 //      }
135         if (throw_error) throw std::invalid_argument("bad type");
136         memset(v_def,0,sizeof(Type));
137         return *v_def;
138       }
139     }
140   };
141
142   template <class Type> 
143   struct traits_as<Type*, pointer_category> {
144     static Type* as(SWIG_Object obj, bool throw_error) {
145       Type *v = 0;      
146       int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
147       if (SWIG_IsOK(res)) {
148         return v;
149       } else {
150 //      if (!PyErr_Occurred()) {
151 //        %type_error(swig::type_name<Type>());
152 //      }
153         if (throw_error) throw std::invalid_argument("bad type");
154         return 0;
155       }
156     }
157   };
158     
159   template <class Type>
160   inline Type as(SWIG_Object obj, bool te = false) {
161     return traits_as<Type, typename traits<Type>::category>::as(obj, te);
162   }
163
164   template <class Type> 
165   struct traits_check<Type, value_category> {
166     static bool check(SWIG_Object obj) {
167       int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR;
168       return SWIG_IsOK(res) ? true : false;
169     }
170   };
171
172   template <class Type> 
173   struct traits_check<Type, pointer_category> {
174     static bool check(SWIG_Object obj) {
175       int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR;
176       return SWIG_IsOK(res) ? true : false;
177     }
178   };
179
180   template <class Type>
181   inline bool check(SWIG_Object obj) {
182     return traits_check<Type, typename traits<Type>::category>::check(obj);
183   }
184 }
185 }
186
187 %define %specialize_std_container(Type,Check,As,From)
188 %{
189 namespace swig {
190   template <>  struct traits_asval<Type > {   
191     typedef Type value_type;
192     static int asval(SWIG_Object obj, value_type *val) {
193       if (Check(obj)) {
194         if (val) *val = As(obj);
195         return SWIG_OK;
196       }
197       return SWIG_ERROR;
198     }
199   };
200   template <>  struct traits_from<Type > {
201     typedef Type value_type;
202     static SWIG_Object from(const value_type& val) {
203       return From(val);
204     }
205   };
206
207   template <> 
208   struct traits_check<Type, value_category> {
209     static int check(SWIG_Object obj) {
210       int res = Check(obj);
211       return obj && res ? res : 0;
212     }
213   };
214 }
215 %}
216 %enddef