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