🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
CommandRouting.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/StringView.hpp"
25#include "AstUtil/ValueView.hpp"
26#include "CommandUtil.hpp"
27#include <vector>
28#include <memory>
29
30
31AST_NAMESPACE_BEGIN
32
41class CommandParams: public std::vector<ValueView>
42{
43public:
44 // todo
45};
46
47
50{
51public:
56 virtual errc_t handle(const CommandParams& params, CommandResult& result) const = 0;
57
58 virtual ~CommandHandler() = default;
59};
60
61
64{
65public:
66 CommandParams& params() { return params_; }
67 CommandHandler* handler() { return handler_; }
68 void setHandler(CommandHandler* handler) { handler_ = handler; }
69private:
70 CommandParams params_;
71 CommandHandler* handler_{nullptr};
72};
73
74
75
77class AST_CMD_API CommandTrie
78{
79public:
80 class Node
81 {
82 public:
83 Node() = default;
84 Node(StringView key)
85 : key_(key)
86 {}
88 const Node* findChild(StringView key) const
89 {
90 for(auto& child : children_)
91 {
92 if (key == child.key_)
93 return &child;
94 }
95 return nullptr;
96 }
99 {
100 for (auto& child : children_)
101 {
102 if (key == child.key_)
103 return child;
104 }
105 children_.emplace_back(key);
106 auto& child = children_.back();
107 return child;
108 }
109 public:
110 int numParams_{-1};
111 std::string key_;
112 std::shared_ptr<CommandHandler> handler_;
113 std::vector<Node> children_;
114 };
115 Node& addRule(StringView tmpl);
116 void addRule(StringView tmpl, std::shared_ptr<CommandHandler> handler);
117 errc_t find(StringView text, RoutingHandleResult& result) const;
118private:
119 Node root_;
120};
121
122
125AST_NAMESPACE_END
命令处理接口
定义 CommandRouting.hpp:50
virtual errc_t handle(const CommandParams &params, CommandResult &result) const =0
命令参数
定义 CommandRouting.hpp:42
定义 CommandAPI.hpp:37
定义 CommandRouting.hpp:81
const Node * findChild(StringView key) const
查找子节点
定义 CommandRouting.hpp:88
std::vector< Node > children_
子节点
定义 CommandRouting.hpp:113
Node & ensureChild(StringView key)
确保子节点存在
定义 CommandRouting.hpp:98
std::string key_
键值
定义 CommandRouting.hpp:111
std::shared_ptr< CommandHandler > handler_
命令处理函数
定义 CommandRouting.hpp:112
路由树
定义 CommandRouting.hpp:78
路由结果
定义 CommandRouting.hpp:64