带参数调用,将从单个参数解析

call with parameters, that will be parsed from a single parameter

我的目标是创建一个具有参数列表的可调用对象(示例中的 Derived)。它将使用单个参数调用,用于解析参数列表的值。

我目前的尝试在结构上类似于一种绑定机制。它看起来像这样:

#include <string>
#include <utility>
#include <type_traits>

// this is a helper meta function
template<typename FunctionType, int ParameterCount> struct parameter_type;
template<typename Ret, typename FirstParam, typename ... MoreParams>
struct parameter_type<Ret(FirstParam, MoreParams...), 0> {
    using type = FirstParam;
};
template<typename Ret, int ParameterCount, typename FirstParam, typename ... MoreParams>
struct parameter_type<Ret(FirstParam, MoreParams...), ParameterCount>  {
    using type = typename parameter_type<Ret(MoreParams...), ParameterCount - 1>::type;
};


// here comes the base with CRTP to call the Derived operator()()
template<typename Derived, typename ... Params> struct Base;

template<typename Derived> struct Base<Derived> {};

template<typename Derived, typename FirstParam, typename ... Params>
struct Base<Derived, FirstParam, Params...> :
    public Base<Base<Derived, FirstParam, Params...>, Params...> {
    private:
        FirstParam first_param_;
    public:
        Base(const FirstParam& first_param, Params& ... params):
            Base<Base<Derived, FirstParam, Params...>, Params...>{params...},
            first_param_{first_param} {};

        template<typename PrefixParamT>
        int operator()(
            typename std::enable_if<std::is_convertible<PrefixParamT, 
                                                        typename parameter_type<Derived, 0>::type>::value,
                                    typename std::remove_reference<PrefixParamT>::type>::type&& prefix,
            Params ... params) {
            // actually we parse first_param from prefix here
            (*static_cast<Derived*>(this))(std::forward(prefix),
                                           first_param_,
                                           params...);
        }
};

// we use that to create various callables like this
struct Derived : public Base<Derived, int, const std::string&, double> {
    Derived(int a, const std::string& b, double c) :
        Base<Derived, int, const std::string&, double>{a, b, c} {};

    int operator()(const std::string& t, int a, const std::string& b, double c) {
        // her comes our business logic
    }

    // this is necessary to make the basic call operator available to 
    // the user of this class.
    int operator()(const std::string&);
};

// they should be usable like this
int main(int argc, char** argv) {
    Derived d{1, argv[0], 0.5};

    // call the most basic operator()(). 
    // here all the values from argv[1] should be parsed and converted
    // to parameters, that we pass to the most derived operator()()
    d(argv[1]);
}

当然这样是编译不通过的,因为typename parameter_type<Derived, 0>::type>不能判断Derived是不完整的类型。我明白这一点,但我还没有想出一个替代实施方案。

当然,如果我可以在不丢失任何功能的情况下省略示例中的可转换性检查,只是编译器消息中的一些清晰度。在我的实际代码中,operator()() 有不同的重载,应根据 Derived::operator()() 的签名进行选择。因此我需要这样的支票。

是否有不同的方法?我的目标是使 Derived 之类的可调用对象尽可能简单。我们将来会有很多不同的签名。这就是为什么我尽量避免在 Derived::operator()().

中解析 prefix 的原因

解决方案

为了这个问题的未来读者。

感谢@Yakk 提供的答案,我想出了一个解决方案。这仍然是示例代码,需要在 parse_params_chooser<> 模板中进行更详细的类型特征检查,以启用其他可调用对象,而不是自由函数。但我认为,现在道路已经铺好了,它应该只是这样工作。

#include <string>
#include <utility>
#include <tuple>
#include <experimental/tuple>
#include <type_traits>
#include <iostream>

// basic machinery
template <typename Derived, typename ResultType, typename ... Params> struct parse_params_t;

template<typename Derived, typename ResultType, typename FirstParam, typename ... Params>
struct parse_params_t<Derived, ResultType, FirstParam, Params...> :
    public parse_params_t<parse_params_t<Derived, ResultType, FirstParam, Params...>, ResultType, Params...> {
    private:
        typename std::remove_reference<FirstParam>::type first_param_;
    public:
        parse_params_t(const typename std::remove_reference<FirstParam>::type& first_param, Params&& ... params):
            parse_params_t<parse_params_t<Derived, ResultType, FirstParam, Params...>, ResultType, Params...>{std::forward<Params>(params)...},
            first_param_{first_param} {};

        using parse_params_t<parse_params_t<Derived, ResultType, FirstParam, Params...>, ResultType, Params...>::parse;

        template<typename PrefixParamT>
        auto parse(const PrefixParamT& prefix, const Params& ... params) -> ResultType {
            return static_cast<Derived*>(this)->parse(prefix, first_param_, params...);
        }
};

