当前位置: 首页 > news >正文

使用c++14标准实现函数注册包装

调用方式bool res = FunctionRegistry::callFromFuncMap1<bool, type1&, type2*, type3,... >(...),其中第一个bool为返回值类型,第二个开始后面均为参数列表类型,可用于注册算子,包装函数指针等。

#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>namespace op_select {#define ORAM_BASE 0x15100000class FunctionWrapperBase {
public:virtual ~FunctionWrapperBase() = default;
};template <typename ReturnType, typename... Args>
class FunctionWrapper : public FunctionWrapperBase {
private:std::function<ReturnType(Args...)> func_;public:FunctionWrapper(std::function<ReturnType(Args...)> func) : func_(func) {}ReturnType operator()(Args... args) {return func_(args...);}
};class FunctionRegistry {
private:static std::unordered_map<std::string, std::shared_ptr<FunctionWrapperBase>>& getFuncMap1() {static std::unordered_map<std::string, std::shared_ptr<FunctionWrapperBase>> func_map1;return func_map1;}static std::unordered_map<std::string, std::shared_ptr<FunctionWrapperBase>>& getFuncMap2() {static std::unordered_map<std::string, std::shared_ptr<FunctionWrapperBase>> func_map2;return func_map2;}static std::mutex& getMutex1() {static std::mutex mutex1;return mutex1;}static std::mutex& getMutex2() {static std::mutex mutex2;return mutex2;}public:FunctionRegistry() = delete;FunctionRegistry(const FunctionRegistry&) = delete;FunctionRegistry& operator=(const FunctionRegistry&) = delete;template <typename ReturnType, typename... Args>static bool registerToFuncMap1(const std::string& name, ReturnType (*func)(Args...)) {std::lock_guard<std::mutex> lock(getMutex1());std::function<ReturnType(Args...)> f = func;auto wrapper = std::make_shared<FunctionWrapper<ReturnType, Args...>>(f);return getFuncMap1().emplace(name, wrapper).second;}template <typename ReturnType, typename... Args>static bool registerToFuncMap1(const std::string& name, std::function<ReturnType(Args...)> func) {std::lock_guard<std::mutex> lock(getMutex1());auto wrapper = std::make_shared<FunctionWrapper<ReturnType, Args...>>(func);bool res = getFuncMap1().emplace(name, wrapper).second;return getFuncMap1().emplace(name, wrapper).second;}template <typename ReturnType, typename... Args>static bool registerToFuncMap2(const std::string& name, ReturnType (*func)(Args...)) {std::lock_guard<std::mutex> lock(getMutex2());std::function<ReturnType(Args...)> f = func;auto wrapper = std::make_shared<FunctionWrapper<ReturnType, Args...>>(f);return getFuncMap2().emplace(name, wrapper).second;}template <typename ReturnType, typename... Args>static bool registerToFuncMap2(const std::string& name, std::function<ReturnType(Args...)> func) {std::lock_guard<std::mutex> lock(getMutex2());auto wrapper = std::make_shared<FunctionWrapper<ReturnType, Args...>>(func);return getFuncMap2().emplace(name, wrapper).second;}template <typename ReturnType, typename... Args>static ReturnType callFromFuncMap1(const std::string& name, Args&&... args) {std::lock_guard<std::mutex> lock(getMutex1());auto it = getFuncMap1().find(name);if (it == getFuncMap1().end()) {throw std::runtime_error("Function '" + name + "' not found in FuncMap1");}auto wrapper = std::dynamic_pointer_cast<FunctionWrapper<ReturnType, Args...>>(it->second);if (!wrapper) {throw std::runtime_error("Type mismatch for function '" + name + "' in FuncMap1");}return (*wrapper)(std::forward<Args>(args)...);}template <typename ReturnType, typename... Args>static ReturnType callFromFuncMap2(const std::string& name, Args&&... args) {std::lock_guard<std::mutex> lock(getMutex2());auto it = getFuncMap2().find(name);if (it == getFuncMap2().end()) {throw std::runtime_error("Function '" + name + "' not found in FuncMap2");}auto wrapper = std::dynamic_pointer_cast<FunctionWrapper<ReturnType, Args...>>(it->second);if (!wrapper) {throw std::runtime_error("Type mismatch for function '" + name + "' in FuncMap2");}return (*wrapper)(std::forward<Args>(args)...);}
};#define REGISTER_FUNCTION_TO_FUNC_MAP1(name, func)                                                                     \static bool _reg##name = [] {                                                                                      \FunctionRegistry::registerToFuncMap1(#name, func);                                                             \return true;                                                                                                   \}()#define REGISTER_FUNCTION_TO_FUNC_MAP1(name, func)                                                                     \static bool _reg##name = [] {                                                                                      \FunctionRegistry::registerToFuncMap2(#name, func);                                                             \return true;                                                                                                   \}()#define REGISTER_FUNCTION_TO_BOTH(name, func1, func2)                                                                  \static bool _reg##name = [] {                                                                                      \FunctionRegistry::registerToFuncMap1(#name, func1);                                                            \FunctionRegistry::registerToFuncMap2(#name, func2);                                                            \return true;                                                                                                   \}()#define REGISTER_LAMBDA_TO_FUNC_MAP1(name, return_type, ...)                                                           \static bool _reg##name = [] {                                                                                      \FunctionRegistry::registerToFuncMap1<return_type, __VA_ARGS__>(name, std::function<return_type(__VA_ARGS__)>); \return true;                                                                                                   \}()#define REGISTER_LAMBDA_TO_FUNC_MAP2(name, return_type, ...)                                                           \static bool _reg##name = [] {                                                                                      \FunctionRegistry::registerToFuncMap2<return_type, __VA_ARGS__>(name, std::function<return_type(__VA_ARGS__)>); \return true;                                                                                                   \}()}  // namespace op_select
http://www.sczhlp.com/news/214014/

