From 65195513eb2676d974f1cef94be5e84f42a52f0f Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 2 Jul 2018 15:13:09 +0200 Subject: python: Restructuring wrapper system Signed-off-by: David Shah --- common/pywrappers.h | 57 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 16 deletions(-) (limited to 'common') diff --git a/common/pywrappers.h b/common/pywrappers.h index 2c91f26f..fe786a3b 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -41,66 +41,91 @@ template struct ContextualWrapper Context *ctx; T base; - ContextualWrapper(Context *c, T &&x) : ctx(c), base(x){}; + inline ContextualWrapper(Context *c, T &&x) : ctx(c), base(x){}; - operator T() { return base; }; + inline operator T() { return base; }; typedef T base_type; }; +template struct WrapIfNotContext { + typedef ContextualWrapper maybe_wrapped_t; +}; + +template <> struct WrapIfNotContext { + typedef Context maybe_wrapped_t; +}; + + +template inline Context *get_ctx(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.ctx; +} +template <> inline Context *get_ctx(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { + return &unwrp_ctx; +} + +template inline T&get_base(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.base; +} +template <> inline Context &get_base(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { + return unwrp_ctx; +} + template ContextualWrapper wrap_ctx(Context *ctx, T x) { return ContextualWrapper(ctx, x); } // Dummy class, to be implemented by users -template class string_converter; +template struct string_converter; // Action options -template class do_nothing +template struct do_nothing { - T operator()(Context *ctx, T x) { return x; } + inline T operator()(Context *ctx, T x) { return x; } using ret_type = T; using arg_type = T; }; -template class wrap_context +template struct wrap_context { - ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + inline ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } using arg_type = T; using ret_type = ContextualWrapper; }; -template class unwrap_context +template struct unwrap_context { - T operator()(Context *ctx, ContextualWrapper x) { return x.base; } + inline T operator()(Context *ctx, ContextualWrapper x) { return x.base; } using ret_type = T; using arg_type = ContextualWrapper; }; -template class conv_from_string +template struct conv_from_str { - T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } + inline T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } using ret_type = T; using arg_type = std::string; }; -template class conv_to_string +template struct conv_to_str { - std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } + inline std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } using ret_type = std::string; using arg_type = T; }; // Function wrapper // Example: one parameter, one return -template struct function_wrapper +template struct fn_wrapper { - using class_type = ContextualWrapper; + using class_type = typename WrapIfNotContext::maybe_wrapped_t; using conv_result_type = typename rv_conv::ret_type; using conv_arg1_type = typename arg1_conv::arg_type; static conv_result_type wrapped_fn(class_type &cls, conv_arg1_type arg1) { - return rv_conv()(cls.ctx, cls.base.*fn(arg1_conv()(cls.ctx, arg1))); + return rv_conv()(get_base(cls).ctx, get_base(cls).*fn(arg1_conv()(get_ctx(cls), arg1))); } + + template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; } // namespace PythonConversion -- cgit v1.2.3