提交 1678ad7b 编写于 作者: Q qiaolongfei

add Create for scope

上级 b8ffa8b9
......@@ -31,10 +31,16 @@ namespace framework {
* scope.
*/
class Scope {
public:
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));
}
/// Create Variable in this Scope. Failed if Variable already been
/// created.
Variable* CreateVariable(const std::string& name) {
......
......@@ -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<Scope>(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");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册