🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
OpBinRegistry.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstScript/ScriptAPI.hpp"
25#include <tuple>
26#include <unordered_map>
27
28AST_NAMESPACE_BEGIN
29
30
32{
33public:
34 // @brief 运算函数注册表键类型
35 typedef std::tuple<int, Class*, Class*> OpBinKey;
36
37 // @brief 运算函数注册表哈希函数
38 struct OpBinKeyHash {
39 std::size_t operator()(const OpBinKey& key) const {
40 // 组合哈希值
41 std::size_t h1 = std::hash<int>()(static_cast<int>(std::get<0>(key)));
42 std::size_t h2 = std::hash<const void*>()(std::get<1>(key));
43 std::size_t h3 = std::hash<const void*>()(std::get<2>(key));
44 // 使用位运算组合哈希值
45 return h1 ^ (h2 << 1) ^ (h3 << 2);
46 }
47 };
48 typedef std::unordered_map<OpBinKey, void*, OpBinKeyHash> OpBinMap;
49public:
50 OpBinRegistry() = default;
51 ~OpBinRegistry() = default;
52
53 void* getFunc(EOpBinType op, Class* leftType, Class* rightType)
54 {
55 auto key = OpBinKey{static_cast<int>(op), leftType, rightType};
56 auto it = map_.find(key);
57 if(it == map_.end()){
58 return nullptr;
59 }
60 return it->second;
61 }
62 void regFunc(EOpBinType op, Class* leftType, Class* rightType, void* func)
63 {
64 map_[OpBinKey{static_cast<int>(op), leftType, rightType}] = func;
65 }
66protected:
67 OpBinMap map_;
68};
69
70AST_NAMESPACE_END
类元信息
定义 Class.hpp:37
定义 OpBinRegistry.hpp:32
EOpBinType
定义 ScriptAPI.hpp:40
定义 OpBinRegistry.hpp:38