1 %fragment("StdTraits","header",fragment="StdTraitsCommon")
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);
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);
17 template <class Type> struct traits_from<Type *> {
18 static octave_value from(Type* val) {
19 return traits_from_ptr<Type>::from(val, 0);
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);
31 inline octave_value from(const Type& val) {
32 return traits_from<Type>::from(val);
36 inline octave_value from_ptr(Type* val, int owner) {
37 return traits_from_ptr<Type>::from(val, owner);
40 // Traits that provides the asval/as/check method
43 static int asptr(const octave_value& obj, Type **val) {
45 int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0);
54 inline int asptr(const octave_value& obj, Type **vptr) {
55 return traits_asptr<Type>::asptr(obj, vptr);
60 static int asval(const octave_value& obj, Type *val) {
63 int res = traits_asptr<Type>::asptr(obj, &p);
64 if (!SWIG_IsOK(res)) return res;
66 typedef typename noconst_traits<Type>::noconst_type noconst_type;
67 *(const_cast<noconst_type*>(val)) = *p;
68 if (SWIG_IsNewObj(res)){
70 res = SWIG_DelNewMask(res);
77 return traits_asptr<Type>::asptr(obj, (Type **)(0));
82 template <class Type> struct traits_asval<Type*> {
83 static int asval(const octave_value& obj, Type **val) {
85 typedef typename noconst_traits<Type>::noconst_type noconst_type;
87 int res = traits_asptr<noconst_type>::asptr(obj, &p);
89 *(const_cast<noconst_type**>(val)) = p;
93 return traits_asptr<Type>::asptr(obj, (Type **)(0));
99 inline int asval(const octave_value& obj, Type *val) {
100 return traits_asval<Type>::asval(obj, val);
103 template <class Type>
104 struct traits_as<Type, value_category> {
105 static Type as(const octave_value& obj, bool throw_error) {
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>());
112 if (throw_error) throw std::invalid_argument("bad type");
118 template <class Type>
119 struct traits_as<Type, pointer_category> {
120 static Type as(const octave_value& obj, bool throw_error) {
122 int res = traits_asptr<Type>::asptr(obj, &v);
123 if (SWIG_IsOK(res) && v) {
124 if (SWIG_IsNewObj(res)) {
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>());
137 if (throw_error) throw std::invalid_argument("bad type");
138 memset(v_def,0,sizeof(Type));
144 template <class Type>
145 struct traits_as<Type*, pointer_category> {
146 static Type* as(const octave_value& obj, bool throw_error) {
148 int res = traits_asptr<Type>::asptr(obj, &v);
149 if (SWIG_IsOK(res)) {
152 if (!Octave_Error_Occurred()) {
153 %type_error(swig::type_name<Type>());
155 if (throw_error) throw std::invalid_argument("bad type");
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);
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;
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;
182 template <class Type>
183 inline bool check(const octave_value& obj) {
184 return traits_check<Type, typename traits<Type>::category>::check(obj);
189 %define %specialize_std_container(Type,Check,As,From)
192 template <> struct traits_asval<Type > {
193 typedef Type value_type;
194 static int asval(const octave_value& obj, value_type *val) {
196 if (val) *val = As(obj);
202 template <> struct traits_from<Type > {
203 typedef Type value_type;
204 static octave_value from(const value_type& val) {
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;
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)