表格线识别通用库文档
载入中...
搜索中...
未找到
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
22
namespace
cm
{
23
65
template
<
typename
T>
66
class
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__"
);
76
Scope
(
const
Scope<T>
&
scope
);
77
~Scope
();
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
);
85
Scope
&
Finish
();
86
Scope
&
Exit
();
87
bool
Has
(
const
std::string&
scope_name
)
const
;
88
bool
IsInitial
()
const
;
89
};
90
100
template
<
typename
T>
101
inline
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
114
template
<
typename
T>
115
inline
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
124
template
<
typename
T>
125
inline
Scope<T>::~Scope
() {}
126
139
template
<
typename
U>
140
inline
std::ostream&
operator<<
(std::ostream&
out
,
const
Scope<U>
&
scope
) {
141
out
<<
"{ activated scope: \""
<<
scope
.activated_scope_name <<
"\" }"
;
142
143
return
out
;
144
}
145
154
template
<
typename
T>
155
inline
T
&
Scope<T>::Value
() {
156
return
scope_table[activated_scope_name];
// 返回引用值,可修改
157
}
158
169
template
<
typename
T>
170
inline
Scope<T>
&
Scope<T>::Set
(
const
std::string&
scope_name
,
T
value) {
171
scope_table[
scope_name
] = value;
// 如果存在作用域,则修改,如果不存在,则添加
172
173
return
*
this
;
174
}
175
186
template
<
typename
T>
187
inline
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
206
template
<
typename
T>
207
inline
Scope<T>
&
Scope<T>::Finish
() {
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
226
template
<
typename
T>
227
inline
Scope<T>
&
Scope<T>::Exit
() {
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
249
template
<
typename
T>
250
inline
bool
Scope<T>::Has
(
const
std::string&
scope_name
)
const
{
251
return
scope_table.count(
scope_name
);
252
}
253
265
template
<
typename
T>
266
inline
bool
Scope<T>::IsInitial
()
const
{
267
return
activated_scope_name == initial_scope_name;
268
}
269
270
}
// namespace cm
271
272
#endif
cm::Point
点类
Definition
point.hpp:52
cm::Point::Point
Point()
点类的默认构造函数
Definition
point.hpp:125
cm::Scope
作用域类
Definition
scope.hpp:66
cm::Scope::Scope
Scope(const std::string &scope_name="__INITIAL__")
作用域类带参构造函数
Definition
scope.hpp:101
cm::Scope::Finish
Scope & Finish()
结束作用域
Definition
scope.hpp:207
cm::Scope::~Scope
~Scope()
作用域类析构函数
Definition
scope.hpp:125
cm::Scope::Value
T & Value()
获取当前作用域的值
Definition
scope.hpp:155
cm::Scope::operator<<
friend std::ostream & operator<<(std::ostream &out, const Scope< U > &scope)
作用域类重载输出流运算符
Definition
scope.hpp:140
cm::Scope::Has
bool Has(const std::string &scope_name) const
判断作用域是否存在
Definition
scope.hpp:250
cm::Scope::Switch
Scope & Switch(const std::string &scope_name)
切换作用域
Definition
scope.hpp:187
cm::Scope::IsInitial
bool IsInitial() const
判断是否为初始作用域
Definition
scope.hpp:266
cm::Scope::Scope
Scope(const Scope< T > &scope)
作用域类拷贝构造函数
Definition
scope.hpp:115
cm::Scope::Set
Scope & Set(const std::string &scope_name, T value)
设置作用域的值
Definition
scope.hpp:170
cm::Scope::Exit
Scope & Exit()
退出作用域
Definition
scope.hpp:227
enum.h
macro.h
cm
Definition
disjoint_set.hpp:22
cm::operator<<
std::ostream & operator<<(std::ostream &out, const Interval &interval)
区间类重载输出流运算符
Definition
interval.hpp:328
type.h
common
include
common
base
scope.hpp
制作者
1.10.0