0

I have a a bunch of subclasses each with static class factory methods of varying argument numbers and types, and would also like to instantiate them from data loaded via a json data file using jsonpp.

So say I'll have an array of Json::Value objects for the arguments, is there a smart way, using macros or something in c++ to create alternate proxy methods that accepts the Json::Value array and calls the original method, e.g.)

Foo* Foo::create(int a, const char* b) { /* ... */ }
Foo* Foo::create(Json::Value args) { //the Json::Value args represents an array value here
    // can I avoid having to manually create each of these Json methods?
    return Foo::create(args[0].asInt(), args[1].asCString());
}

Sorry, my c++ is still getting up to speed. I've found a few topics that seem to deal with unpacking tuple arguments for example,

but I am unsure of which approach to proceed with. Thanks for the help

Community
  • 1
  • 1
magoo
  • 151
  • 1
  • 3
  • 9
  • Do you have access to C++11 or C++03? C++11 had methods of handling this. If you only have C++03, then you will have to do a lot of the work yourself. – Cort Ammon Sep 14 '13 at 19:46
  • Hi Cort, yes, I am compiling with C++11 – magoo Sep 15 '13 at 20:05
  • You stated you load some data from a file and use it to instantiate some classes. If the number of arguments (or their types) for this `create` function call is dependent on the loaded data, then there's no sane way to do it. Templates are "resolved" at compile-time, which means to use a pack expansion, the types and number of arguments must be known at compile-time. – dyp Sep 15 '13 at 23:42

0 Answers0