24#include "AstUtil/RTTIAPI.hpp"
25#include "AstUtil/ValueView.hpp"
26#include "AstUtil/Span.hpp"
27#include "AstUtil/ParseFormat.hpp"
29#include <unordered_map>
49constexpr int find_closing(
const char* s,
int p) {
50 return s[p] ==
'>' ? p : find_closing(s, p + 1);
55constexpr bool is_type(
const char* s,
int p,
const char* type) {
57 while (type[i] !=
'\0') {
58 if (s[p + i] != type[i])
return false;
62 return s[p + i] ==
'>' ||
s[p + i] ==
':';
66constexpr bool is_type_impl(
const char* s,
int p,
const char* type,
int i) {
67 return type[i] ==
'\0'
70 ? ((
s[p + i] ==
'>') | (
s[p + i] ==
':'))
71 : (
s[p + i] == type[i] && is_type_impl(
s, p, type, i + 1));
75constexpr bool is_type(
const char* s,
int p,
const char* type) {
76 return is_type_impl(s, p, type, 0);
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) :
98constexpr uint64_t get_parameter_tag(
const char* s,
int p = 0) {
99 return s[p] ==
'\0' ? 0 :
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"
108 get_parameter_tag(
s, p + 1);
111template <
int Pos,
typename T>
113 static constexpr int pos = Pos;
119template <
int TypeCode>
130template <u
int64_t Tag,
int Pos>
136template <u
int64_t Tag,
int Pos,
int Count>
138 using type =
decltype(
141 typename decode_impl<Tag / num_types, Pos + 1, Count - 1>::type()
146template <u
int64_t Tag,
int Pos>
148 using type = std::tuple<>;
154template <u
int64_t Tag,
int N>
155using decode_tag =
typename decode_impl<Tag, 0, N>::type;
158inline std::vector<StringView> split(
StringView str)
160 std::vector<StringView> result;
161 const char* p = str.data();
162 const char* end = p + str.size();
166 while (p < end && std::isspace(
static_cast<unsigned char>(*p))) {
173 const char* start = p;
174 while (p < end && *p !=
'"') {
178 result.emplace_back(start,
static_cast<std::size_t
>(p - start));
181 const char* start = p;
182 while (p < end && !std::isspace(
static_cast<unsigned char>(*p)) && *p !=
'"') {
185 result.emplace_back(start,
static_cast<std::size_t
>(p - start));
191inline std::string extract_command_name(
const char* tmpl) {
193 while (*tmpl && *tmpl !=
' ' && *tmpl !=
'<') {
203T convert_token(StringView s);
206inline bool convert_token<bool>(StringView s) {
209 throw std::runtime_error(std::string(
"invalid bool value: ") + std::string(s));
214inline int convert_token<int>(StringView s) {
217 throw std::runtime_error(std::string(
"invalid int value: ") + std::string(s));
223inline double convert_token<double>(StringView s) {
226 throw std::runtime_error(std::string(
"invalid double value: ") + std::string(s));
231inline Object* convert_token<Object*>(StringView s) {
237inline StringView convert_token<StringView>(StringView s) {
243inline errc_t fill_result(CommandResult& ) {
249typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, errc_t>::type
250fill_result(CommandResult& , T ret) {
251 return static_cast<errc_t
>(ret);
255inline errc_t fill_result(CommandResult& result, std::vector<std::string> vec) {
256 result = std::move(vec);
261inline errc_t fill_result(CommandResult& result,
const std::string& str) {
262 result.push_back(str);
265inline errc_t fill_result(CommandResult& result, std::string&& str) {
266 result.push_back(std::move(str));
270inline errc_t fill_result(CommandResult& result,
const char* str) {
271 result.push_back(str);
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& , T ret) {
280 return static_cast<errc_t
>(ret);
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:95
Object * aResolveObject(StringView value, Class *cls)
解析对象
定义 RTTIAPI.cpp:94