相关文章:

  • 正规刷手机单做任务网站水源logo设计制作网
  • 结对项目-实现一个自动生成小学四则运算题目的命令行程序
  • 免费学编程的网站有哪些怎么制作微信链接
  • 网站源码怎么做wordpress和jwplayer
  • 网站页面图片尺寸wordpress无法安装500
  • 破解WordPress站点网站开发学习步骤
  • 内江 网站建设合肥论坛网站建设
  • 网站建设的计划国家建筑信息管理平台
  • 电商店铺图片邵阳网站seo
  • 购物网站开发django十大待遇最好央企
  • 怎么做垂直门户网站做网站的技术难点
  • 城乡建设网站网站建设 方案 评价表
  • 电子商务网站建设的目标是什么兰州手机网站制作公司
  • 网站定制开发报价单网站内容收费
  • 欧美做的爱爱网站有哪些室内设计说明200字
  • 企业做网站预付账款会计分录wordpress自定义文章类型面包屑
  • 荣欣建设集团有限公司网站wordpress实现表格填写功能
  • 旅游网站只做什么是网络营销取得成功的基础
  • 找代理做网站网站域名归属谁设计网站教程
  • 柯桥教育网站建设如何自己做官网
  • 北京php网站制作宝塔wordpress安装页面打不开
  • 怎样学网站开发网站关键词没被搜出来
  • 能打开的a站天长网站seo
  • 关于网站建设的书个人视频网站应该怎么做
  • wordpress互动主题wordpress优化记录
  • 昌宁网站建设广广东网站建设
  • 百度网盘非会员下载慢怎么解决 - fosgrignonhto
  • CF1973 Codeforces Round 945 (Div. 2) 游记(VP)
  • 做电影资讯网站算侵权吗拟采用建站技术
  • 2025年不锈钢酸洗钝化液厂家推荐排行榜,环保型不锈钢管酸洗钝化液,不锈钢清洗钝化液,酸洗钝化处理工艺及不锈钢清洗剂公司推荐