diff --git a/paddle/framework/scope.h b/paddle/framework/scope.h index 88a13145ca9ce622894b36fdf9638817f523dfb8..a4470f726fb0d59a82db29b3239c111ea1569c55 100644 --- a/paddle/framework/scope.h +++ b/paddle/framework/scope.h @@ -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& parent = nullptr) - : parent_(parent) {} - public: - static std::shared_ptr Create( - const std::shared_ptr& parent = nullptr) { - return std::make_shared(Scope(parent)); - } + /** + * @brief Initialize s Scope without parent. + */ + Scope() {} + + /** + * @brief Initialize a Scope with parent. + */ + explicit Scope(const std::shared_ptr& 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))); diff --git a/paddle/framework/scope_test.cc b/paddle/framework/scope_test.cc index ec6236ec62197f20b77cbdcfaad6be35ef42835b..df1afb200ce9e75c5b1e40f2da56667487ae3576 100644 --- a/paddle/framework/scope_test.cc +++ b/paddle/framework/scope_test.cc @@ -19,7 +19,7 @@ TEST(Scope, Create) { using paddle::framework::Scope; using paddle::framework::Variable; - auto scope = Scope::Create(); + auto scope = std::make_shared(); 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(); + auto scope = std::make_shared(parent_scope); Variable* var0 = parent_scope->CreateVariable("a"); EXPECT_NE(var0, nullptr);