add Create for scope

gangliao-patch-1
qiaolongfei 8 years ago
parent b8ffa8b9e9
commit 1678ad7b30

@ -31,10 +31,16 @@ namespace framework {
* scope. * scope.
*/ */
class Scope { class Scope {
public: private:
explicit Scope(const std::shared_ptr<Scope>& parent = nullptr) explicit Scope(const std::shared_ptr<Scope>& parent = nullptr)
: parent_(parent) {} : parent_(parent) {}
public:
static std::shared_ptr<Scope> Create(
const std::shared_ptr<Scope>& parent = nullptr) {
return std::make_shared<Scope>(Scope(parent));
}
/// Create Variable in this Scope. Failed if Variable already been /// Create Variable in this Scope. Failed if Variable already been
/// created. /// created.
Variable* CreateVariable(const std::string& name) { Variable* CreateVariable(const std::string& name) {

@ -19,7 +19,7 @@ TEST(Scope, Create) {
using paddle::framework::Scope; using paddle::framework::Scope;
using paddle::framework::Variable; using paddle::framework::Variable;
Scope* scope = new Scope(); auto scope = Scope::Create();
Variable* var0 = scope->CreateVariable(""); Variable* var0 = scope->CreateVariable("");
EXPECT_NE(var0, nullptr); EXPECT_NE(var0, nullptr);
@ -42,10 +42,10 @@ TEST(Scope, Parent) {
using paddle::framework::Scope; using paddle::framework::Scope;
using paddle::framework::Variable; using paddle::framework::Variable;
const auto parent_scope_ptr = std::shared_ptr<Scope>(new Scope()); auto parent_scope = Scope::Create();
Scope* scope = new Scope(parent_scope_ptr); auto scope = Scope::Create(parent_scope);
Variable* var0 = parent_scope_ptr->CreateVariable("a"); Variable* var0 = parent_scope->CreateVariable("a");
EXPECT_NE(var0, nullptr); EXPECT_NE(var0, nullptr);
Variable* var1 = scope->GetVariable("a"); Variable* var1 = scope->GetVariable("a");

Loading…
Cancel
Save