diff --git a/Lib/ruby/rubystdcommon.swg b/Lib/ruby/rubystdcommon.swg index 9a72ef48983..2f7c03c0b3f 100644 --- a/Lib/ruby/rubystdcommon.swg +++ b/Lib/ruby/rubystdcommon.swg @@ -7,6 +7,8 @@ %fragment("StdTraits","header",fragment="StdTraitsCommon") { +%#define SWIG_RUBYSTDCOMMON + namespace swig { /* Traits that provides the from method @@ -198,79 +200,6 @@ namespace swig { inline bool check(VALUE obj) { return traits_check::category>::check(obj); } - - template - struct traits_asptr > { - static int asptr(VALUE obj, std::shared_ptr **val) { - std::shared_ptr *p=0; - swig_type_info *descriptor = type_info >(); - swig_ruby_owntype newmem = {0, 0}; - int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR; - if (SWIG_IsOK(res) && p) { - if (val && *val) **val = *p; - if (newmem.own & SWIG_CAST_NEW_MEMORY) delete p; - return SWIG_OK; - } else { - return SWIG_ERROR; - } - } - }; - - template - struct traits_asval > { - static int asval(VALUE obj, std::shared_ptr *val) { - if (val) { - std::shared_ptr ret; - std::shared_ptr *p=&ret; - int res = traits_asptr >::asptr(obj, &p); - if (!SWIG_IsOK(res)) return res; - if (val) *val = ret; - return SWIG_OK; - } else { - return traits_asptr >::asptr(obj, (std::shared_ptr **)(0)); - } - } - }; - - template - struct traits_asval*> { - static int asval(VALUE obj, std::shared_ptr **val) { - if (val && *val) { - typedef typename noconst_traits >::noconst_type noconst_type; - noconst_type ret; - noconst_type *p = &ret; - int res = traits_asptr::asptr(obj, &p); - if (SWIG_IsOK(res)) { - **(const_cast(val)) = ret; - } - return res; - } else { - return traits_asptr >::asptr(obj, (std::shared_ptr **)(0)); - } - } - }; - - template - struct traits_as, pointer_category> { - static std::shared_ptr as(VALUE obj, bool throw_error) { - std::shared_ptr ret; - std::shared_ptr *v = &ret; - int res = (obj ? traits_asptr >::asptr(obj, &v) : SWIG_ERROR); - if (SWIG_IsOK(res)) { - return ret; - } else { - // Uninitialized return value, no Type() constructor required. - if (throw_error) throw std::invalid_argument("bad type"); - VALUE lastErr = rb_gv_get("$!"); - if (lastErr == Qnil) { - SWIG_Error(SWIG_TypeError, swig::type_name >()); - } - static std::shared_ptr *v_def = (std::shared_ptr*) malloc(sizeof(std::shared_ptr)); - memset(v_def,0,sizeof(std::shared_ptr)); - return *v_def; - } - } - }; } - } + diff --git a/Lib/ruby/std_shared_ptr.i b/Lib/ruby/std_shared_ptr.i index df873679c62..4192c87cb03 100644 --- a/Lib/ruby/std_shared_ptr.i +++ b/Lib/ruby/std_shared_ptr.i @@ -1,2 +1,87 @@ #define SWIG_SHARED_PTR_NAMESPACE std %include + + + /* + * We want to put the folloing code after the fragment "StdTraits" at rubystdcommon.swg. + * This code is needed if and only if "StdTraits" and this std_shared_ptr.i are included at the same time. + * They don't always require each other. So specifying the dependecy by using %fragment does not work. + */ +%wrapper %{ +#ifdef SWIG_RUBYSTDCOMMON +namespace swig { + template + struct traits_asptr > { + static int asptr(VALUE obj, std::shared_ptr **val) { + std::shared_ptr *p=0; + swig_type_info *descriptor = type_info >(); + swig_ruby_owntype newmem = {0, 0}; + int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR; + if (SWIG_IsOK(res) && p) { + if (val && *val) **val = *p; + if (newmem.own & SWIG_CAST_NEW_MEMORY) delete p; + return SWIG_OK; + } else { + return SWIG_ERROR; + } + } + }; + + template + struct traits_asval > { + static int asval(VALUE obj, std::shared_ptr *val) { + if (val) { + std::shared_ptr ret; + std::shared_ptr *p=&ret; + int res = traits_asptr >::asptr(obj, &p); + if (!SWIG_IsOK(res)) return res; + if (val) *val = ret; + return SWIG_OK; + } else { + return traits_asptr >::asptr(obj, (std::shared_ptr **)(0)); + } + } + }; + + template + struct traits_asval*> { + static int asval(VALUE obj, std::shared_ptr **val) { + if (val && *val) { + typedef typename noconst_traits >::noconst_type noconst_type; + noconst_type ret; + noconst_type *p = &ret; + int res = traits_asptr::asptr(obj, &p); + if (SWIG_IsOK(res)) { + **(const_cast(val)) = ret; + } + return res; + } else { + return traits_asptr >::asptr(obj, (std::shared_ptr **)(0)); + } + } + }; + + template + struct traits_as, pointer_category> { + static std::shared_ptr as(VALUE obj, bool throw_error) { + std::shared_ptr ret; + std::shared_ptr *v = &ret; + int res = (obj ? traits_asptr >::asptr(obj, &v) : SWIG_ERROR); + if (SWIG_IsOK(res)) { + return ret; + } else { + // Uninitialized return value, no Type() constructor required. + if (throw_error) throw std::invalid_argument("bad type"); + VALUE lastErr = rb_gv_get("$!"); + if (lastErr == Qnil) { + SWIG_Error(SWIG_TypeError, swig::type_name >()); + } + static std::shared_ptr *v_def = (std::shared_ptr*) malloc(sizeof(std::shared_ptr)); + memset(v_def,0,sizeof(std::shared_ptr)); + return *v_def; + } + } + }; + } +#endif +%}