1 %fragment("StdTraits","header",fragment="StdTraitsCommon")
5 Traits that provides the from method
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);
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);
20 template <class Type> struct traits_from<Type *> {
21 static SWIG_Object from(Type* val) {
22 return traits_from_ptr<Type>::from(val, 0);
27 inline SWIG_Object from(const Type& val) {
28 return traits_from<Type>::from(val);
32 inline SWIG_Object from_ptr(Type* val, int owner) {
33 return traits_from_ptr<Type>::from(val, owner);
37 Traits that provides the asval/as/check method
41 static int asptr(SWIG_Object obj, Type **val) {
43 int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0);
52 inline int asptr(SWIG_Object obj, Type **vptr) {
53 return traits_asptr<Type>::asptr(obj, vptr);
58 static int asval(SWIG_Object obj, Type *val) {
61 int res = traits_asptr<Type>::asptr(obj, &p);
62 if (!SWIG_IsOK(res)) return res;
64 typedef typename noconst_traits<Type>::noconst_type noconst_type;
65 *(const_cast<noconst_type*>(val)) = *p;
66 if (SWIG_IsNewObj(res)){
68 res = SWIG_DelNewMask(res);
75 return traits_asptr<Type>::asptr(obj, (Type **)(0));
80 template <class Type> struct traits_asval<Type*> {
81 static int asval(SWIG_Object obj, Type **val) {
83 typedef typename noconst_traits<Type>::noconst_type noconst_type;
85 int res = traits_asptr<noconst_type>::asptr(obj, &p);
87 *(const_cast<noconst_type**>(val)) = p;
91 return traits_asptr<Type>::asptr(obj, (Type **)(0));
97 inline int asval(SWIG_Object obj, Type *val) {
98 return traits_asval<Type>::asval(obj, val);
101 template <class Type>
102 struct traits_as<Type, value_category> {
103 static Type as(SWIG_Object obj, bool throw_error) {
105 int res = asval(obj, &v);
106 if (!obj || !SWIG_IsOK(res)) {
107 // if (!PyErr_Occurred()) {
108 // %type_error(swig::type_name<Type>());
110 if (throw_error) throw std::invalid_argument("bad type");
116 template <class Type>
117 struct traits_as<Type, pointer_category> {
118 static Type as(SWIG_Object obj, bool throw_error) {
120 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
121 if (SWIG_IsOK(res) && v) {
122 if (SWIG_IsNewObj(res)) {
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>());
135 if (throw_error) throw std::invalid_argument("bad type");
136 memset(v_def,0,sizeof(Type));
142 template <class Type>
143 struct traits_as<Type*, pointer_category> {
144 static Type* as(SWIG_Object obj, bool throw_error) {
146 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
147 if (SWIG_IsOK(res)) {
150 // if (!PyErr_Occurred()) {
151 // %type_error(swig::type_name<Type>());
153 if (throw_error) throw std::invalid_argument("bad type");
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);
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;
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;
180 template <class Type>
181 inline bool check(SWIG_Object obj) {
182 return traits_check<Type, typename traits<Type>::category>::check(obj);
187 %define %specialize_std_container(Type,Check,As,From)
190 template <> struct traits_asval<Type > {
191 typedef Type value_type;
192 static int asval(SWIG_Object obj, value_type *val) {
194 if (val) *val = As(obj);
200 template <> struct traits_from<Type > {
201 typedef Type value_type;
202 static SWIG_Object from(const value_type& val) {
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;