提交 db96c0ee 编写于 作者: Y Yu Yang

Use unique_ptr instead of shared_ptr/weak_ptr.

But user can not hold this pointers.
上级 1f0056b2
...@@ -73,8 +73,6 @@ private: ...@@ -73,8 +73,6 @@ private:
friend class Scope; friend class Scope;
}; };
using VariablePtr = std::weak_ptr<Variable>;
class Scope { class Scope {
private: private:
Scope(const std::shared_ptr<Scope>& parent = nullptr); Scope(const std::shared_ptr<Scope>& parent = nullptr);
...@@ -83,14 +81,14 @@ public: ...@@ -83,14 +81,14 @@ public:
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.
VariablePtr GetVariable(const std::string& name) const; Variable* GetVariable(const std::string& name) const;
// return Error if already contains same name variable. // return Error if already contains same name variable.
Error CreateVariable(const std::string& name); Error CreateVariable(const std::string& name);
private: private:
std::shared_ptr<Scope> parent_; std::shared_ptr<Scope> parent_;
std::unordered_map<std::string, std::shared_ptr<Scope>> attrs_; std::unordered_map<std::string, std::unique_ptr<Scope>> attrs_;
}; };
``` ```
## Only scope can create a variable ## Only scope can create a variable
...@@ -99,7 +97,7 @@ To ensure `only scope can create a variable`, we should mark `Variable`'s constr ...@@ -99,7 +97,7 @@ To ensure `only scope can create a variable`, we should mark `Variable`'s constr
## 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 `VariablePtr` is a `weak_ptr`. `Net` and `Op` can only get a Variable from `Scope`, but cannot hold it. When scope is destroyed, all `VariablePtr`s belong to this Scope will be changed to `nullptr`. 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.
## Sharing a parent scope ## Sharing a parent scope
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册