GC::create改为全局函数,并支持VS2012

This commit is contained in:
Nomango 2018-05-24 22:06:14 +08:00
parent 2058dfaa7d
commit 745739575f
24 changed files with 183 additions and 50 deletions

View File

@ -98,7 +98,7 @@ e2d::Animate * e2d::Animate::clone() const
{ {
if (_animation) if (_animation)
{ {
return GC::create<Animate>(_animation); return Create<Animate>(_animation);
} }
return nullptr; return nullptr;
} }
@ -110,7 +110,7 @@ e2d::Animate * e2d::Animate::reverse() const
auto animation = _animation->reverse(); auto animation = _animation->reverse();
if (animation) if (animation)
{ {
return GC::create<Animate>(animation); return Create<Animate>(animation);
} }
} }
return nullptr; return nullptr;

View File

@ -68,7 +68,7 @@ const std::vector<e2d::Image*>& e2d::Animation::getFrames() const
e2d::Animation * e2d::Animation::clone() const e2d::Animation * e2d::Animation::clone() const
{ {
auto animation = GC::create<Animation>(_interval); auto animation = Create<Animation>(_interval);
if (animation) if (animation)
{ {
for (auto frame : _frames) for (auto frame : _frames)
@ -99,5 +99,5 @@ e2d::Animation * e2d::Animation::reverse() const
} }
} }
return GC::create<Animation>(this->getInterval(), frames); return Create<Animation>(this->getInterval(), frames);
} }

View File

@ -7,12 +7,12 @@ e2d::CallFunc::CallFunc(const Function& func) :
e2d::CallFunc * e2d::CallFunc::clone() const e2d::CallFunc * e2d::CallFunc::clone() const
{ {
return GC::create<CallFunc>(_func); return Create<CallFunc>(_func);
} }
e2d::CallFunc * e2d::CallFunc::reverse() const e2d::CallFunc * e2d::CallFunc::reverse() const
{ {
return GC::create<CallFunc>(_func); return Create<CallFunc>(_func);
} }
void e2d::CallFunc::_init() void e2d::CallFunc::_init()

View File

@ -8,12 +8,12 @@ e2d::Delay::Delay(double duration)
e2d::Delay * e2d::Delay::clone() const e2d::Delay * e2d::Delay::clone() const
{ {
return GC::create<Delay>(_delay); return Create<Delay>(_delay);
} }
e2d::Delay * e2d::Delay::reverse() const e2d::Delay * e2d::Delay::reverse() const
{ {
return GC::create<Delay>(_delay); return Create<Delay>(_delay);
} }
void e2d::Delay::reset() void e2d::Delay::reset()

View File

