Dynamically creating a C ++ function argument list at runtime

I am trying to create an argument list for calling a function at runtime, but I cannot figure out how to do this in C ++.

This is for the helper library I'm writing. I take input from the client over the network and use this data to call the function pointer that the user set earlier. The function accepts a string (tokens similar to printf) and a different number of arguments. What I need is a way to add additional arguments depending on what data was received from the client.

I store functions in a map of function pointers

typedef void (*varying_args_fp)(string,...);
map<string,varying_args_fp> func_map;

Usage example will be

void printall(string tokens, ...)
{
    va_list a_list;
    va_start(a_list, tokens);

    for each(auto x in tokens)
    {
        if (x == 'i')
        {
            cout << "Int: " << va_arg(a_list, int) << ' ';
        }
        else if(x == 'c')
        {
            cout << "Char: " << va_arg(a_list, char) << ' ';
        }
    }

    va_end(a_list);
}

func_map["printall"] = printall;
func_map["printall"]("iic",5,10,'x');
// prints "Int: 5 Int: 10 Char: x"

, "CreateX 10 20", . ,

// func_name = "CreateX", tokens = 'ii', first_arg = 10, second_arg = 20
func_map[func_name](tokens,first_arg,second_arg);

, .

- , . , "" , , , , .

+5
2

++ 11. varargs, printall printf, , IMO , , . , , , . , .

, (?) .

#include <vector>
#include <iostream>
#include <functional>
#include <stdexcept>
#include <string>
#include <boost/any.hpp>


template <typename Ret, typename... Args>
Ret callfunc (std::function<Ret(Args...)> func, std::vector<boost::any> anyargs);

template <typename Ret>
Ret callfunc (std::function<Ret()> func, std::vector<boost::any> anyargs)
{
    if (anyargs.size() > 0)
        throw std::runtime_error("oops, argument list too long");
    return func();
}

template <typename Ret, typename Arg0, typename... Args>
Ret callfunc (std::function<Ret(Arg0, Args...)> func, std::vector<boost::any> anyargs)
{
    if (anyargs.size() == 0)
        throw std::runtime_error("oops, argument list too short");
    Arg0 arg0 = boost::any_cast<Arg0>(anyargs[0]);
    anyargs.erase(anyargs.begin());
    std::function<Ret(Args... args)> lambda =
        ([=](Args... args) -> Ret {
         return func(arg0, args...);
    });
    return callfunc (lambda, anyargs);
}

template <typename Ret, typename... Args>
std::function<boost::any(std::vector<boost::any>)> adaptfunc (Ret (*func)(Args...)) {
    std::function<Ret(Args...)> stdfunc = func;
    std::function<boost::any(std::vector<boost::any>)> result =
        ([=](std::vector<boost::any> anyargs) -> boost::any {
         return boost::any(callfunc(stdfunc, anyargs));
         });
    return result;
}

adaptfunc(your_function), your_function - ( varargs). std::function, boost::any a boost::any. func_map , .

.

void , boost::any<void> . , void. .

:

int func1 (int a)
{
    std::cout << "func1(" << a << ") = ";
    return 33;
}

int func2 (double a, std::string b)
{
    std::cout << "func2(" << a << ",\"" << b << "\") = ";
    return 7;
}

int func3 (std::string a, double b)
{
    std::cout << "func3(" << a << ",\"" << b << "\") = ";
    return 7;
}

int func4 (int a, int b)
{
    std::cout << "func4(" << a << "," << b << ") = ";
    return a+b;
}


int main ()
{
    std::vector<std::function<boost::any(std::vector<boost::any>)>> fcs = {
        adaptfunc(func1), adaptfunc(func2), adaptfunc(func3), adaptfunc(func4) };

    std::vector<std::vector<boost::any>> args =
    {{777}, {66.6, std::string("yeah right")}, {std::string("whatever"), 0.123}, {3, 2}};

    // correct calls will succeed
    for (int i = 0; i < fcs.size(); ++i)
        std::cout << boost::any_cast<int>(fcs[i](args[i])) << std::endl;

    // incorrect calls will throw
    for (int i = 0; i < fcs.size(); ++i)
        try {
            std::cout << boost::any_cast<int>(fcs[i](args[fcs.size()-1-i])) << std::endl;
        } catch (std::exception& e) {
            std::cout << "Could not call, got exception: " << e.what() << std::endl;
        }
}
+6

@TonyTheLion, boost::variant boost::any :

typedef std::function<void(const std::string&, const std::vector<boost::variant<char, int>>&)> varying_args_fn;
std::map<std::string, varying_args_fn> func_map;

, , , . , , tokens , boost::variant , :

#include <map>
#include <vector>
#include <string>
#include <functional>
#include <iostream>

#include <boost/variant.hpp>
#include <boost/any.hpp>

typedef std::function<void(const std::string&, const std::vector<boost::variant<char, int>>&)> varying_args_fn;

void printall(const std::string& tokens, const std::vector<boost::variant<char, int>>& args) {
  for (const auto& x : args) {
    struct : boost::static_visitor<> {
      void operator()(int i) {
        std::cout << "Int: " << i << ' ';
      }
      void operator()(char c) {
        std::cout << "Char: " << c << ' ';
      }
    } visitor;
    boost::apply_visitor(visitor, x);
  }
}

int main() {
  std::map<std::string, varying_args_fn> func_map;
  func_map["printall"] = printall;
  func_map["printall"]("iic", {5, 10, 'x'});
}
+2

All Articles