scope.md 1.2 KB
Newer Older
Y
Yu Yang 已提交
1
# What is a scope.
Q
qiaolongfei 已提交
2

Y
Yu Yang 已提交
3
## Overview
Q
qiaolongfei 已提交
4

Y
Yu Yang 已提交
5
预期使用场景。
Q
qiaolongfei 已提交
6

Y
Yu Yang 已提交
7 8 9
引出Scope的两个属性。
    1. Scope是Variable的Container
    2. Scope可以共享
Y
Yu Yang 已提交
10

Q
qiaolongfei 已提交
11
## Scope is a Container of Variables.
Y
Yu Yang 已提交
12

Q
qiaolongfei 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    * Scope contains Variables as it's data member.
    * Scope contains methods that are used to manage Variables, such as Create/Get/Delete.
    * every variable only belong to one certain Scope.
    * Scope should destruct all Variables within it when itself is destructed.
    * Variable can only be created by Scope.
    * Variable can only be got from Scope.

    * Scope do not contains Operators and have no information to run them.

```cpp
class Scope {
 public:
  Variable* CreateVariable(const std::string& name);
  const Variable* GetVariable(const std::string& name) const;
  bool DeleteVariable(const std::string& name);

 private:
    std::unordered_map<std::string, std::shared_ptr<Vairable>> variable_map_;
};
```
Y
Yu Yang 已提交
33

Y
Yu Yang 已提交
34
## Scope 可以被继承或者叫共享
Q
qiaolongfei 已提交
35

Y
Yu Yang 已提交
36 37 38
解释下Scope如何被共享,如何查找Variable的算法。
       * Scope永远从本地寻找Variable,找不到会从他的父亲Scope寻找Variable
    * 嵌套深度不做要求。
Y
Yu Yang 已提交
39

Y
Yu Yang 已提交
40
# 接口实现
Y
Yu Yang 已提交
41

42
# 各个接口是啥意思,为啥这么设计