dl
dl_script_variable.h
浏览该文件的文档.
1
12#pragma once
13
14#include <variant>
15
16#include "io/dl_log.h"
17
18namespace dl::Script
19{
20class Variable;
21using Function = std::function<int(std::vector<Variable>)>;
23{
24public:
25 enum class Type
26 {
27 NIL,
28 NUMBER,
29 STRING,
31 TABLE,
33 };
34 std::string_view to_string(Type type) const
35 {
36 switch (type)
37 {
38 case Type::NIL:
39 return "nil";
40 case Type::NUMBER:
41 return "number";
42 case Type::STRING:
43 return "string";
44 case Type::FUNCTION:
45 return "function";
46 case Type::TABLE:
47 return "table";
48 }
49 assert(0);
50 return "error";
51 }
52
54 _type{ Type::NIL },
55 _val{ 0 }
56 {}
59 {
60 _copy(b);
61 return *this;
62 }
64 {
65 _copy(b);
66 }
67 Variable(const std::string& str) :
68 _type{ Type::STRING },
69 _val{ str }
70 {}
71 Variable(std::string_view str) :
72 _type{ Type::STRING },
73 _val{ std::string{str} }
74 {}
76 _type{ Type::FUNCTION },
77 _val{ func }
78 {}
79 Variable(int tag, std::string_view str) :
80 _type{ Type::VAR_LOCAL },
81 _val{ std::string{str} }
82 {}
83
84 operator std::string_view() const
85 {
86 if (_type != Type::STRING)
87 {
88 log_err("类型不是字符串,当前为{}!", to_string(_type));
89 return {};
90 }
91 return std::get<std::string>(_val);
92 }
93 operator Function() const
94 {
95 if (_type != Type::FUNCTION)
96 {
97 log_err("类型不是函数,当前为{}!", to_string(_type));
98 return {};
99 }
100 return std::get<Function>(_val);
101 }
102
103 Type GetType() const
104 {
105 return _type;
106 }
107
108 std::string GetString() const
109 {
110 switch (_type)
111 {
113 return "nil";
115 return "12345";
118 return std::get<std::string>(_val);
120 //Function func = std::get<Function>(_val);
121 return "function_xx";
123 return "table_xx";
124 default:
125 break;
126 }
127 assert(0);
128 return "error";
129 }
130private:
131 std::variant<int, std::string, Function> _val;
132 Type _type;
133
134 void _copy(const Variable& b)
135 {
136 _type = b._type;
137 _val = b._val;
138 }
139};
140
141
142}
std::string GetString() const
Variable(int tag, std::string_view str)
Variable(std::string_view str)
std::string_view to_string(Type type) const
Variable & operator=(const Variable &b)
Variable(const Variable &b)
Variable(const std::string &str)
日志系统
#define log_err(...)
Definition: dl_log.h:243
std::function< int(std::vector< Variable >)> Function