From 1678ad7b3067a8c72ac504fd8cb00e83766cbba2 Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Wed, 28 Jun 2017 16:33:43 +0800 Subject: [PATCH] add Create for scope --- paddle/framework/scope.h | 8 +++++++- paddle/framework/scope_test.cc | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/paddle/framework/scope.h b/paddle/framework/scope.h index 2f8d6dbd976..bb22c4b834f 100644 --- a/paddle/framework/scope.h +++ b/paddle/framework/scope.h @@ -31,10 +31,16 @@ namespace framework { * scope. */ class Scope { - public: + 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)); + } + /// Create Variable in this Scope. Failed if Variable already been /// created. Variable* CreateVariable(const std::string& name) { diff --git a/paddle/framework/scope_test.cc b/paddle/framework/scope_test.cc index 34ee21e1aaa..d73391d9770 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; - Scope* scope = new Scope(); + auto scope = Scope::Create(); Variable* var0 = scope->CreateVariable(""); EXPECT_NE(var0, nullptr); @@ -42,10 +42,10 @@ TEST(Scope, Parent) { using paddle::framework::Scope; using paddle::framework::Variable; - const auto parent_scope_ptr = std::shared_ptr(new Scope()); - Scope* scope = new Scope(parent_scope_ptr); + auto parent_scope = Scope::Create(); + auto scope = Scope::Create(parent_scope); - Variable* var0 = parent_scope_ptr->CreateVariable("a"); + Variable* var0 = parent_scope->CreateVariable("a"); EXPECT_NE(var0, nullptr); Variable* var1 = scope->GetVariable("a"); -- GitLab