提交 9af54c9e 编写于 作者: Q qiaolongfei

do not use default argument in Scope

上级 cfdfa89b
......@@ -24,24 +24,31 @@ namespace paddle {
namespace framework {
/**
* @brief Scope that manage all variables.
*
* Scope is an association of a name to Variable. All variables belong to
* Scope. You need to specify a scope to run a Net, i.e., `net.Run(&scope)`.
* One net can run in different scopes and update different variable in the
* scope.
*/
class Scope {
private:
explicit Scope(const std::shared_ptr<Scope>& parent = nullptr)
: parent_(parent) {}
public:
static std::shared_ptr<Scope> Create(
const std::shared_ptr<Scope>& parent = nullptr) {
return std::make_shared<Scope>(Scope(parent));
}
/**
* @brief Initialize s Scope without parent.
*/
Scope() {}
/**
* @brief Initialize a Scope with parent.
*/
explicit Scope(const std::shared_ptr<Scope>& parent) : parent_(parent) {}
/// Create Variable in this Scope. Failed if Variable already been
/// created.
/**
* @brief Create Variable
*
* Create Variable in this Scope. Return the exist one if Variable already
* been created.
*/
Variable* CreateVariable(const std::string& name) {
auto var = GetVariable(name);
if (var) {
......@@ -52,8 +59,12 @@ class Scope {
}
}
/// Get Variable from this Scope, this function will recursive find Variable
/// from it's parent scope. Return nullptr if not found.
/**
* @brief Get Variable.
*
* Get Variable from this Scope, this function will recursive find Variable
* from it's parent scope. Return nullptr if not found.
*/
Variable* GetVariable(const std::string& name) const {
auto it = vars_.find(name);
if (it != vars_.end()) {
......@@ -65,7 +76,11 @@ class Scope {
}
}
/// Find if there is a Variable in this scope and it's parent scope
/**
* @brief If this scope has a Var named name.
*
* Find if there is a Variable in this scope and it's parent scope
*/
bool HasVariable(const std::string& name) const {
return (vars_.find(name) != vars_.end() ||
(parent_ && parent_->HasVariable(name)));
......
......@@ -19,7 +19,7 @@ TEST(Scope, Create) {
using paddle::framework::Scope;
using paddle::framework::Variable;
auto scope = Scope::Create();
auto scope = std::make_shared<Scope>();
Variable* var0 = scope->CreateVariable("");
EXPECT_NE(var0, nullptr);
......@@ -46,8 +46,8 @@ TEST(Scope, Parent) {
using paddle::framework::Scope;
using paddle::framework::Variable;
auto parent_scope = Scope::Create();
auto scope = Scope::Create(parent_scope);
auto parent_scope = std::make_shared<Scope>();
auto scope = std::make_shared<Scope>(parent_scope);
Variable* var0 = parent_scope->CreateVariable("a");
EXPECT_NE(var0, nullptr);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册