Make CustomCommand safe copyable and moveable

This commit is contained in:
halx99 2020-08-19 11:42:32 +08:00
parent aa5fc9dc0f
commit 93b39601f9
2 changed files with 47 additions and 1 deletions

View File

@ -41,6 +41,44 @@ CustomCommand::~CustomCommand()
CC_SAFE_RELEASE(_indexBuffer); CC_SAFE_RELEASE(_indexBuffer);
} }
CustomCommand::CustomCommand(const CustomCommand& rhs)
{
this->copyAssign(rhs);
}
CustomCommand::CustomCommand(CustomCommand&& rhs)
{
this->moveAssign(std::move(rhs));
}
CustomCommand& CustomCommand::operator=(const CustomCommand& rhs)
{
this->copyAssign(rhs);
return *this;
}
CustomCommand& CustomCommand::operator=(CustomCommand&& rhs)
{
this->moveAssign(std::move(rhs));
return *this;
}
void CustomCommand::copyAssign(const CustomCommand& rhs)
{
if (this != &rhs) {
memcpy(this, &rhs, sizeof(rhs));
CC_SAFE_RETAIN(_vertexBuffer);
CC_SAFE_RETAIN(_indexBuffer);
}
}
void CustomCommand::moveAssign(CustomCommand&& rhs)
{
if (this != &rhs) {
memcpy(this, &rhs, sizeof(rhs));
rhs._vertexBuffer = rhs._indexBuffer = nullptr;
}
}
void CustomCommand::init(float depth, const cocos2d::Mat4 &modelViewTransform, unsigned int flags) void CustomCommand::init(float depth, const cocos2d::Mat4 &modelViewTransform, unsigned int flags)
{ {
RenderCommand::init(depth, modelViewTransform, flags); RenderCommand::init(depth, modelViewTransform, flags);

View File

@ -68,10 +68,18 @@ public:
/**Constructor.*/ /**Constructor.*/
CustomCommand(); CustomCommand();
CustomCommand(const CustomCommand& rhs);
CustomCommand(CustomCommand&& rhs);
/**Destructor.*/ /**Destructor.*/
~CustomCommand(); ~CustomCommand();
CustomCommand& operator=(const CustomCommand& rhs);
CustomCommand& operator=(CustomCommand&& rhs);
protected:
void copyAssign(const CustomCommand& rhs);
void moveAssign(CustomCommand&& rhs);
public: public:
/** /**
TODO: should remove it. TODO: should remove it.