提交 14253875 编写于 作者: Y Yu Yang

Using unique_ptr instead of raw ptr

Fit google C++ style
上级 a0d77533
......@@ -112,8 +112,8 @@ class OperatorBase {
const AttributeMap& Attrs() const { return attrs_; }
// Return a new operator instance, which is as same as this.
// NOTE: It is caller's responsibility to delete that operator instance.
virtual OperatorBase* Clone() const = 0;
// Use unique_ptr to prevent caller forget to delete this pointer.
virtual std::unique_ptr<OperatorBase> Clone() const = 0;
public:
std::string type_;
......@@ -132,8 +132,10 @@ class OperatorBase {
// Macro for define a clone method.
// If you are writing an kernel operator, `Clone` will be defined when you
// register it.
#define DEFINE_OP_CLONE_METHOD(CLS) \
OperatorBase* Clone() const final { return new CLS(*this); }
#define DEFINE_OP_CLONE_METHOD(CLS) \
std::unique_ptr<OperatorBase> Clone() const final { \
return std::unique_ptr<OperatorBase>(new CLS(*this)); \
}
// Macro for define a default constructor for Operator.
// You can also use
......
......@@ -257,7 +257,6 @@ class OperatorClone : public paddle::framework::OperatorBase {
TEST(Operator, Clone) {
OperatorClone a("ABC", {}, {}, {});
auto* b = a.Clone();
auto b = a.Clone();
ASSERT_EQ(a.Type(), b->Type());
delete b;
}
\ No newline at end of file
......@@ -85,13 +85,13 @@ NetOp::NetOp(const std::string& type,
const framework::OperatorBase::VarNameMap& inputs,
const framework::OperatorBase::VarNameMap& outputs,
const framework::AttributeMap& attrs)
: OperatorBase(type, inputs, outputs, attrs) {}
: framework::OperatorBase(type, inputs, outputs, attrs) {}
framework::OperatorBase* NetOp::Clone() const {
std::unique_ptr<framework::OperatorBase> NetOp::Clone() const {
PADDLE_ENFORCE(
add_op_done_,
"Must clone a sealed NetOp, invoke Net::CompleteAddOp before clone");
return new NetOp(*this);
return std::unique_ptr<OperatorBase>(new NetOp(*this));
}
} // namespace operators
......
......@@ -109,7 +109,8 @@ class NetOp : public framework::OperatorBase {
bool IsNetOp() const override;
std::vector<std::string> OutputVars(bool has_intermediate) const override;
framework::OperatorBase* Clone() const override;
std::unique_ptr<framework::OperatorBase> Clone() const override;
std::vector<std::shared_ptr<OperatorBase>> ops_;
......
......@@ -84,14 +84,13 @@ TEST(NetOp, Clone) {
net.AddOp(std::shared_ptr<EmptyOp>(new EmptyOp{"empty", {}, {}, {}}));
net.AddOp(std::shared_ptr<EmptyOp>(new EmptyOp{"empty2", {}, {}, {}}));
net.CompleteAddOp(true);
auto* new_net_op = net.Clone();
auto new_net_op = net.Clone();
ASSERT_NE(new_net_op, nullptr);
ASSERT_TRUE(new_net_op->IsNetOp());
auto* new_net = static_cast<NetOp*>(new_net_op);
auto* new_net = static_cast<NetOp*>(new_net_op.get());
ASSERT_EQ(2, new_net->ops_.size());
ASSERT_EQ(new_net->ops_[0]->Type(), "empty");
ASSERT_EQ(new_net->ops_[1]->Type(), "empty2");
delete new_net;
}
} // namespace operators
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册