用于不同功能的容器
Container for different functions?
我正在尝试为不同的函数实现一个容器类,我可以在其中保存函数指针并在以后使用它来调用这些函数。我会尝试更准确地描述我的问题。
例如,我有 2 个不同的测试函数:
int func1(int a, int b) {
printf("func1 works! %i %in", a, b);
return 0;
}
void func2(double a, double b) {
printf("func2 works! %.2lf %.2lfn", a, b);
}
我还有一系列变体,其中包含函数参数:
std::vector<boost::variant<int, double>> args = {2.2, 3.3};
我决定使用我自己的从某个基类派生的函子类(我考虑过使用虚拟方法):
class BaseFunc {
public:
BaseFunc() {}
~BaseFunc() {}
};
template <typename T>
class Func;
template <typename R, typename... Tn>
class Func<R(Tn...)> : public BaseFunc {
typedef R(*fptr_t)(Tn...);
fptr_t fptr;
public:
Func() : fptr(nullptr) {}
Func(fptr_t f) : fptr(f) {}
R operator()(Tn... args) {
return fptr(args...);
}
Func& operator=(fptr_t f) {
fptr = f;
return *this;
}
};
此外,我决定存储一些有关函数及其参数的信息:
struct TypeInfo {
int type_id; // for this example: 0 - int, 1 - double
template <class T>
void ObtainType() {
if (std::is_same<void, T>::value)
type_id = 0;
else if (std::is_same<int, T>::value)
type_id = 1;
else if (std::is_same<double, T>::value)
type_id = 2;
else
type_id = -1;
}
};
struct FunctionInfo {
public:
FunctionInfo() {}
FunctionInfo(BaseFunc *func, const TypeInfo& ret, std::vector<TypeInfo>& args) :
func_ptr(func), return_info(ret)
{
args_info.swap(args);
}
~FunctionInfo() {
delete func_ptr;
}
BaseFunc * func_ptr;
TypeInfo return_info;
std::vector<TypeInfo> args_info;
};
所以现在我可以定义一个容器类:
class Container {
private:
template <size_t n, typename... T>
void ObtainTypeImpl(size_t i, TypeInfo& t)
{
if (i == n)
t.ObtainType<std::tuple_element<n, std::tuple<T...>>::type>();
else if (n == sizeof...(T)-1)
throw std::out_of_range("Tuple element out of range.");
else
ObtainTypeImpl<(n < sizeof...(T)-1 ? n + 1 : 0), T...>(i, t);
}
template <typename... T>
void ObtainType(size_t i, TypeInfo& t)
{
return ObtainTypeImpl<0, T...>(i, t);
}
public:
template <class R, class ...Args>
void AddFunc(const std::string& str, R(*func)(Args...)) {
BaseFunc * func_ptr = new Func<R(Args...)>(func);
size_t arity = sizeof...(Args);
TypeInfo ret;
ret.ObtainType<R>();
std::vector<TypeInfo> args;
args.resize(arity);
for (size_t i = 0; i < arity; ++i)
{
ObtainType<Args...>(i, args[i]);
}
cont_[str] = FunctionInfo(func_ptr, ret, args);
}
void CallFunc(const std::string& func_name,
std::vector<boost::variant<int, double>>& args_vec) {
auto it = cont_.find(func_name);
if (it != cont_.end())
{
// ???????
// And here I stucked
}
}
private:
std::map<std::string, FunctionInfo> cont_;
};
然后我卡住了。
- 不知道如何从我的结构:)中获取函数类型信息。
- 不知道如何将变体向量转换为参数列表。
也许我的道路是错误的?除了像Lua这样的脚本引擎之外,你能提出这个问题的任何解决方案吗?
你可以做这样的事情:
class BaseFunc {
public:
virtual ~BaseFunc() = default;
virtual void Call(std::vector<boost::variant<int, double>>& args_vec) const = 0;
};
template <typename F> class Function;
template <typename R, typename... Args> class Function<R(Args...)> : public BaseFunc
{
public:
Function(R (*f)(Args...)) : f(f) {}
void Call(std::vector<boost::variant<int, double>>& args_vec) const override
{
Call(args_vec, std::index_sequence_for<Args...>());
}
private:
template <std::size_t ... Is>
void Call(
std::vector<boost::variant<int, double>>& args_vec,
std::index_sequence<Is...>) const
{
// Add additional check here if you want.
f(boost::get<Args>(args_vec.at(Is))...);
}
private:
R (*f)(Args...);
};
现场示例
相关文章:
- 在用于格式4的arm模拟器中实现功能时的一个问题
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 删除功能不适用于串行通信后多个循环中的多个实例
- 不正确的比较和交换计数器输出用于快速排序功能
- 私有成员数据不可用于公共成员功能
- 如何编写用于多映射插入和擦除功能的 API?
- '_HAS_CXX17'宏是否可用于自定义项目标头以启用C++17 语言集功能?
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- C++中用于结构的纯数组的类似于TableView/DataFrame的通用功能
- C++用于填充树右侧的功能
- 用于检查我们是否有有效数独的辅助功能
- 如何实现通用 C++ 用于配置功能?
- "no main"用于C++中链接或执行的功能
- 如何使用 Turbo C++并行运行两个功能,一个用于键盘,一个用于鼠标?
- 维护/维持两个代码集的风险,一个用于 CPU,一个用于 GPU,需要执行非常相似的功能
- 是否有用于执行反向字符串拆分器的开箱即用功能?
- 二叉树插入功能不适用于 10 个条目
- STL功能用于确定距离是否在n`中
- 我可以将 AVX/SSE 与旋转功能用于 AoS 布局,而不是 SoA 吗?
- 用户定义的功能用于竞争编程中,以获取输入