vendredi 23 mars 2018

How to declare "basic" type for argument of generic std::function

I'm trying to write a generic wrapper (for some script interpreter) for function/class method that would convert all call parameters from string to some arbitrary type T. I'll try to cover topic in points:

  • Script allows to map user function
  • When interpreter tries to process user function - a callback routine is made
  • Callback is intended to take array of objects that describe (one-by-one) arguments' values
  • I already got (template) routines that converts string to arbitrary (basic) type T
  • I would like to wrap user routine (provided externally as variadic std::function<> type) so that conversion from subsequent strings from callback's array to appropriate argument is done automatically

Example:

Prototype for callback routine is as follows:

int CallbackFn(Interp *interp, int argc, const char **argv)

I got (sample) user function:

int UserRoutine(const std::string &in_str, int x);

so std::function would look like:

std::function<int(const std::string&, int)>

Generic conversion routine has syntax:

template <typename T>
T conv(const char *str);

I have specializations that convert:

  1. "const char*" to "std::string"
  2. "const char*" to "int"

so that ideally conversion would look like:

std::string p0 = conv<std::string>(argv[0]);
int p1 = conv<int>(argv[1]);

It could be wrapped all into variadic templates, but std::function<...> arguments do not exactly match the types i'm preparing - e.g. it is very common to pass objects as const T&, while i need to create "pure" type of T.

Any ideas how to handle different means of passing arguments?

Aucun commentaire:

Enregistrer un commentaire