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
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
...
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## 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.
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="Permalink to this headline">¶</a></h2>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<spanid="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<aclass="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 <codeclass="docutils literal"><spanclass="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 <codeclass="docutils literal"><spanclass="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>
<divclass="section"id="sharing-a-parent-scope">
<spanid="sharing-a-parent-scope"></span><h2>Sharing a parent scope<aclass="headerlink"href="#sharing-a-parent-scope"title="Permalink to this headline">¶</a></h2>
...
...
@@ -297,7 +297,7 @@
</div>
<divclass="section"id="orthogonal-interface">
<spanid="orthogonal-interface"></span><h2>Orthogonal interface<aclass="headerlink"href="#orthogonal-interface"title="Permalink to this headline">¶</a></h2>
<p><codeclass="docutils literal"><spanclass="pre">GetVariable</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code> will return a <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">GetVariable</span></code> and <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">CreateOrGetVariable</span></code> easily.</p>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> will return a <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">NewVar</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> easily.</p>
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
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
...
...
@@ -121,4 +121,4 @@ Also, as the parent scope is a `shared_ptr`, we can only `Create()` a scope shar
## 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.
<spanid="only-scope-can-create-a-variable"></span><h2>Only scope can create a variable<aclass="headerlink"href="#only-scope-can-create-a-variable"title="永久链接至标题">¶</a></h2>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<p>To ensure <codeclass="docutils literal"><spanclass="pre">only</span><spanclass="pre">scope</span><spanclass="pre">can</span><spanclass="pre">create</span><spanclass="pre">a</span><spanclass="pre">variable</span></code>, we should mark <codeclass="docutils literal"><spanclass="pre">Variable</span></code>‘s constructor as a private member function, and Scope is a friend class of Variable. And then only <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> can construct <codeclass="docutils literal"><spanclass="pre">Variable</span></code>.</p>
<spanid="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<aclass="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 <codeclass="docutils literal"><spanclass="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 <codeclass="docutils literal"><spanclass="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>
<divclass="section"id="sharing-a-parent-scope">
<spanid="sharing-a-parent-scope"></span><h2>Sharing a parent scope<aclass="headerlink"href="#sharing-a-parent-scope"title="永久链接至标题">¶</a></h2>
<p><codeclass="docutils literal"><spanclass="pre">GetVariable</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code> will return a <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">GetVariable</span></code> and <codeclass="docutils literal"><spanclass="pre">CreateVariable</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">CreateOrGetVariable</span></code> easily.</p>
<p><codeclass="docutils literal"><spanclass="pre">FindVar</span></code> will return <codeclass="docutils literal"><spanclass="pre">nullptr</span></code> when <codeclass="docutils literal"><spanclass="pre">name</span></code> is not found. It can be used as <codeclass="docutils literal"><spanclass="pre">Contains</span></code> method. <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> will return a <codeclass="docutils literal"><spanclass="pre">Error</span></code> when there is a name conflict locally. Combine <codeclass="docutils literal"><spanclass="pre">FindVar</span></code> and <codeclass="docutils literal"><spanclass="pre">NewVar</span></code>, we can implement <codeclass="docutils literal"><spanclass="pre">NewVar</span></code> easily.</p>