问题描述
我有一些结构可以在FunDeclaration
和FunParam
中保存我现在需要的内容,并有一个示例函数foo
来反映。实时代码:https://onlinegdb.com/BySyS8f7D
完整的来源:
#include <iostream>
#include <string>
#include <unordered_map>
#include <any>
struct FunParam {
// This will be a custom identifier (string for Now) "int","string",etc...
std::string type;
// Default value for that parameter
std::any default_value;
};
struct FunDeclaration {
// Function input params
std::unordered_map<std::string,FunParam> ins;
// Function output params
std::unordered_map<std::string,FunParam> outs;
};
using FunctionsMap = std::unordered_map<std::string,FunDeclaration>;
void print(FunctionsMap &fmap) {
auto printParam = [](FunParam& p) {
if (p.type == "int") {
std::cout << "type: " << p.type << " default_value: " << std::any_cast<int>(p.default_value);
} else if (p.type == "double") {
std::cout << "type: " << p.type << " default_value: " << std::any_cast<double>(p.default_value);
} else if (p.type == "float") {
std::cout << "type: " << p.type << " default_value: " << std::any_cast<float>(p.default_value);
} else if (p.type == "std::string") {
std::cout << "type: " << p.type << " default_value: " << std::any_cast<std::string>(p.default_value);
}
};
for (auto& f : fmap) {
std::cout << "Fun: " << f.first << std::endl;
for (auto& in: f.second.ins) {
std::cout << "\t[in] name: " << in.first << " ";
printParam(in.second);
std::cout << std::endl;
}
for (auto& in : f.second.outs) {
std::cout << "\t[out] name: " << in.first << " ";
printParam(in.second);
std::cout << std::endl;
}
}
}
// Just an example function to work with,multiple inputs (default values),and multiple outputs
std::tuple<double,float> foo(int a = 10,std::string b = "HelloWorld") {
return { a * 10.0,b.size() };
}
int main() {
FunctionsMap gFuns;
gFuns["foo"].ins =
{
{"a",{"int",std::any(int(10))} },{"b",{"std::string",std::any(std::string("HelloWorld"))} }
};
gFuns["foo"].outs = {
{"out0",{"double",std::any(double())} },{"out1",{"float",std::any(float())} }
};
print(gFuns);
return 1;
}
您将如何定义一个宏来吐出此粘合代码(假设gFuns是全局的)并具有多个输入/输出?仅使用一个宏就可以吗?还是我必须对输入/输出参数编号的每种可能性做一个宏?
gFuns["foo"].ins =
{
{"a",std::any(float())} }
};
我正在考虑这样的事情:
#define ADD_FUN_DECL(name,in_types,in_defaultvalues,out_types,out_defaultvalues)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)