🛰️航天仿真算法库 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 // 使用 | 而不是 ||,使用 || 运算符在一些旧编译器中无法编译期求值,
69 // 可能因为 || 的短路求值特性,一些旧编译器实现存在缺陷
70 ? ((s[p + i] == '>') | (s[p + i] == ':'))
71 : (s[p + i] == type[i] && is_type_impl(s, p, type, i + 1));
72}
73
75constexpr bool is_type(const char* s, int p, const char* type) {
76 return is_type_impl(s, p, type, 0);
77}
78#endif
79
81constexpr int count_args(const char* s, int p = 0) {
82 return s[p] == '\0' ? 0 :
83 s[p] == '<' ? 1 + count_args(s, p + 1) :
84 count_args(s, p + 1);
85}
86
87
88enum{
89 type_none = 0,
90 type_bool = 1,
91 type_int = 2,
92 type_double = 3,
93 type_string = 4,
94 type_object = 5,
95 num_types = 6,
96};
97
98constexpr uint64_t get_parameter_tag(const char* s, int p = 0) {
99 return s[p] == '\0' ? 0 :
100 s[p] == '<' ? (
101 is_type(s, p+1, "bool") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_bool :
102 is_type(s, p+1, "int") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_int :
103 is_type(s, p+1, "double") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_double :
104 is_type(s, p+1, "string") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_string :
105 is_type(s, p+1, "object") ? get_parameter_tag(s, find_closing(s, p)+1) * num_types + type_object :
106 throw "invalid parameter type"
107 ) :
108 get_parameter_tag(s, p + 1);
109}
110
111template <int Pos, typename T>
112struct arg_pair {
113 static constexpr int pos = Pos;
114 using type = T;
115};
116
117
118// 1. 定义类型码到 C++ 类型的映射 traits
119template <int TypeCode>
121
122template <> struct type_from_tag<type_bool> { using type = bool; }; // <bool> → bool
123template <> struct type_from_tag<type_int> { using type = int; }; // <int> → int
124template <> struct type_from_tag<type_double> { using type = double; }; // <double> → double
125template <> struct type_from_tag<type_string> { using type = StringView; }; // <string> → StringView
126template <> struct type_from_tag<type_object> { using type = Object*; }; // <object> → Object*
127
128
129// 2. 极度简化的 make_arg_pair
130template <uint64_t Tag, int Pos>
134
135// 递归解码,用 std::tuple 累积 arg_pair 类型(避免 seq 可变参数包问题)
136template <uint64_t Tag, int Pos, int Count>
138 using type = decltype(
139 std::tuple_cat(
140 std::tuple<typename make_arg_pair<Tag % num_types, Pos>::type>(),
141 typename decode_impl<Tag / num_types, Pos + 1, Count - 1>::type()
142 )
143 );
144};
145
146template <uint64_t Tag, int Pos>
147struct decode_impl<Tag, Pos, 0> {
148 using type = std::tuple<>;
149};
150
151
152
153// 对外接口:给定 tag 和参数个数 N,得到 std::tuple<arg_pair<Pos, Type>...>
154template <uint64_t Tag, int N>
155using decode_tag = typename decode_impl<Tag, 0, N>::type;
156
157
158inline std::vector<StringView> split(StringView str)
159{
160 std::vector<StringView> result;
161 const char* p = str.data();
162 const char* end = p + str.size();
163
164 while (p < end) {
165 // 跳过前导空白
166 while (p < end && std::isspace(static_cast<unsigned char>(*p))) {
167 ++p;
168 }
169 if (p >= end) break;
170
171 if (*p == '"') {
172 ++p; // 跳过左引号
173 const char* start = p;
174 while (p < end && *p != '"') {
175 ++p;
176 }
177 // 添加引号内的内容(可能为空)
178 result.emplace_back(start, static_cast<std::size_t>(p - start));
179 ++p; // 跳过右引号
180 } else {
181 const char* start = p;
182 while (p < end && !std::isspace(static_cast<unsigned char>(*p)) && *p != '"') {
183 ++p;
184 }
185 result.emplace_back(start, static_cast<std::size_t>(p - start));
186 }
187 }
188 return result;
189}
190
191inline std::string extract_command_name(const char* tmpl) {
192 std::string name;
193 while (*tmpl && *tmpl != ' ' && *tmpl != '<') {
194 name += *tmpl++;
195 }
196 return name;
197}
198
199
200// ================== 运行时字符串转类型 ==================
201
202template <typename T>
203T convert_token(StringView s);
204
205template <>
206inline bool convert_token<bool>(StringView s) {
207 bool value;
208 if (aParseBool(s, value))
209 throw std::runtime_error(std::string("invalid bool value: ") + std::string(s));
210 return value;
211}
212
213template <>
214inline int convert_token<int>(StringView s) {
215 int value;
216 if (aParseInt(s, value))
217 throw std::runtime_error(std::string("invalid int value: ") + std::string(s));
218 return value;
219}
220
221
222template <>
223inline double convert_token<double>(StringView s) {
224 double value;
225 if (aParseDouble(s, value))
226 throw std::runtime_error(std::string("invalid double value: ") + std::string(s));
227 return value;
228}
229
230template<>
231inline Object* convert_token<Object*>(StringView s) {
232 Object* value = aResolveObject(s);
233 return value;
234}
235
236template <>
237inline StringView convert_token<StringView>(StringView s) {
238 return s;
239}
240
241
242// 处理 void 返回值
243inline errc_t fill_result(CommandResult& /*result*/) {
244 return 0;
245}
246
247// 处理 int(及其他整数类型)
248template <typename T>
249typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, errc_t>::type
250fill_result(CommandResult& /*result*/, T ret) {
251 return static_cast<errc_t>(ret);
252}
253
254// 处理 std::vector<std::string>
255inline errc_t fill_result(CommandResult& result, std::vector<std::string> vec) {
256 result = std::move(vec);
257 return 0;
258}
259
260// 处理 std::string
261inline errc_t fill_result(CommandResult& result, const std::string& str) {
262 result.push_back(str);
263 return 0;
264}
265inline errc_t fill_result(CommandResult& result, std::string&& str) {
266 result.push_back(std::move(str));
267 return 0;
268}
269// 处理 const char*
270inline errc_t fill_result(CommandResult& result, const char* str) {
271 result.push_back(str);
272 return 0;
273}
274template <typename T>
275typename std::enable_if<!std::is_integral<T>::value &&
276 !std::is_same<T, std::vector<std::string>>::value &&
277 !std::is_same<T, std::string>::value &&
278 !std::is_same<T, const char*>::value, errc_t>::type
279fill_result(CommandResult& /*result*/, T ret) {
280 return static_cast<errc_t>(ret);
281}
282
283
284} // namespace detail
285
286#endif
287
290AST_NAMESPACE_END
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:95
errc_t aParseDouble(StringView str, double &value)
将字符串转换为双精度浮点数
定义 ParseFormat.cpp:260
errc_t aParseBool(StringView str, bool &value)
将字符串转换为布尔值
定义 ParseFormat.cpp:57
errc_t aParseInt(StringView str, int &value)
将字符串转换为整数
定义 ParseFormat.cpp:79
Object * aResolveObject(StringView value, Class *cls)
解析对象
定义 RTTIAPI.cpp:94
Unit s
定义 Unit.cpp:465
Unit T
特斯拉
定义 Unit.cpp:492
定义 CommandUtil.hpp:112
定义 CommandUtil.hpp:137
定义 CommandUtil.hpp:131
定义 CommandUtil.hpp:120