template<typename Derived, typename ResultType, typename LastParam>
struct parse_params_t<Derived, ResultType, LastParam> {
    private:
        LastParam last_param_;
    public:
        parse_params_t(const LastParam& last_param):
            last_param_{last_param} {};

        template<typename PrefixParamT>
        auto parse(PrefixParamT&& prefix) -> ResultType {
            return static_cast<Derived*>(this)->parse(std::forward<PrefixParamT>(prefix), last_param_);
        }
};


// put things together in a last derived type
template <typename ResultType, typename ... Params>
struct parse_params_helper : public parse_params_t<parse_params_helper<ResultType, Params...>, ResultType, Params...> {
    parse_params_helper(Params&& ... params):
        parse_params_t<parse_params_helper<ResultType, Params...>, ResultType, Params...>{std::forward<Params>(params)...} {};

    using parse_params_t<parse_params_helper<ResultType, Params...>, ResultType, Params...>::parse;

    template<typename PrefixParamT>
    auto parse(const PrefixParamT& prefix, const Params& ... params) -> ResultType {
        return {params...};
    }
};


// choose parser depending on handler parameter types.
template <typename PrefixParamT, typename Handler> struct parse_params_chooser;

template <typename PrefixParamT, typename ... Params>
struct parse_params_chooser<PrefixParamT, int(Params...)> {
    static auto parse(int (handler)(Params...), Params&& ... params) {
        return [helper = parse_params_helper<std::tuple<Params...>, Params...>{std::forward<Params>(params)...},
                handler](PrefixParamT&& prefix) mutable -> int {
            return std::experimental::apply(handler, std::tuple_cat(helper.parse(prefix)));
        };
    }
};

template <typename PrefixParamT, typename ... Params>
struct parse_params_chooser<PrefixParamT, int(PrefixParamT, Params...)> {
    static auto parse(int (handler)(PrefixParamT, Params...), Params&& ... params) {
        return [helper = parse_params_helper<std::tuple<Params...>, Params...>{std::forward<Params>(params)...},
                handler](PrefixParamT&& prefix) mutable -> int {
            return std::experimental::apply(handler, std::tuple_cat(std::make_tuple(prefix), helper.parse(prefix)));
        };
    }
};

// create a nice free function interface to trigger the meta programm
template <typename PrefixParamT, typename Handler, typename ... Params>
auto parse_params(Handler& handler, Params&& ... params) {
    return parse_params_chooser<PrefixParamT, Handler>::parse(handler, std::forward<Params>(params)...);
}


// now we can use that to create various callables like this
auto handler(std::string t, int a, std::string b, double c) -> int {
    // her comes our business logic
    std::cout << "handler: " << t << " " << a << " " << b << " " << c << std::endl;
}

auto other_handler(int a, std::string b, double c) -> int {
    // more business logic
    std::cout << "other_handler: " << a << " " << b << " " << c << std::endl;
}

// they should be usable like this
auto main(int argc, char** argv) -> int {
    auto h = parse_params<std::string>(handler, 1, argv[0], 0.5);
    auto g = parse_params<std::string>(other_handler, 2, std::string(argv[0]) + " blabla", 1.5);

    // call the lambda, that will parse argv[1] and call the handler
    h(argv[1]);
    // call the lambda, that will parse argv[2] and call the handler
    g(argv[1]);
}

使用 std::tuplestd::apply

在初始化列表中使用 Ts... 扩展以特定顺序构建参数。也许使用对齐存储或选项来简化它,或者如果懒惰则构造和分配。

可能将编译时或运行时索引和类型传递给解析代码。

因此派生应如下所示:

struct bob : auto_parse< bob, void(int, std::string, double) >{
  int parse( std::string_view& data, size_t arg_index, tag_t<int> ) { return {}; } // actually parse
  // optional!  If missing default parser used.
  // etc
  void execute( int x, std::string const& s, double d ) const { /* code */ }
};

我会避免模棱两可的 operator() 位。