From 745739575fcbb03776de52d7a6508d1acdb1a7dd Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 24 May 2018 22:06:14 +0800 Subject: [PATCH] =?UTF-8?q?GC::create=E6=94=B9=E4=B8=BA=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E5=B9=B6=E6=94=AF=E6=8C=81VS2012?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Action/Animate.cpp | 4 +- core/Action/Animation.cpp | 4 +- core/Action/CallFunc.cpp | 4 +- core/Action/Delay.cpp | 4 +- core/Action/JumpBy.cpp | 4 +- core/Action/JumpTo.cpp | 2 +- core/Action/Loop.cpp | 4 +- core/Action/MoveBy.cpp | 4 +- core/Action/MoveTo.cpp | 2 +- core/Action/OpacityBy.cpp | 4 +- core/Action/OpacityTo.cpp | 2 +- core/Action/RotateBy.cpp | 4 +- core/Action/RotateTo.cpp | 2 +- core/Action/ScaleBy.cpp | 4 +- core/Action/ScaleTo.cpp | 2 +- core/Action/Sequence.cpp | 4 +- core/Action/Spawn.cpp | 4 +- core/Base/Input.cpp | 2 +- core/Collider/Collision.cpp | 2 +- core/Common/Scene.cpp | 2 +- core/Node/Node.cpp | 6 +- core/Node/Sprite.cpp | 4 +- core/e2dbase.h | 13 ---- core/e2dcommon.h | 146 ++++++++++++++++++++++++++++++++++++ 24 files changed, 183 insertions(+), 50 deletions(-) diff --git a/core/Action/Animate.cpp b/core/Action/Animate.cpp index a369fb54..fbb562d8 100644 --- a/core/Action/Animate.cpp +++ b/core/Action/Animate.cpp @@ -98,7 +98,7 @@ e2d::Animate * e2d::Animate::clone() const { if (_animation) { - return GC::create(_animation); + return Create(_animation); } return nullptr; } @@ -110,7 +110,7 @@ e2d::Animate * e2d::Animate::reverse() const auto animation = _animation->reverse(); if (animation) { - return GC::create(animation); + return Create(animation); } } return nullptr; diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index 7debbf99..4017c17e 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -68,7 +68,7 @@ const std::vector& e2d::Animation::getFrames() const e2d::Animation * e2d::Animation::clone() const { - auto animation = GC::create(_interval); + auto animation = Create(_interval); if (animation) { for (auto frame : _frames) @@ -99,5 +99,5 @@ e2d::Animation * e2d::Animation::reverse() const } } - return GC::create(this->getInterval(), frames); + return Create(this->getInterval(), frames); } diff --git a/core/Action/CallFunc.cpp b/core/Action/CallFunc.cpp index 1f5bb284..ea739cf2 100644 --- a/core/Action/CallFunc.cpp +++ b/core/Action/CallFunc.cpp @@ -7,12 +7,12 @@ e2d::CallFunc::CallFunc(const Function& func) : e2d::CallFunc * e2d::CallFunc::clone() const { - return GC::create(_func); + return Create(_func); } e2d::CallFunc * e2d::CallFunc::reverse() const { - return GC::create(_func); + return Create(_func); } void e2d::CallFunc::_init() diff --git a/core/Action/Delay.cpp b/core/Action/Delay.cpp index ce686470..b8597844 100644 --- a/core/Action/Delay.cpp +++ b/core/Action/Delay.cpp @@ -8,12 +8,12 @@ e2d::Delay::Delay(double duration) e2d::Delay * e2d::Delay::clone() const { - return GC::create(_delay); + return Create(_delay); } e2d::Delay * e2d::Delay::reverse() const { - return GC::create(_delay); + return Create(_delay); } void e2d::Delay::reset() diff --git a/core/Action/JumpBy.cpp b/core/Action/JumpBy.cpp index 7b7102cc..dacbbb2e 100644 --- a/core/Action/JumpBy.cpp +++ b/core/Action/JumpBy.cpp @@ -11,12 +11,12 @@ e2d::JumpBy::JumpBy(double duration, const Vector & vec, double height, int jump e2d::JumpBy * e2d::JumpBy::clone() const { - return GC::create(_duration, _deltaPos, _height, _jumps); + return Create(_duration, _deltaPos, _height, _jumps); } e2d::JumpBy * e2d::JumpBy::reverse() const { - return GC::create(_duration, -_deltaPos, _height, _jumps); + return Create(_duration, -_deltaPos, _height, _jumps); } void e2d::JumpBy::_init() diff --git a/core/Action/JumpTo.cpp b/core/Action/JumpTo.cpp index ea262ada..e2426bbf 100644 --- a/core/Action/JumpTo.cpp +++ b/core/Action/JumpTo.cpp @@ -9,7 +9,7 @@ e2d::JumpTo::JumpTo(double duration, const Point & pos, double height, int jumps e2d::JumpTo * e2d::JumpTo::clone() const { - return GC::create(_duration, _endPos, _height, _jumps); + return Create(_duration, _endPos, _height, _jumps); } void e2d::JumpTo::_init() diff --git a/core/Action/Loop.cpp b/core/Action/Loop.cpp index 27633c7c..341bfef5 100644 --- a/core/Action/Loop.cpp +++ b/core/Action/Loop.cpp @@ -23,7 +23,7 @@ e2d::Loop * e2d::Loop::clone() const { if (_action) { - return GC::create(_action->clone()); + return Create(_action->clone()); } else { @@ -35,7 +35,7 @@ e2d::Loop * e2d::Loop::reverse() const { if (_action) { - return GC::create(_action->clone()); + return Create(_action->clone()); } else { diff --git a/core/Action/MoveBy.cpp b/core/Action/MoveBy.cpp index 4a75063b..e4da97a6 100644 --- a/core/Action/MoveBy.cpp +++ b/core/Action/MoveBy.cpp @@ -30,10 +30,10 @@ void e2d::MoveBy::_update() e2d::MoveBy * e2d::MoveBy::clone() const { - return GC::create(_duration, _deltaPos); + return Create(_duration, _deltaPos); } e2d::MoveBy * e2d::MoveBy::reverse() const { - return GC::create(_duration, -_deltaPos); + return Create(_duration, -_deltaPos); } \ No newline at end of file diff --git a/core/Action/MoveTo.cpp b/core/Action/MoveTo.cpp index 308c99c4..b56d7e40 100644 --- a/core/Action/MoveTo.cpp +++ b/core/Action/MoveTo.cpp @@ -9,7 +9,7 @@ e2d::MoveTo::MoveTo(double duration, Point pos) e2d::MoveTo * e2d::MoveTo::clone() const { - return GC::create(_duration, _endPos); + return Create(_duration, _endPos); } void e2d::MoveTo::_init() diff --git a/core/Action/OpacityBy.cpp b/core/Action/OpacityBy.cpp index a7acdce9..8726bcb0 100644 --- a/core/Action/OpacityBy.cpp +++ b/core/Action/OpacityBy.cpp @@ -30,10 +30,10 @@ void e2d::OpacityBy::_update() e2d::OpacityBy * e2d::OpacityBy::clone() const { - return GC::create(_duration, _deltaVal); + return Create(_duration, _deltaVal); } e2d::OpacityBy * e2d::OpacityBy::reverse() const { - return GC::create(_duration, -_deltaVal); + return Create(_duration, -_deltaVal); } \ No newline at end of file diff --git a/core/Action/OpacityTo.cpp b/core/Action/OpacityTo.cpp index ec054f61..bf4df219 100644 --- a/core/Action/OpacityTo.cpp +++ b/core/Action/OpacityTo.cpp @@ -10,7 +10,7 @@ e2d::OpacityTo::OpacityTo(double duration, double opacity) e2d::OpacityTo * e2d::OpacityTo::clone() const { - return GC::create(_duration, _endVal); + return Create(_duration, _endVal); } void e2d::OpacityTo::_init() diff --git a/core/Action/RotateBy.cpp b/core/Action/RotateBy.cpp index aba86b8e..bdcafb1c 100644 --- a/core/Action/RotateBy.cpp +++ b/core/Action/RotateBy.cpp @@ -30,10 +30,10 @@ void e2d::RotateBy::_update() e2d::RotateBy * e2d::RotateBy::clone() const { - return GC::create(_duration, _deltaVal); + return Create(_duration, _deltaVal); } e2d::RotateBy * e2d::RotateBy::reverse() const { - return GC::create(_duration, -_deltaVal); + return Create(_duration, -_deltaVal); } \ No newline at end of file diff --git a/core/Action/RotateTo.cpp b/core/Action/RotateTo.cpp index ee058676..25a766d3 100644 --- a/core/Action/RotateTo.cpp +++ b/core/Action/RotateTo.cpp @@ -10,7 +10,7 @@ e2d::RotateTo::RotateTo(double duration, double rotation) e2d::RotateTo * e2d::RotateTo::clone() const { - return GC::create(_duration, _endVal); + return Create(_duration, _endVal); } void e2d::RotateTo::_init() diff --git a/core/Action/ScaleBy.cpp b/core/Action/ScaleBy.cpp index f0ad98dc..8c56472c 100644 --- a/core/Action/ScaleBy.cpp +++ b/core/Action/ScaleBy.cpp @@ -39,10 +39,10 @@ void e2d::ScaleBy::_update() e2d::ScaleBy * e2d::ScaleBy::clone() const { - return GC::create(_duration, _deltaX, _deltaY); + return Create(_duration, _deltaX, _deltaY); } e2d::ScaleBy * e2d::ScaleBy::reverse() const { - return GC::create(_duration, -_deltaX, -_deltaY); + return Create(_duration, -_deltaX, -_deltaY); } \ No newline at end of file diff --git a/core/Action/ScaleTo.cpp b/core/Action/ScaleTo.cpp index c9ac7bb1..3dcba0e9 100644 --- a/core/Action/ScaleTo.cpp +++ b/core/Action/ScaleTo.cpp @@ -17,7 +17,7 @@ e2d::ScaleTo::ScaleTo(double duration, double scaleX, double scaleY) e2d::ScaleTo * e2d::ScaleTo::clone() const { - return GC::create(_duration, _endScaleX, _endScaleY); + return Create(_duration, _endScaleX, _endScaleY); } void e2d::ScaleTo::_init() diff --git a/core/Action/Sequence.cpp b/core/Action/Sequence.cpp index 33aceab6..01b1f957 100644 --- a/core/Action/Sequence.cpp +++ b/core/Action/Sequence.cpp @@ -97,7 +97,7 @@ void e2d::Sequence::add(const std::vector& actions) e2d::Sequence * e2d::Sequence::clone() const { - auto sequence = GC::create(); + auto sequence = Create(); for (const auto& action : _actions) { if (action) @@ -110,7 +110,7 @@ e2d::Sequence * e2d::Sequence::clone() const e2d::Sequence * e2d::Sequence::reverse() const { - auto sequence = GC::create(); + auto sequence = Create(); if (sequence && !_actions.empty()) { std::vector newActions(_actions.size()); diff --git a/core/Action/Spawn.cpp b/core/Action/Spawn.cpp index 79b28f6b..eb9138a3 100644 --- a/core/Action/Spawn.cpp +++ b/core/Action/Spawn.cpp @@ -95,7 +95,7 @@ void e2d::Spawn::add(const std::vector& actions) e2d::Spawn * e2d::Spawn::clone() const { - auto spawn = GC::create(); + auto spawn = Create(); for (const auto& action : _actions) { if (action) @@ -108,7 +108,7 @@ e2d::Spawn * e2d::Spawn::clone() const e2d::Spawn * e2d::Spawn::reverse() const { - auto spawn = GC::create(); + auto spawn = Create(); if (spawn && !_actions.empty()) { std::vector newActions(_actions.size()); diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index c5a843fb..e9657d5f 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -222,7 +222,7 @@ double Input::getMouseDeltaZ() e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused) { - auto listener = GC::create(func, name, paused); + auto listener = Create(func, name, paused); GC::retain(listener); s_vListeners.push_back(listener); return listener; diff --git a/core/Collider/Collision.cpp b/core/Collider/Collision.cpp index a7f95aa7..97df57b4 100644 --- a/core/Collider/Collision.cpp +++ b/core/Collider/Collision.cpp @@ -119,7 +119,7 @@ void e2d::Collision::__update(Node * active, Node * passive) e2d::Listener * e2d::Collision::addListener(const Function& func, const String& name, bool paused) { - auto listener = GC::create(func, name, paused); + auto listener = Create(func, name, paused); GC::retain(listener); s_vListeners.push_back(listener); return listener; diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index 36b7f45f..b4ccd3fd 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -7,7 +7,7 @@ e2d::Scene::Scene() , _colliderVisiable(false) , _root(nullptr) { - _root = GC::create(); + _root = Create(); if (_root) { _root->retain(); diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index a79eb194..ce9ab205 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -544,19 +544,19 @@ void e2d::Node::setCollider(Collider::Type type) { case Collider::Type::Rect: { - this->setCollider(GC::create(this)); + this->setCollider(Create(this)); break; } case Collider::Type::Circle: { - this->setCollider(GC::create(this)); + this->setCollider(Create(this)); break; } case Collider::Type::Ellipse: { - this->setCollider(GC::create(this)); + this->setCollider(Create(this)); break; } diff --git a/core/Node/Sprite.cpp b/core/Node/Sprite.cpp index 8db6dd8b..4bc3c8d5 100644 --- a/core/Node/Sprite.cpp +++ b/core/Node/Sprite.cpp @@ -60,7 +60,7 @@ bool e2d::Sprite::open(const String& filePath) { if (!_image) { - _image = GC::create(); + _image = Create(); _image->retain(); } @@ -76,7 +76,7 @@ bool e2d::Sprite::open(int resNameId, const String& resType) { if (!_image) { - _image = GC::create(); + _image = Create(); _image->retain(); } diff --git a/core/e2dbase.h b/core/e2dbase.h index d1e8909a..36b6d3e1 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -447,19 +447,6 @@ class GC friend class Object; public: - // 创建可自动回收内存的对象 - template - static inline Type * create(Args&&... args) - { - auto newObj = new (std::nothrow) Type(std::forward(args)...); - if (newObj) - { - newObj->autorelease(); - return newObj; - } - return nullptr; - } - // 保留对象 template static inline void retain(Type*& p) diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 07f364df..dcaf9a8a 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -652,4 +652,150 @@ protected: }; +#if _MSC_VER > 1700 + +// 创建可自动回收内存的对象 +template +inline Type * Create(Args&&... args) +{ + auto newObj = new (std::nothrow) Type(std::forward(args)...); + if (newObj) + { + newObj->autorelease(); + return newObj; + } + return nullptr; +} + +#else + +template +inline Type * Create() +{ + auto newObj = new (std::nothrow) Type(); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template +inline Type * Create(Arg1&& arg1) +{ + auto newObj = new (std::nothrow) Type(std::forward(arg1)); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template + inline Type * Create( + Arg1&& arg1, + Arg2&& arg2 + ) +{ + auto newObj = new (std::nothrow) Type( + std::forward(arg1), + std::forward(arg2) + ); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template + inline Type * Create( + Arg1&& arg1, + Arg2&& arg2, + Arg3&& arg3 + ) +{ + auto newObj = new (std::nothrow) Type( + std::forward(arg1), + std::forward(arg2), + std::forward(arg3) + ); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template + inline Type * Create( + Arg1&& arg1, + Arg2&& arg2, + Arg3&& arg3, + Arg4&& arg4 + ) +{ + auto newObj = new (std::nothrow) Type( + std::forward(arg1), + std::forward(arg2), + std::forward(arg3), + std::forward(arg4) + ); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template + inline Type * Create( + Arg1&& arg1, + Arg2&& arg2, + Arg3&& arg3, + Arg4&& arg4, + Arg5&& arg5 + ) +{ + auto newObj = new (std::nothrow) Type( + std::forward(arg1), + std::forward(arg2), + std::forward(arg3), + std::forward(arg4), + std::forward(arg5) + ); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +template + inline Type * Create( + Arg1&& arg1, + Arg2&& arg2, + Arg3&& arg3, + Arg4&& arg4, + Arg5&& arg5, + Arg6&& arg6 + ) +{ + auto newObj = new (std::nothrow) Type( + std::forward(arg1), + std::forward(arg2), + std::forward(arg3), + std::forward(arg4), + std::forward(arg5), + std::forward(arg6) + ); + if (newObj) { newObj->autorelease(); return newObj; } + return nullptr; +} + +#endif + + } \ No newline at end of file