表格线识别通用库文档
载入中...
搜索中...
未找到
scope.hpp
浏览该文件的文档.
1/*
2 * @Description: 作用域 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2023-12-06
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-04
8 */
9
10#ifndef COMMON_SCOPE_HPP
11#define COMMON_SCOPE_HPP
12
13#include <iostream>
14#include <stack>
15#include <string>
16#include <unordered_map>
17
18#include "common/enum.h"
19#include "common/macro.h"
20#include "common/type.h"
21
22namespace cm {
23
65template <typename T>
66class Scope {
67 private:
68 std::unordered_map<std::string, T> scope_table; // 作用域表
69 std::string activated_scope_name; // 当前被激活的作用域名称
70 std::string initial_scope_name; // 最初的作用域名称(构造函数构造时的初始化作用域)
71
72 std::stack<std::string> scope_switch_record; // 作用域切换记录栈
73
74 public:
75 Scope(const std::string& scope_name = "__INITIAL__");
78
79 template <typename U>
80 friend std::ostream& operator<<(std::ostream& out, const Scope<U>& scope);
81
82 T& Value();
83 Scope& Set(const std::string& scope_name, T value);
84 Scope& Switch(const std::string& scope_name);
87 bool Has(const std::string& scope_name) const;
88 bool IsInitial() const;
89};
90
100template <typename T>
101inline Scope<T>::Scope(const std::string& initial_scope_name) : activated_scope_name(initial_scope_name), initial_scope_name(initial_scope_name) {
102 scope_table[initial_scope_name] = T(); // 更新作用域表,默认激活初始作用域
103 scope_switch_record.emplace(initial_scope_name); // 最初的作用域入栈
104}
105
114template <typename T>
115inline Scope<T>::Scope(const Scope<T>& scope) : scope_table(scope.scope_table), activated_scope_name(scope.activated_scope_name), initial_scope_name(scope.initial_scope_name), scope_switch_record(scope.scope_switch_record) {}
116
124template <typename T>
126
139template <typename U>
140inline std::ostream& operator<<(std::ostream& out, const Scope<U>& scope) {
141 out << "{ activated scope: \"" << scope.activated_scope_name << "\" }";
142
143 return out;
144}
145
154template <typename T>
156 return scope_table[activated_scope_name]; // 返回引用值,可修改
157}
158
169template <typename T>
170inline Scope<T>& Scope<T>::Set(const std::string& scope_name, T value) {
171 scope_table[scope_name] = value; // 如果存在作用域,则修改,如果不存在,则添加
172
173 return *this;
174}
175
186template <typename T>
187inline Scope<T>& Scope<T>::Switch(const std::string& scope_name) {
188 if (!scope_table.count(scope_name)) // 如果不存在该作用域
189 scope_table[scope_name] = T(); // 作用域将被创建,值为T的默认构造
190
191 activated_scope_name = scope_name; // 激活被切换的作用域
192 scope_switch_record.emplace(activated_scope_name); // 被激活的作用域入栈
193
194 return *this;
195}
196
206template <typename T>
208 if (scope_switch_record.size() <= 1) // 保证栈底始终存在一个元素,即最初的作用域始终不出栈
209 return *this;
210
211 scope_switch_record.pop(); // 作用域切换记录栈 栈顶元素出栈
212 activated_scope_name = scope_switch_record.top(); // 激活出栈后的栈顶作用域
213
214 return *this;
215}
216
226template <typename T>
228 activated_scope_name = initial_scope_name; // 激活最初作用域
229
230 while (scope_switch_record.size() > 1) // 清空作用域切换记录栈,保证栈底始终存在一个元素,即最初的作用域始终在栈底
231 scope_switch_record.pop();
232
233 return *this;
234}
235
249template <typename T>
250inline bool Scope<T>::Has(const std::string& scope_name) const {
251 return scope_table.count(scope_name);
252}
253
265template <typename T>
266inline bool Scope<T>::IsInitial() const {
267 return activated_scope_name == initial_scope_name;
268}
269
270} // namespace cm
271
272#endif
点类
Definition point.hpp:52
Point()
点类的默认构造函数
Definition point.hpp:125
作用域类
Definition scope.hpp:66
Scope(const std::string &scope_name="__INITIAL__")
作用域类带参构造函数
Definition scope.hpp:101
Scope & Finish()
结束作用域
Definition scope.hpp:207
~Scope()
作用域类析构函数
Definition scope.hpp:125
T & Value()
获取当前作用域的值
Definition scope.hpp:155
friend std::ostream & operator<<(std::ostream &out, const Scope< U > &scope)
作用域类重载输出流运算符
Definition scope.hpp:140
bool Has(const std::string &scope_name) const
判断作用域是否存在
Definition scope.hpp:250
Scope & Switch(const std::string &scope_name)
切换作用域
Definition scope.hpp:187
bool IsInitial() const
判断是否为初始作用域
Definition scope.hpp:266
Scope(const Scope< T > &scope)
作用域类拷贝构造函数
Definition scope.hpp:115
Scope & Set(const std::string &scope_name, T value)
设置作用域的值
Definition scope.hpp:170
Scope & Exit()
退出作用域
Definition scope.hpp:227
std::ostream & operator<<(std::ostream &out, const Interval &interval)
区间类重载输出流运算符
Definition interval.hpp:328