From fe1ad5b086c46b82aa72cb0f9625ebdf941e50f9 Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 2 Jul 2018 14:14:08 +0200 Subject: python: Tweaking how the new wrappers will work Signed-off-by: David Shah --- common/pywrappers.h | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'common') diff --git a/common/pywrappers.h b/common/pywrappers.h index e4391b21..2c91f26f 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -47,38 +47,57 @@ template struct ContextualWrapper typedef T base_type; }; +template ContextualWrapper wrap_ctx(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + +// Dummy class, to be implemented by users +template class string_converter; + // Action options -class do_nothing +template class do_nothing { - template T operator()(Context *ctx, T x) { return x; } + T operator()(Context *ctx, T x) { return x; } - template using ret_type = T; - template using arg_type = T; + using ret_type = T; + using arg_type = T; }; -class wrap_context +template class wrap_context { - template ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + using arg_type = T; + using ret_type = ContextualWrapper; +}; - template using ret_type = ContextualWrapper; +template class unwrap_context +{ + T operator()(Context *ctx, ContextualWrapper x) { return x.base; } + using ret_type = T; + using arg_type = ContextualWrapper; }; -class unwrap_context +template class conv_from_string { - template T operator()(Context *ctx, ContextualWrapper x) { return x.base; } + T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } + using ret_type = T; + using arg_type = std::string; +}; - template using arg_type = ContextualWrapper; +template class conv_to_string +{ + 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 { - using result_type = typename boost::function_types::result_type::type; - using arg1_type = typename boost::mpl::at_c, 1>::type; using class_type = ContextualWrapper; + using conv_result_type = typename rv_conv::ret_type; + using conv_arg1_type = typename arg1_conv::arg_type; - static typename rv_conv::ret_type wrapped_fn(class_type &cls, typename arg1_conv::arg_type arg1) + 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))); } -- cgit v1.2.3