veselink1 / refl-cpp

Static reflection for C++17 (compile-time enumeration, attributes, proxies, overloads, template functions, metaprogramming).
https://veselink1.github.io/refl-cpp/md__introduction.html
MIT License
1.05k stars 76 forks source link

How to implement my case with this library? #73

Closed paulocoutinhox closed 1 year ago

paulocoutinhox commented 1 year ago

Hi,

How to implement my case with this library?

My case is this: https://github.com/paulocoutinhox/cpp-native-mobile/blob/main/cxx/src/main.cpp#L90

I have this scenario:

[mobile] <==[send and receive json string] ==> [cxx library]

When my cxx library receive the json string, it will be like this:

json j = json::parse(R"({
            "function": "test",
            "param": true,
            "param-type": "bool",
})");

The function value is the cxx function name inside my mapping. The param value is the value that will be converted to a type. The param-type value is the type of param that will be sent to mapped function.

So in practice, i only need do something like this:

void testMethod(const bool &param)
{
      //
}

std::map<std::string, std::function<void(const T &)>> flist; // i don't know how to store a type that can be any type (string, int, Product, custom)

flist["test"] = & testMethod;

json j = json::parse(R"({
    "function": "test",
    "param": true,
    "param-type": "bool"
})");

auto function = j["function"].get<std::string>();
auto param = j["param"].get<???>(); // error here - i don't know how to get the real type
flist[function](param);

The problem is that i need get the type dynamically, and appear that C++ can't do it. I can't store the type.

There is any approach with your library that i can make something like this?

I need call a function by name, generally inside a class (can be static or i can construct when need if need), and call with a unique param that is converted from json, but the type is inside the json or inside in the mapping when program start. I can't use the type on parsing, because parsing happen in an adapter layer (it don't know nothing about the final type, but receive it).

veselink1 commented 1 year ago

This question is too broad and so I will be closing this issue. Please do not hesitate to open issues if you discover a bug in refl-cpp or if you would like to request a feature to be implemented.