@ -11,12 +11,12 @@ e2d::JumpBy::JumpBy(double duration, const Vector & vec, double height, int jump
e2d::JumpBy * e2d::JumpBy::clone() const e2d::JumpBy * e2d::JumpBy::clone() const
{ {
return GC::create<JumpBy>(_duration, _deltaPos, _height, _jumps); return Create<JumpBy>(_duration, _deltaPos, _height, _jumps);
} }
e2d::JumpBy * e2d::JumpBy::reverse() const e2d::JumpBy * e2d::JumpBy::reverse() const
{ {
return GC::create<JumpBy>(_duration, -_deltaPos, _height, _jumps); return Create<JumpBy>(_duration, -_deltaPos, _height, _jumps);
} }
void e2d::JumpBy::_init() void e2d::JumpBy::_init()

View File

@ -9,7 +9,7 @@ e2d::JumpTo::JumpTo(double duration, const Point & pos, double height, int jumps
e2d::JumpTo * e2d::JumpTo::clone() const e2d::JumpTo * e2d::JumpTo::clone() const
{ {
return GC::create<JumpTo>(_duration, _endPos, _height, _jumps); return Create<JumpTo>(_duration, _endPos, _height, _jumps);
} }
void e2d::JumpTo::_init() void e2d::JumpTo::_init()

View File

@ -23,7 +23,7 @@ e2d::Loop * e2d::Loop::clone() const
{ {
if (_action) if (_action)
{ {
return GC::create<Loop>(_action->clone()); return Create<Loop>(_action->clone());
} }
else else
{ {
@ -35,7 +35,7 @@ e2d::Loop * e2d::Loop::reverse() const
{ {
if (_action) if (_action)
{ {
return GC::create<Loop>(_action->clone()); return Create<Loop>(_action->clone());
} }
else else
{ {

View File

@ -30,10 +30,10 @@ void e2d::MoveBy::_update()
e2d::MoveBy * e2d::MoveBy::clone() const e2d::MoveBy * e2d::MoveBy::clone() const
{ {
return GC::create<MoveBy>(_duration, _deltaPos); return Create<MoveBy>(_duration, _deltaPos);
} }
e2d::MoveBy * e2d::MoveBy::reverse() const e2d::MoveBy * e2d::MoveBy::reverse() const
{ {
return GC::create<MoveBy>(_duration, -_deltaPos); return Create<MoveBy>(_duration, -_deltaPos);
} }

View File

@ -9,7 +9,7 @@ e2d::MoveTo::MoveTo(double duration, Point pos)
e2d::MoveTo * e2d::MoveTo::clone() const e2d::MoveTo * e2d::MoveTo::clone() const
{ {
return GC::create<MoveTo>(_duration, _endPos); return Create<MoveTo>(_duration, _endPos);
} }
void e2d::MoveTo::_init() void e2d::MoveTo::_init()

View File

@ -30,10 +30,10 @@ void e2d::OpacityBy::_update()
e2d::OpacityBy * e2d::OpacityBy::clone() const e2d::OpacityBy * e2d::OpacityBy::clone() const
{ {
return GC::create<OpacityBy>(_duration, _deltaVal); return Create<OpacityBy>(_duration, _deltaVal);
} }
e2d::OpacityBy * e2d::OpacityBy::reverse() const e2d::OpacityBy * e2d::OpacityBy::reverse() const
{ {
return GC::create<OpacityBy>(_duration, -_deltaVal); return Create<OpacityBy>(_duration, -_deltaVal);
} }

View File

@ -10,7 +10,7 @@ e2d::OpacityTo::OpacityTo(double duration, double opacity)
e2d::OpacityTo * e2d::OpacityTo::clone() const e2d::OpacityTo * e2d::OpacityTo::clone() const
{ {
return GC::create<OpacityTo>(_duration, _endVal); return Create<OpacityTo>(_duration, _endVal);
} }
void e2d::OpacityTo::_init() void e2d::OpacityTo::_init()

View File

@ -30,10 +30,10 @@ void e2d::RotateBy::_update()
e2d::RotateBy * e2d::RotateBy::clone() const e2d::RotateBy * e2d::RotateBy::clone() const
{ {
return GC::create<RotateBy>(_duration, _deltaVal); return Create<RotateBy>(_duration, _deltaVal);
} }
e2d::RotateBy * e2d::RotateBy::reverse() const e2d::RotateBy * e2d::RotateBy::reverse() const
{ {
return GC::create<RotateBy>(_duration, -_deltaVal); return Create<RotateBy>(_duration, -_deltaVal);
} }

View File

@ -10,7 +10,7 @@ e2d::RotateTo::RotateTo(double duration, double rotation)
e2d::RotateTo * e2d::RotateTo::clone() const e2d::RotateTo * e2d::RotateTo::clone() const
{ {
return GC::create<RotateTo>(_duration, _endVal); return Create<RotateTo>(_duration, _endVal);
} }
void e2d::RotateTo::_init() void e2d::RotateTo::_init()

View File

@ -39,10 +39,10 @@ void e2d::ScaleBy::_update()
e2d::ScaleBy * e2d::ScaleBy::clone() const e2d::ScaleBy * e2d::ScaleBy::clone() const
{ {
return GC::create<ScaleBy>(_duration, _deltaX, _deltaY); return Create<ScaleBy>(_duration, _deltaX, _deltaY);
} }
e2d::ScaleBy * e2d::ScaleBy::reverse() const e2d::ScaleBy * e2d::ScaleBy::reverse() const
{ {
return GC::create<ScaleBy>(_duration, -_deltaX, -_deltaY); return Create<ScaleBy>(_duration, -_deltaX, -_deltaY);
} }

View File

@ -17,7 +17,7 @@ e2d::ScaleTo::ScaleTo(double duration, double scaleX, double scaleY)
e2d::ScaleTo * e2d::ScaleTo::clone() const e2d::ScaleTo * e2d::ScaleTo::clone() const
{ {
return GC::create<ScaleTo>(_duration, _endScaleX, _endScaleY); return Create<ScaleTo>(_duration, _endScaleX, _endScaleY);
} }
void e2d::ScaleTo::_init() void e2d::ScaleTo::_init()

View File

@ -97,7 +97,7 @@ void e2d::Sequence::add(const std::vector<Action*>& actions)
e2d::Sequence * e2d::Sequence::clone() const e2d::Sequence * e2d::Sequence::clone() const
{ {
auto sequence = GC::create<Sequence>(); auto sequence = Create<Sequence>();
for (const auto& action : _actions) for (const auto& action : _actions)
{ {
if (action) if (action)
@ -110,7 +110,7 @@ e2d::Sequence * e2d::Sequence::clone() const
e2d::Sequence * e2d::Sequence::reverse() const e2d::Sequence * e2d::Sequence::reverse() const
{ {
auto sequence = GC::create<Sequence>(); auto sequence = Create<Sequence>();
if (sequence && !_actions.empty()) if (sequence && !_actions.empty())
{ {
std::vector<Action*> newActions(_actions.size()); std::vector<Action*> newActions(_actions.size());

View File

@ -95,7 +95,7 @@ void e2d::Spawn::add(const std::vector<Action*>& actions)
e2d::Spawn * e2d::Spawn::clone() const e2d::Spawn * e2d::Spawn::clone() const
{ {
auto spawn = GC::create<Spawn>(); auto spawn = Create<Spawn>();
for (const auto& action : _actions) for (const auto& action : _actions)
{ {
if (action) if (action)
@ -108,7 +108,7 @@ e2d::Spawn * e2d::Spawn::clone() const
e2d::Spawn * e2d::Spawn::reverse() const e2d::Spawn * e2d::Spawn::reverse() const
{ {
auto spawn = GC::create<Spawn>(); auto spawn = Create<Spawn>();
if (spawn && !_actions.empty()) if (spawn && !_actions.empty())
{ {
std::vector<Action*> newActions(_actions.size()); std::vector<Action*> newActions(_actions.size());

View File

@ -222,7 +222,7 @@ double Input::getMouseDeltaZ()
e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused) e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused)
{ {
auto listener = GC::create<Listener>(func, name, paused); auto listener = Create<Listener>(func, name, paused);
GC::retain(listener); GC::retain(listener);
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;

View File

@ -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) e2d::Listener * e2d::Collision::addListener(const Function& func, const String& name, bool paused)
{ {
auto listener = GC::create<Listener>(func, name, paused); auto listener = Create<Listener>(func, name, paused);
GC::retain(listener); GC::retain(listener);
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;

View File

@ -7,7 +7,7 @@ e2d::Scene::Scene()
, _colliderVisiable(false) , _colliderVisiable(false)
, _root(nullptr) , _root(nullptr)
{ {
_root = GC::create<Node>(); _root = Create<Node>();
if (_root) if (_root)
{ {
_root->retain(); _root->retain();

View File

@ -544,19 +544,19 @@ void e2d::Node::setCollider(Collider::Type type)
{ {
case Collider::Type::Rect: case Collider::Type::Rect:
{ {
this->setCollider(GC::create<RectCollider>(this)); this->setCollider(Create<RectCollider>(this));
break; break;
} }
case Collider::Type::Circle: case Collider::Type::Circle:
{ {
this->setCollider(GC::create<CircleCollider>(this)); this->setCollider(Create<CircleCollider>(this));
break; break;
} }
case Collider::Type::Ellipse: case Collider::Type::Ellipse:
{ {
this->setCollider(GC::create<EllipseCollider>(this)); this->setCollider(Create<EllipseCollider>(this));
break; break;
} }

View File

@ -60,7 +60,7 @@ bool e2d::Sprite::open(const String& filePath)
{ {
if (!_image) if (!_image)
{ {
_image = GC::create<Image>(); _image = Create<Image>();
_image->retain(); _image->retain();
} }
@ -76,7 +76,7 @@ bool e2d::Sprite::open(int resNameId, const String& resType)
{ {
if (!_image) if (!_image)
{ {
_image = GC::create<Image>(); _image = Create<Image>();
_image->retain(); _image->retain();
} }

View File

@ -447,19 +447,6 @@ class GC
friend class Object; friend class Object;
public: public:
// 创建可自动回收内存的对象
template <typename Type, typename... Args>
static inline Type * create(Args&&... args)
{
auto newObj = new (std::nothrow) Type(std::forward<Args>(args)...);
if (newObj)
{
newObj->autorelease();
return newObj;
}
return nullptr;
}
// 保留对象 // 保留对象
template <typename Type> template <typename Type>
static inline void retain(Type*& p) static inline void retain(Type*& p)

View File

@ -652,4 +652,150 @@ protected:
}; };
#if _MSC_VER > 1700
// 创建可自动回收内存的对象
template <typename Type, typename... Args>
inline Type * Create(Args&&... args)
{
auto newObj = new (std::nothrow) Type(std::forward<Args>(args)...);
if (newObj)
{
newObj->autorelease();
return newObj;
}
return nullptr;
}
#else
template <typename Type>
inline Type * Create()
{
auto newObj = new (std::nothrow) Type();
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type, typename Arg1>
inline Type * Create(Arg1&& arg1)
{
auto newObj = new (std::nothrow) Type(std::forward<Arg1>(arg1));
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4,
typename Arg5>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4,
Arg5&& arg5
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4),
std::forward<Arg5>(arg5)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4,
typename Arg5,
typename Arg6>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4,
Arg5&& arg5,
Arg6&& arg6
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4),
std::forward<Arg5>(arg5),
std::forward<Arg6>(arg6)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
#endif
} }