🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
CommandUtil.hpp
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/RTTIAPI.hpp"
25#include "AstUtil/ValueView.hpp"
26#include "AstUtil/Span.hpp"
27#include "AstUtil/ParseFormat.hpp"
28#include "CommandAPI.hpp"
29#include <unordered_map>
30#include <vector>
31#include <string>
32#include <memory>
33#include <type_traits>
34
35
36AST_NAMESPACE_BEGIN
37
44#ifndef SWIG
45
46namespace detail{
47
49constexpr int find_closing(const char* s, int p) {
50 return s[p] == '>' ? p : find_closing(s, p + 1);
51}
52
53#ifdef A_CXX14
55constexpr bool is_type(const char* s, int p, const char* type) {
56 int i = 0;
57 while (type[i] != '\0') {
58 if (s[p + i] != type[i]) return false;
59 ++i;
60 }
61 // 允许类型后紧跟 '>' 或 ':'(名称注释)
62 return s[p + i] == '>' || s[p + i] == ':';
63}
64#else
65// 辅助递归函数,比较类型字符串并检查终止字符
66constexpr bool is_type_impl(const char* s, int p, const char* type, int i) {
67 return type[i] == '\0'
68 ? (s[p + i] == '>' || s[p + i] == ':')
69 : (s[p + i] == type[i] && is_type_impl(s, p, type, i + 1));
70}
71
73constexpr bool is_type(const char* s, int p, const char* type) {
74 return is_type_impl(s, p, type, 0);
75}
76#endif
77
79constexpr int count_args(const char* s, int p = 0) {
80 return s[p] == '\0' ? 0 :
81 s[p] == '<' ? 1 + count_args(s, p + 1) :
82 count_args(s, p + 1);
83}
84
85
86enum{
87 type_none = 0,
88 type_bool = 1,
89 type_int = 2,
90 type_double = 3,
91 type_string = 4,
92 type_object = 5,
93 num_types = 6,
94};
95
96constexpr uint64_t get_parameter_tag(const char* s, int p = 0) {
97 return s[p] == '\0' ? 0 :
98 s[p] == '<' ? (
99 is_type(s, p+1, "bool") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_bool :
100 is_type(s, p+1, "int") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_int :
101 is_type(s, p+1, "double") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_double :
102 is_type(s, p+1, "string") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_string :
103 is_type(s, p+1, "object") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_object :
104 throw "invalid parameter type"
105 ) :
106 get_parameter_tag(s, p + 1);
107}
108
109template <int Pos, typename T>
110struct arg_pair {
111 static constexpr int pos = Pos;
112 using type = T;
113};
114
115
116// 1. 定义类型码到 C++ 类型的映射 traits
117template <int TypeCode>
119
120template <> struct type_from_tag<type_bool> { using type = bool; }; // <bool> → bool
121template <> struct type_from_tag<type_int> { using type = int; }; // <int> → int
122template <> struct type_from_tag<type_double> { using type = double; }; // <double> → double
123template <> struct type_from_tag<type_string> { using type = StringView; }; // <string> → StringView
124template <> struct type_from_tag<type_object> { using type = Object*; }; // <object> → Object*
125
126
127// 2. 极度简化的 make_arg_pair
128template <uint64_t Tag, int Pos>
132
133// 递归解码,用 std::tuple 累积 arg_pair 类型(避免 seq 可变参数包问题)
134template <uint64_t Tag, int Pos, int Count>
136 using type = decltype(
137 std::tuple_cat(
138 std::tuple<typename make_arg_pair<Tag % num_types, Pos>::type>(),
139 typename decode_impl<Tag / num_types, Pos + 1, Count - 1>::type()
140 )
141 );
142};
143
144template <uint64_t Tag, int Pos>
145struct decode_impl<Tag, Pos, 0> {
146 using type = std::tuple<>;
147};
148
149
150
151// 对外接口:给定 tag 和参数个数 N,得到 std::tuple<arg_pair<Pos, Type>...>
152template <uint64_t Tag, int N>
153using decode_tag = typename decode_impl<Tag, 0, N>::type;
154
155
156inline std::vector<StringView> split(StringView str)
157{
158 std::vector<StringView> result;
159 const char* p = str.data();
160 const char* end = p + str.size();
161
162 while (p < end) {
163 // 跳过前导空白
164 while (p < end && std::isspace(static_cast<unsigned char>(*p))) {
165 ++p;
166 }
167 if (p >= end) break;
168
169 if (*p == '"') {
170 ++p; // 跳过左引号
171 const char* start = p;
172 while (p < end && *p != '"') {
173 ++p;
174 }
175 // 添加引号内的内容(可能为空)
176 result.emplace_back(start, static_cast<std::size_t>(p - start));
177 ++p; // 跳过右引号
178 } else {
179 const char* start = p;
180 while (p < end && !std::isspace(static_cast<unsigned char>(*p)) && *p != '"') {
181 ++p;
182 }
183 result.emplace_back(start, static_cast<std::size_t>(p - start));
184 }
185 }
186 return result;
187}
188
189inline std::string extract_command_name(const char* tmpl) {
190 std::string name;
191 while (*tmpl && *tmpl != ' ' && *tmpl != '<') {
192 name += *tmpl++;
193 }
194 return name;
195}
196
197
198// ================== 运行时字符串转类型 ==================
199
200template <typename T>
201T convert_token(StringView s);
202
203template <>
204inline bool convert_token<bool>(StringView s) {
205 bool value;
206 if (aParseBool(s, value))
207 throw std::runtime_error(std::string("invalid bool value: ") + std::string(s));
208 return value;
209}
210
211template <>
212inline int convert_token<int>(StringView s) {
213 int value;
214 if (aParseInt(s, value))
215 throw std::runtime_error(std::string("invalid int value: ") + std::string(s));
216 return value;
217}
218
219
220template <>
221inline double convert_token<double>(StringView s) {
222 double value;
223 if (aParseDouble(s, value))
224 throw std::runtime_error(std::string("invalid double value: ") + std::string(s));
225 return value;
226}
227
228template<>
229inline Object* convert_token<Object*>(StringView s) {
230 Object* value = aResolveObject(s);
231 return value;
232}
233
234template <>
235inline StringView convert_token<StringView>(StringView s) {
236 return s;
237}
238
239
240// 处理 void 返回值
241inline errc_t fill_result(CommandResult& /*result*/) {
242 return 0;
243}
244
245// 处理 int(及其他整数类型)
246template <typename T>
247typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, errc_t>::type
248fill_result(CommandResult& /*result*/, T ret) {
249 return static_cast<errc_t>(ret);
250}
251
252// 处理 std::vector<std::string>
253inline errc_t fill_result(CommandResult& result, std::vector<std::string> vec) {
254 result = std::move(vec);
255 return 0;
256}
257
258// 处理 std::string
259inline errc_t fill_result(CommandResult& result, const std::string& str) {
260 result.push_back(str);
261 return 0;
262}
263inline errc_t fill_result(CommandResult& result, std::string&& str) {
264 result.push_back(std::move(str));
265 return 0;
266}
267// 处理 const char*
268inline errc_t fill_result(CommandResult& result, const char* str) {
269 result.push_back(str);
270 return 0;
271}
272template <typename T>
273typename std::enable_if<!std::is_integral<T>::value &&
274 !std::is_same<T, std::vector<std::string>>::value &&
275 !std::is_same<T, std::string>::value &&
276 !std::is_same<T, const char*>::value, errc_t>::type
277fill_result(CommandResult& /*result*/, T ret) {
278 return static_cast<errc_t>(ret);
279}
280
281
282} // namespace detail
283
284#endif
285
288AST_NAMESPACE_END
Unit s
定义 Unit.cpp:435
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:86
errc_t aParseDouble(StringView str, double &value)
将字符串转换为双精度浮点数
定义 ParseFormat.cpp:253
errc_t aParseBool(StringView str, bool &value)
将字符串转换为布尔值
定义 ParseFormat.cpp:50
errc_t aParseInt(StringView str, int &value)
将字符串转换为整数
定义 ParseFormat.cpp:72
Object * aResolveObject(StringView value, Class *cls)
解析对象
定义 RTTIAPI.cpp:94
定义 CommandUtil.hpp:110
定义 CommandUtil.hpp:135
定义 CommandUtil.hpp:129
定义 CommandUtil.hpp:118