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