提交 5e0ac86f 编写于 作者: T Travis CI

Deploy to GitHub Pages: db3fd344

上级 d9a953e0
...@@ -37,8 +37,8 @@ Scope is an association of a name to variable. All variables belong to `Scope`. ...@@ -37,8 +37,8 @@ Scope is an association of a name to variable. All variables belong to `Scope`.
```cpp ```cpp
class Scope { class Scope {
public: public:
Variable* CreateVariable(const std::string& name); Variable* NewVar(const std::string& name);
const Variable* GetVariable(const std::string& name) const; const Variable* FindVar(const std::string& name) const;
private: private:
std::unordered_map<std::string, std::unique_ptr<Variable>> vars_; std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
...@@ -58,12 +58,12 @@ class Scope { ...@@ -58,12 +58,12 @@ class Scope {
public: public:
Scope(const std::shared_ptr<Scope>& scope): parent_(scope) {} Scope(const std::shared_ptr<Scope>& scope): parent_(scope) {}
Variable* GetVariable(const std::string& name) const { Variable* FindVar(const std::string& name) const {
auto it = vars_.find(name); auto it = vars_.find(name);
if (it != vars_.end()) { if (it != vars_.end()) {
return it->second.get(); return it->second.get();
} else if (parent_ != nullptr) { } else if (parent_ != nullptr) {
return parent_->GetVariable(name); return parent_->FindVar(name);
} else { } else {
return nullptr; return nullptr;
} }
...@@ -95,10 +95,10 @@ class Scope { ...@@ -95,10 +95,10 @@ class Scope {
static std::shared_ptr<Scope> Create(const std::shared_ptr<Scope>& parent = nullptr); static std::shared_ptr<Scope> Create(const std::shared_ptr<Scope>& parent = nullptr);
// return nullptr if not found. // return nullptr if not found.
Variable* GetVariable(const std::string& name) const; Variable* FindVar(const std::string& name) const;
// return if already contains same name variable. // return if already contains same name variable.
Variable* CreateVariable(const std::string& name); Variable* NewVar(const std::string& name);
private: private:
std::shared_ptr<Scope> parent_; std::shared_ptr<Scope> parent_;
...@@ -107,11 +107,11 @@ class Scope { ...@@ -107,11 +107,11 @@ class Scope {
``` ```
## Only scope can create a variable ## Only scope can create a variable
To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `CreateVariable` can construct `Variable`. To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `NewVar` can construct `Variable`.
## When scope destroyed, all variables inside this scope should be destroyed together ## When scope destroyed, all variables inside this scope should be destroyed together
The scope hold unique pointers for all variables. User can `GetVariable` from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together. The scope hold unique pointers for all variables. User can `FindVar` from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.
## Sharing a parent scope ## Sharing a parent scope
...@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar ...@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## Orthogonal interface ## Orthogonal interface
`GetVariable` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `CreateVariable` will return a `Error` when there is a name conflict locally. Combine `GetVariable` and `CreateVariable`, we can implement `CreateOrGetVariable` easily. `FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `NewVar` will return a `Error` when there is a name conflict locally. Combine `FindVar` and `NewVar`, we can implement `NewVar` easily.
...@@ -215,8 +215,8 @@ ...@@ -215,8 +215,8 @@
</ol> </ol>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Scope</span> <span class="p">{</span> <div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Scope</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span> <span class="k">public</span><span class="o">:</span>
<span class="n">Variable</span><span class="o">*</span> <span class="n">CreateVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span> <span class="n">Variable</span><span class="o">*</span> <span class="n">NewVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span>
<span class="k">const</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span> <span class="k">const</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
<span class="k">private</span><span class="o">:</span> <span class="k">private</span><span class="o">:</span>
<span class="n">std</span><span class="o">::</span><span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Variable</span><span class="o">&gt;&gt;</span> <span class="n">vars_</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Variable</span><span class="o">&gt;&gt;</span> <span class="n">vars_</span><span class="p">;</span>
...@@ -235,12 +235,12 @@ ...@@ -235,12 +235,12 @@
<span class="k">public</span><span class="o">:</span> <span class="k">public</span><span class="o">:</span>
<span class="n">Scope</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">scope</span><span class="p">)</span><span class="o">:</span> <span class="n">parent_</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> <span class="p">{}</span> <span class="n">Scope</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">scope</span><span class="p">)</span><span class="o">:</span> <span class="n">parent_</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> <span class="p">{}</span>
<span class="n">Variable</span><span class="o">*</span> <span class="n">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="n">Variable</span><span class="o">*</span> <span class="n">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span>
<span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">name</span><span class="p">);</span> <span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">second</span><span class="p">.</span><span class="n">get</span><span class="p">();</span> <span class="k">return</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">second</span><span class="p">.</span><span class="n">get</span><span class="p">();</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">parent_</span> <span class="o">!=</span> <span class="k">nullptr</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">parent_</span> <span class="o">!=</span> <span class="k">nullptr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">parent_</span><span class="o">-&gt;</span><span class="n">GetVariable</span><span class="p">(</span><span class="n">name</span><span class="p">);</span> <span class="k">return</span> <span class="n">parent_</span><span class="o">-&gt;</span><span class="n">FindVar</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">nullptr</span><span class="p">;</span> <span class="k">return</span> <span class="k">nullptr</span><span class="p">;</span>
<span class="p">}</span> <span class="p">}</span>
...@@ -271,10 +271,10 @@ ...@@ -271,10 +271,10 @@
<span class="k">static</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">Create</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">parent</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">);</span> <span class="k">static</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">Create</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">parent</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">);</span>
<span class="c1">// return nullptr if not found.</span> <span class="c1">// return nullptr if not found.</span>
<span class="n">Variable</span><span class="o">*</span> <span class="nf">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
<span class="c1">// return if already contains same name variable.</span> <span class="c1">// return if already contains same name variable.</span>
<span class="n">Variable</span><span class="o">*</span> <span class="nf">CreateVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">NewVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span> <span class="k">private</span><span class="o">:</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">parent_</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">parent_</span><span class="p">;</span>
...@@ -284,11 +284,11 @@ ...@@ -284,11 +284,11 @@
</div> </div>
<div class="section" id="only-scope-can-create-a-variable"> <div class="section" id="only-scope-can-create-a-variable">
<span id="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<a class="headerlink" href="#only-scope-can-create-a-variable" title="Permalink to this headline"></a></h2> <span id="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<a class="headerlink" href="#only-scope-can-create-a-variable" title="Permalink to this headline"></a></h2>
<p>To ensure <code class="docutils literal"><span class="pre">only</span> <span class="pre">scope</span> <span class="pre">can</span> <span class="pre">create</span> <span class="pre">a</span> <span class="pre">variable</span></code>, we should mark <code class="docutils literal"><span class="pre">Variable</span></code>&#8216;s constructor as a private member function, and Scope is a friend class of Variable. And then only <code class="docutils literal"><span class="pre">CreateVariable</span></code> can construct <code class="docutils literal"><span class="pre">Variable</span></code>.</p> <p>To ensure <code class="docutils literal"><span class="pre">only</span> <span class="pre">scope</span> <span class="pre">can</span> <span class="pre">create</span> <span class="pre">a</span> <span class="pre">variable</span></code>, we should mark <code class="docutils literal"><span class="pre">Variable</span></code>&#8216;s constructor as a private member function, and Scope is a friend class of Variable. And then only <code class="docutils literal"><span class="pre">NewVar</span></code> can construct <code class="docutils literal"><span class="pre">Variable</span></code>.</p>
</div> </div>
<div class="section" id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"> <div class="section" id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together">
<span id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<a class="headerlink" href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together" title="Permalink to this headline"></a></h2> <span id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<a class="headerlink" href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together" title="Permalink to this headline"></a></h2>
<p>The scope hold unique pointers for all variables. User can <code class="docutils literal"><span class="pre">GetVariable</span></code> from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.</p> <p>The scope hold unique pointers for all variables. User can <code class="docutils literal"><span class="pre">FindVar</span></code> from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.</p>
</div> </div>
<div class="section" id="sharing-a-parent-scope"> <div class="section" id="sharing-a-parent-scope">
<span id="sharing-a-parent-scope"></span><h2>Sharing a parent scope<a class="headerlink" href="#sharing-a-parent-scope" title="Permalink to this headline"></a></h2> <span id="sharing-a-parent-scope"></span><h2>Sharing a parent scope<a class="headerlink" href="#sharing-a-parent-scope" title="Permalink to this headline"></a></h2>
...@@ -297,7 +297,7 @@ ...@@ -297,7 +297,7 @@
</div> </div>
<div class="section" id="orthogonal-interface"> <div class="section" id="orthogonal-interface">
<span id="orthogonal-interface"></span><h2>Orthogonal interface<a class="headerlink" href="#orthogonal-interface" title="Permalink to this headline"></a></h2> <span id="orthogonal-interface"></span><h2>Orthogonal interface<a class="headerlink" href="#orthogonal-interface" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">GetVariable</span></code> will return <code class="docutils literal"><span class="pre">nullptr</span></code> when <code class="docutils literal"><span class="pre">name</span></code> is not found. It can be used as <code class="docutils literal"><span class="pre">Contains</span></code> method. <code class="docutils literal"><span class="pre">CreateVariable</span></code> will return a <code class="docutils literal"><span class="pre">Error</span></code> when there is a name conflict locally. Combine <code class="docutils literal"><span class="pre">GetVariable</span></code> and <code class="docutils literal"><span class="pre">CreateVariable</span></code>, we can implement <code class="docutils literal"><span class="pre">CreateOrGetVariable</span></code> easily.</p> <p><code class="docutils literal"><span class="pre">FindVar</span></code> will return <code class="docutils literal"><span class="pre">nullptr</span></code> when <code class="docutils literal"><span class="pre">name</span></code> is not found. It can be used as <code class="docutils literal"><span class="pre">Contains</span></code> method. <code class="docutils literal"><span class="pre">NewVar</span></code> will return a <code class="docutils literal"><span class="pre">Error</span></code> when there is a name conflict locally. Combine <code class="docutils literal"><span class="pre">FindVar</span></code> and <code class="docutils literal"><span class="pre">NewVar</span></code>, we can implement <code class="docutils literal"><span class="pre">NewVar</span></code> easily.</p>
</div> </div>
</div> </div>
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -37,8 +37,8 @@ Scope is an association of a name to variable. All variables belong to `Scope`. ...@@ -37,8 +37,8 @@ Scope is an association of a name to variable. All variables belong to `Scope`.
```cpp ```cpp
class Scope { class Scope {
public: public:
Variable* CreateVariable(const std::string& name); Variable* NewVar(const std::string& name);
const Variable* GetVariable(const std::string& name) const; const Variable* FindVar(const std::string& name) const;
private: private:
std::unordered_map<std::string, std::unique_ptr<Variable>> vars_; std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
...@@ -58,12 +58,12 @@ class Scope { ...@@ -58,12 +58,12 @@ class Scope {
public: public:
Scope(const std::shared_ptr<Scope>& scope): parent_(scope) {} Scope(const std::shared_ptr<Scope>& scope): parent_(scope) {}
Variable* GetVariable(const std::string& name) const { Variable* FindVar(const std::string& name) const {
auto it = vars_.find(name); auto it = vars_.find(name);
if (it != vars_.end()) { if (it != vars_.end()) {
return it->second.get(); return it->second.get();
} else if (parent_ != nullptr) { } else if (parent_ != nullptr) {
return parent_->GetVariable(name); return parent_->FindVar(name);
} else { } else {
return nullptr; return nullptr;
} }
...@@ -95,10 +95,10 @@ class Scope { ...@@ -95,10 +95,10 @@ class Scope {
static std::shared_ptr<Scope> Create(const std::shared_ptr<Scope>& parent = nullptr); static std::shared_ptr<Scope> Create(const std::shared_ptr<Scope>& parent = nullptr);
// return nullptr if not found. // return nullptr if not found.
Variable* GetVariable(const std::string& name) const; Variable* FindVar(const std::string& name) const;
// return if already contains same name variable. // return if already contains same name variable.
Variable* CreateVariable(const std::string& name); Variable* NewVar(const std::string& name);
private: private:
std::shared_ptr<Scope> parent_; std::shared_ptr<Scope> parent_;
...@@ -107,11 +107,11 @@ class Scope { ...@@ -107,11 +107,11 @@ class Scope {
``` ```
## Only scope can create a variable ## Only scope can create a variable
To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `CreateVariable` can construct `Variable`. To ensure `only scope can create a variable`, we should mark `Variable`'s constructor as a private member function, and Scope is a friend class of Variable. And then only `NewVar` can construct `Variable`.
## When scope destroyed, all variables inside this scope should be destroyed together ## When scope destroyed, all variables inside this scope should be destroyed together
The scope hold unique pointers for all variables. User can `GetVariable` from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together. The scope hold unique pointers for all variables. User can `FindVar` from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.
## Sharing a parent scope ## Sharing a parent scope
...@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar ...@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## Orthogonal interface ## Orthogonal interface
`GetVariable` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `CreateVariable` will return a `Error` when there is a name conflict locally. Combine `GetVariable` and `CreateVariable`, we can implement `CreateOrGetVariable` easily. `FindVar` will return `nullptr` when `name` is not found. It can be used as `Contains` method. `NewVar` will return a `Error` when there is a name conflict locally. Combine `FindVar` and `NewVar`, we can implement `NewVar` easily.
...@@ -220,8 +220,8 @@ ...@@ -220,8 +220,8 @@
</ol> </ol>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Scope</span> <span class="p">{</span> <div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Scope</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span> <span class="k">public</span><span class="o">:</span>
<span class="n">Variable</span><span class="o">*</span> <span class="n">CreateVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span> <span class="n">Variable</span><span class="o">*</span> <span class="n">NewVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span>
<span class="k">const</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span> <span class="k">const</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
<span class="k">private</span><span class="o">:</span> <span class="k">private</span><span class="o">:</span>
<span class="n">std</span><span class="o">::</span><span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Variable</span><span class="o">&gt;&gt;</span> <span class="n">vars_</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Variable</span><span class="o">&gt;&gt;</span> <span class="n">vars_</span><span class="p">;</span>
...@@ -240,12 +240,12 @@ ...@@ -240,12 +240,12 @@
<span class="k">public</span><span class="o">:</span> <span class="k">public</span><span class="o">:</span>
<span class="n">Scope</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">scope</span><span class="p">)</span><span class="o">:</span> <span class="n">parent_</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> <span class="p">{}</span> <span class="n">Scope</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">scope</span><span class="p">)</span><span class="o">:</span> <span class="n">parent_</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span> <span class="p">{}</span>
<span class="n">Variable</span><span class="o">*</span> <span class="n">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="n">Variable</span><span class="o">*</span> <span class="n">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span>
<span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">name</span><span class="p">);</span> <span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">vars_</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">second</span><span class="p">.</span><span class="n">get</span><span class="p">();</span> <span class="k">return</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">second</span><span class="p">.</span><span class="n">get</span><span class="p">();</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">parent_</span> <span class="o">!=</span> <span class="k">nullptr</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">parent_</span> <span class="o">!=</span> <span class="k">nullptr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">parent_</span><span class="o">-&gt;</span><span class="n">GetVariable</span><span class="p">(</span><span class="n">name</span><span class="p">);</span> <span class="k">return</span> <span class="n">parent_</span><span class="o">-&gt;</span><span class="n">FindVar</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">nullptr</span><span class="p">;</span> <span class="k">return</span> <span class="k">nullptr</span><span class="p">;</span>
<span class="p">}</span> <span class="p">}</span>
...@@ -276,10 +276,10 @@ ...@@ -276,10 +276,10 @@
<span class="k">static</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">Create</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">parent</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">);</span> <span class="k">static</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">Create</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;&amp;</span> <span class="n">parent</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">);</span>
<span class="c1">// return nullptr if not found.</span> <span class="c1">// return nullptr if not found.</span>
<span class="n">Variable</span><span class="o">*</span> <span class="nf">GetVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">FindVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
<span class="c1">// return if already contains same name variable.</span> <span class="c1">// return if already contains same name variable.</span>
<span class="n">Variable</span><span class="o">*</span> <span class="nf">CreateVariable</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span> <span class="n">Variable</span><span class="o">*</span> <span class="nf">NewVar</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">name</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span> <span class="k">private</span><span class="o">:</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">parent_</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Scope</span><span class="o">&gt;</span> <span class="n">parent_</span><span class="p">;</span>
...@@ -289,11 +289,11 @@ ...@@ -289,11 +289,11 @@
</div> </div>
<div class="section" id="only-scope-can-create-a-variable"> <div class="section" id="only-scope-can-create-a-variable">
<span id="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<a class="headerlink" href="#only-scope-can-create-a-variable" title="永久链接至标题"></a></h2> <span id="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<a class="headerlink" href="#only-scope-can-create-a-variable" title="永久链接至标题"></a></h2>
<p>To ensure <code class="docutils literal"><span class="pre">only</span> <span class="pre">scope</span> <span class="pre">can</span> <span class="pre">create</span> <span class="pre">a</span> <span class="pre">variable</span></code>, we should mark <code class="docutils literal"><span class="pre">Variable</span></code>&#8216;s constructor as a private member function, and Scope is a friend class of Variable. And then only <code class="docutils literal"><span class="pre">CreateVariable</span></code> can construct <code class="docutils literal"><span class="pre">Variable</span></code>.</p> <p>To ensure <code class="docutils literal"><span class="pre">only</span> <span class="pre">scope</span> <span class="pre">can</span> <span class="pre">create</span> <span class="pre">a</span> <span class="pre">variable</span></code>, we should mark <code class="docutils literal"><span class="pre">Variable</span></code>&#8216;s constructor as a private member function, and Scope is a friend class of Variable. And then only <code class="docutils literal"><span class="pre">NewVar</span></code> can construct <code class="docutils literal"><span class="pre">Variable</span></code>.</p>
</div> </div>
<div class="section" id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"> <div class="section" id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together">
<span id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<a class="headerlink" href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together" title="永久链接至标题"></a></h2> <span id="when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together"></span><h2>When scope destroyed, all variables inside this scope should be destroyed together<a class="headerlink" href="#when-scope-destroyed-all-variables-inside-this-scope-should-be-destroyed-together" title="永久链接至标题"></a></h2>
<p>The scope hold unique pointers for all variables. User can <code class="docutils literal"><span class="pre">GetVariable</span></code> from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.</p> <p>The scope hold unique pointers for all variables. User can <code class="docutils literal"><span class="pre">FindVar</span></code> from scope, but he should not hold this pointer as a member variable. Because when scope is destroyed, all variables inside this scope will be destroyed together.</p>
</div> </div>
<div class="section" id="sharing-a-parent-scope"> <div class="section" id="sharing-a-parent-scope">
<span id="sharing-a-parent-scope"></span><h2>Sharing a parent scope<a class="headerlink" href="#sharing-a-parent-scope" title="永久链接至标题"></a></h2> <span id="sharing-a-parent-scope"></span><h2>Sharing a parent scope<a class="headerlink" href="#sharing-a-parent-scope" title="永久链接至标题"></a></h2>
...@@ -302,7 +302,7 @@ ...@@ -302,7 +302,7 @@
</div> </div>
<div class="section" id="orthogonal-interface"> <div class="section" id="orthogonal-interface">
<span id="orthogonal-interface"></span><h2>Orthogonal interface<a class="headerlink" href="#orthogonal-interface" title="永久链接至标题"></a></h2> <span id="orthogonal-interface"></span><h2>Orthogonal interface<a class="headerlink" href="#orthogonal-interface" title="永久链接至标题"></a></h2>
<p><code class="docutils literal"><span class="pre">GetVariable</span></code> will return <code class="docutils literal"><span class="pre">nullptr</span></code> when <code class="docutils literal"><span class="pre">name</span></code> is not found. It can be used as <code class="docutils literal"><span class="pre">Contains</span></code> method. <code class="docutils literal"><span class="pre">CreateVariable</span></code> will return a <code class="docutils literal"><span class="pre">Error</span></code> when there is a name conflict locally. Combine <code class="docutils literal"><span class="pre">GetVariable</span></code> and <code class="docutils literal"><span class="pre">CreateVariable</span></code>, we can implement <code class="docutils literal"><span class="pre">CreateOrGetVariable</span></code> easily.</p> <p><code class="docutils literal"><span class="pre">FindVar</span></code> will return <code class="docutils literal"><span class="pre">nullptr</span></code> when <code class="docutils literal"><span class="pre">name</span></code> is not found. It can be used as <code class="docutils literal"><span class="pre">Contains</span></code> method. <code class="docutils literal"><span class="pre">NewVar</span></code> will return a <code class="docutils literal"><span class="pre">Error</span></code> when there is a name conflict locally. Combine <code class="docutils literal"><span class="pre">FindVar</span></code> and <code class="docutils literal"><span class="pre">NewVar</span></code>, we can implement <code class="docutils literal"><span class="pre">NewVar</span></code> easily.</p>
</div> </div>
</div> </div>
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册