去除Config的自动回收对象选项,增加operator new重载

This commit is contained in:
Nomango 2018-07-06 12:59:32 +08:00
parent fb389cce89
commit a2d173800b
32 changed files with 104 additions and 94 deletions

View File

@ -98,7 +98,7 @@ e2d::Animate * e2d::Animate::clone() const
{ {
if (_animation) if (_animation)
{ {
return new (std::nothrow) Animate(_animation); return new (e2d::autorelease) 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 new (std::nothrow) Animate(animation); return new (e2d::autorelease) 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 = new (std::nothrow) Animation(_interval); auto animation = new (e2d::autorelease) 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 new (std::nothrow) Animation(this->getInterval(), frames); return new (e2d::autorelease) 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 new (std::nothrow) CallFunc(_func); return new (e2d::autorelease) CallFunc(_func);
} }
e2d::CallFunc * e2d::CallFunc::reverse() const e2d::CallFunc * e2d::CallFunc::reverse() const
{ {
return new (std::nothrow) CallFunc(_func); return new (e2d::autorelease) 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 new (std::nothrow) Delay(_delay); return new (e2d::autorelease) Delay(_delay);
} }
e2d::Delay * e2d::Delay::reverse() const e2d::Delay * e2d::Delay::reverse() const
{ {
return new (std::nothrow) Delay(_delay); return new (e2d::autorelease) Delay(_delay);
} }
void e2d::Delay::reset() void e2d::Delay::reset()

View File

@ -11,12 +11,12 @@ e2d::JumpBy::JumpBy(double duration, const Vector2 & vec, double height, int jum
e2d::JumpBy * e2d::JumpBy::clone() const e2d::JumpBy * e2d::JumpBy::clone() const
{ {
return new (std::nothrow) JumpBy(_duration, _deltaPos, _height, _jumps); return new (e2d::autorelease) JumpBy(_duration, _deltaPos, _height, _jumps);
} }
e2d::JumpBy * e2d::JumpBy::reverse() const e2d::JumpBy * e2d::JumpBy::reverse() const
{ {
return new (std::nothrow) JumpBy(_duration, -_deltaPos, _height, _jumps); return new (e2d::autorelease) 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 new (std::nothrow) JumpTo(_duration, _endPos, _height, _jumps); return new (e2d::autorelease) 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 new (std::nothrow) Loop(_action->clone()); return new (e2d::autorelease) Loop(_action->clone());
} }
else else
{ {
@ -35,7 +35,7 @@ e2d::Loop * e2d::Loop::reverse() const
{ {
if (_action) if (_action)
{ {
return new (std::nothrow) Loop(_action->clone()); return new (e2d::autorelease) Loop(_action->clone());
} }
else else
{ {

View File

@ -37,10 +37,10 @@ void e2d::MoveBy::_update()
e2d::MoveBy * e2d::MoveBy::clone() const e2d::MoveBy * e2d::MoveBy::clone() const
{ {
return new (std::nothrow) MoveBy(_duration, _deltaPos); return new (e2d::autorelease) MoveBy(_duration, _deltaPos);
} }
e2d::MoveBy * e2d::MoveBy::reverse() const e2d::MoveBy * e2d::MoveBy::reverse() const
{ {
return new (std::nothrow) MoveBy(_duration, -_deltaPos); return new (e2d::autorelease) 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 new (std::nothrow) MoveTo(_duration, _endPos); return new (e2d::autorelease) 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 new (std::nothrow) OpacityBy(_duration, _deltaVal); return new (e2d::autorelease) OpacityBy(_duration, _deltaVal);
} }
e2d::OpacityBy * e2d::OpacityBy::reverse() const e2d::OpacityBy * e2d::OpacityBy::reverse() const
{ {
return new (std::nothrow) OpacityBy(_duration, -_deltaVal); return new (e2d::autorelease) 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 new (std::nothrow) OpacityTo(_duration, _endVal); return new (e2d::autorelease) 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 new (std::nothrow) RotateBy(_duration, _deltaVal); return new (e2d::autorelease) RotateBy(_duration, _deltaVal);
} }
e2d::RotateBy * e2d::RotateBy::reverse() const e2d::RotateBy * e2d::RotateBy::reverse() const
{ {
return new (std::nothrow) RotateBy(_duration, -_deltaVal); return new (e2d::autorelease) 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 new (std::nothrow) RotateTo(_duration, _endVal); return new (e2d::autorelease) 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 new (std::nothrow) ScaleBy(_duration, _deltaX, _deltaY); return new (e2d::autorelease) ScaleBy(_duration, _deltaX, _deltaY);
} }
e2d::ScaleBy * e2d::ScaleBy::reverse() const e2d::ScaleBy * e2d::ScaleBy::reverse() const
{ {
return new (std::nothrow) ScaleBy(_duration, -_deltaX, -_deltaY); return new (e2d::autorelease) 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 new (std::nothrow) ScaleTo(_duration, _endScaleX, _endScaleY); return new (e2d::autorelease) 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 = new (std::nothrow) Sequence(); auto sequence = new (e2d::autorelease) 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 = new (std::nothrow) Sequence(); auto sequence = new (e2d::autorelease) 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 = new (std::nothrow) Spawn(); auto spawn = new (e2d::autorelease) 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 = new (std::nothrow) Spawn(); auto spawn = new (e2d::autorelease) Spawn();
if (spawn && !_actions.empty()) if (spawn && !_actions.empty())
{ {
std::vector<Action*> newActions(_actions.size()); std::vector<Action*> newActions(_actions.size());

View File

@ -236,8 +236,7 @@ double e2d::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 = new (std::nothrow) Listener(func, name, paused); auto listener = new (e2d::autorelease) Listener(func, name, paused);
listener->autorelease();
listener->retain(); listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;

View File

@ -121,8 +121,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 = new (std::nothrow) Listener(func, name, paused); auto listener = new (e2d::autorelease) Listener(func, name, paused);
listener->autorelease();
listener->retain(); listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;

View File

@ -7,7 +7,6 @@ e2d::Config::Config()
, _soundEnabled(true) , _soundEnabled(true)
, _collisionEnabled(false) , _collisionEnabled(false)
, _colliderVisiable(false) , _colliderVisiable(false)
, _objectsAutoRelease(false)
, _nodeDefColliderType(Collider::Type::None) , _nodeDefColliderType(Collider::Type::None)
, _unconfigured(true) , _unconfigured(true)
{ {
@ -31,11 +30,6 @@ void e2d::Config::setSoundEnabled(bool enabled)
} }
} }
void e2d::Config::setObjectsAutoReleaseEnabled(bool enabled)
{
_objectsAutoRelease = enabled;
}
void e2d::Config::setCollisionEnabled(bool enabled) void e2d::Config::setCollisionEnabled(bool enabled)
{ {
_collisionEnabled = enabled; _collisionEnabled = enabled;
@ -69,11 +63,6 @@ bool e2d::Config::isSoundEnabled() const
return _soundEnabled; return _soundEnabled;
} }
bool e2d::Config::isObjectsAutoReleaseEnabled() const
{
return _objectsAutoRelease;
}
bool e2d::Config::isCollisionEnabled() const bool e2d::Config::isCollisionEnabled() const
{ {
return _collisionEnabled; return _collisionEnabled;

View File

@ -1,14 +1,28 @@
#include "..\e2dbase.h" #include "..\e2dbase.h"
#include "..\e2dmanager.h" #include "..\e2dmanager.h"
e2d::autorelease_t const e2d::autorelease = e2d::autorelease_t();
void * operator new(size_t _Size, e2d::autorelease_t const &) E2D_NOEXCEPT
{
void* p = ::operator new (_Size, std::nothrow);
e2d::Object * newObject = static_cast<e2d::Object*>(p);
if (newObject)
{
newObject->autorelease();
}
return p;
}
void operator delete(void * _Block, e2d::autorelease_t const &) E2D_NOEXCEPT
{
::operator delete (_Block, std::nothrow);
}
e2d::Object::Object() e2d::Object::Object()
: _refCount(0) : _refCount(0)
{ {
if (Game::getInstance()->getConfig().isObjectsAutoReleaseEnabled())
{
this->autorelease();
}
} }
e2d::Object::~Object() e2d::Object::~Object()

View File

@ -6,8 +6,7 @@ e2d::Scene::Scene()
: _autoUpdate(true) : _autoUpdate(true)
, _root(nullptr) , _root(nullptr)
{ {
_root = new (std::nothrow) Node(); _root = new (e2d::autorelease) Node();
_root->autorelease();
_root->retain(); _root->retain();
_root->_setParentScene(this); _root->_setParentScene(this);
} }

View File

@ -1,22 +1,22 @@
#include "..\e2dcustom.h" #include "..\e2dcustom.h"
e2d::Exception::Exception() throw() e2d::Exception::Exception() E2D_NOEXCEPT
: _message() : _message()
{ {
} }
e2d::Exception::Exception(const String& message) throw() e2d::Exception::Exception(const String& message) E2D_NOEXCEPT
: _message(message) : _message(message)
{ {
} }
e2d::Exception::Exception(Exception const& other) throw() e2d::Exception::Exception(Exception const& other) E2D_NOEXCEPT
: _message(other._message) : _message(other._message)
{ {
} }
e2d::Exception& e2d::Exception::operator=(Exception const& other) throw() e2d::Exception& e2d::Exception::operator=(Exception const& other) E2D_NOEXCEPT
{ {
if (this == &other) if (this == &other)
{ {
@ -27,7 +27,7 @@ e2d::Exception& e2d::Exception::operator=(Exception const& other) throw()
return *this; return *this;
} }
e2d::Exception::~Exception() throw() e2d::Exception::~Exception() E2D_NOEXCEPT
{ {
} }

View File

@ -1,11 +1,11 @@
#include "..\e2dcustom.h" #include "..\e2dcustom.h"
e2d::SystemException::SystemException() throw() e2d::SystemException::SystemException() E2D_NOEXCEPT
: Exception(L"未知的系统异常") : Exception(L"未知的系统异常")
{ {
} }
e2d::SystemException::SystemException(const String& message) throw() e2d::SystemException::SystemException(const String& message) E2D_NOEXCEPT
: Exception(message) : Exception(message)
{ {
} }

View File

@ -573,8 +573,7 @@ void e2d::Node::setColliderType(Collider::Type type)
case Collider::Type::Circle: case Collider::Type::Circle:
case Collider::Type::Ellipse: case Collider::Type::Ellipse:
{ {
_collider = new (std::nothrow) Collider(); _collider = new (e2d::autorelease) Collider();
_collider->autorelease();
_collider->_parentNode = this; _collider->_parentNode = this;
_collider->_recreate(type); _collider->_recreate(type);
// 添加新的碰撞体 // 添加新的碰撞体

View File

@ -60,8 +60,7 @@ bool e2d::Sprite::open(const String& filePath)
{ {
if (!_image) if (!_image)
{ {
_image = new (std::nothrow) Image(); _image = new (e2d::autorelease) Image();
_image->autorelease();
_image->retain(); _image->retain();
} }
@ -77,8 +76,7 @@ bool e2d::Sprite::open(int resNameId, const String& resType)
{ {
if (!_image) if (!_image)
{ {
_image = new (std::nothrow) Image(); _image = new (e2d::autorelease) Image();
_image->autorelease();
_image->retain(); _image->retain();
} }

View File

@ -118,7 +118,7 @@ bool e2d::Music::open(const e2d::String& filePath)
_dwSize = _ck.cksize; _dwSize = _ck.cksize;
// 将样本数据读取到内存中 // 将样本数据读取到内存中
_waveData = new (std::nothrow) BYTE[_dwSize]; _waveData = new BYTE[_dwSize];
if (!_read(_waveData, _dwSize)) if (!_read(_waveData, _dwSize))
{ {
@ -173,7 +173,7 @@ bool e2d::Music::open(int resNameId, const e2d::String& resType)
if (nullptr == (pvRes = LockResource(hResData))) if (nullptr == (pvRes = LockResource(hResData)))
return TraceError(L"LockResource"); return TraceError(L"LockResource");
_resBuffer = new (std::nothrow) CHAR[dwSize]; _resBuffer = new CHAR[dwSize];
memcpy(_resBuffer, pvRes, dwSize); memcpy(_resBuffer, pvRes, dwSize);
MMIOINFO mmioInfo; MMIOINFO mmioInfo;
@ -198,7 +198,7 @@ bool e2d::Music::open(int resNameId, const e2d::String& resType)
_dwSize = _ck.cksize; _dwSize = _ck.cksize;
// 将样本数据读取到内存中 // 将样本数据读取到内存中
_waveData = new (std::nothrow) BYTE[_dwSize]; _waveData = new BYTE[_dwSize];
if (!_read(_waveData, _dwSize)) if (!_read(_waveData, _dwSize))
{ {
@ -404,7 +404,7 @@ bool e2d::Music::_readMMIO()
// 的数据,这个数据就是额外分配的大小 // 的数据,这个数据就是额外分配的大小
if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM) if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
{ {
_wfx = (WAVEFORMATEX*) new (std::nothrow) CHAR[sizeof(WAVEFORMATEX)]; _wfx = (WAVEFORMATEX*) new CHAR[sizeof(WAVEFORMATEX)];
// 拷贝数据 // 拷贝数据
memcpy(_wfx, &pcmWaveFormat, sizeof(pcmWaveFormat)); memcpy(_wfx, &pcmWaveFormat, sizeof(pcmWaveFormat));
@ -417,7 +417,7 @@ bool e2d::Music::_readMMIO()
if (mmioRead(_hmmio, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD)) if (mmioRead(_hmmio, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD))
return TraceError(L"mmioRead"); return TraceError(L"mmioRead");
_wfx = (WAVEFORMATEX*) new (std::nothrow) CHAR[sizeof(WAVEFORMATEX) + cbExtraBytes]; _wfx = (WAVEFORMATEX*) new CHAR[sizeof(WAVEFORMATEX) + cbExtraBytes];
// 拷贝数据 // 拷贝数据
memcpy(_wfx, &pcmWaveFormat, sizeof(pcmWaveFormat)); memcpy(_wfx, &pcmWaveFormat, sizeof(pcmWaveFormat));

View File

@ -69,8 +69,7 @@ bool e2d::Player::preload(const String& filePath)
} }
else else
{ {
Music * music = new (std::nothrow) Music(); Music * music = new (e2d::autorelease) Music();
music->autorelease();
if (music->open(filePath)) if (music->open(filePath))
{ {
@ -91,8 +90,7 @@ bool e2d::Player::preload(int resNameId, const String& resType)
} }
else else
{ {
Music * music = new (std::nothrow) Music(); Music * music = new (e2d::autorelease) Music();
music->autorelease();
if (music->open(resNameId, resType)) if (music->open(resNameId, resType))
{ {

View File

@ -764,12 +764,6 @@ public:
bool enabled bool enabled
); );
// 打开或关闭自动回收 Easy2D 对象功能
// 默认:关闭
void setObjectsAutoReleaseEnabled(
bool enabled
);
// 打开或关闭碰撞监听 // 打开或关闭碰撞监听
// 默认:关闭 // 默认:关闭
void setCollisionEnabled( void setCollisionEnabled(
@ -800,9 +794,6 @@ public:
// 获取声音打开状态 // 获取声音打开状态
bool isSoundEnabled() const; bool isSoundEnabled() const;
// 获取 Easy2D 对象自动释放状态
bool isObjectsAutoReleaseEnabled() const;
// 获取碰撞监听状态 // 获取碰撞监听状态
bool isCollisionEnabled() const; bool isCollisionEnabled() const;
@ -820,7 +811,6 @@ protected:
protected: protected:
bool _unconfigured; bool _unconfigured;
bool _objectsAutoRelease;
bool _soundEnabled; bool _soundEnabled;
bool _collisionEnabled; bool _collisionEnabled;
bool _colliderVisiable; bool _colliderVisiable;
@ -830,4 +820,25 @@ protected:
}; };
} }
#ifndef __AUTORELEASE_T_DEFINED
#define __AUTORELEASE_T_DEFINED
namespace e2d
{
struct autorelease_t { };
extern autorelease_t const autorelease;
}
void* operator new(
size_t _Size,
e2d::autorelease_t const&
) E2D_NOEXCEPT;
void operator delete(
void* _Block,
e2d::autorelease_t const&
) E2D_NOEXCEPT;
#endif

View File

@ -167,15 +167,15 @@ private:
class Exception class Exception
{ {
public: public:
Exception() throw(); Exception() E2D_NOEXCEPT;
explicit Exception(const String& message) throw(); explicit Exception(const String& message) E2D_NOEXCEPT;
Exception(Exception const& other) throw(); Exception(Exception const& other) E2D_NOEXCEPT;
virtual ~Exception() throw(); virtual ~Exception() E2D_NOEXCEPT;
Exception& operator=(Exception const& other) throw(); Exception& operator=(Exception const& other) E2D_NOEXCEPT;
// 获取异常信息 // 获取异常信息
virtual String msg() const; virtual String msg() const;
@ -190,9 +190,9 @@ class SystemException
: public Exception : public Exception
{ {
public: public:
SystemException() throw(); SystemException() E2D_NOEXCEPT;
explicit SystemException(const String& message) throw(); explicit SystemException(const String& message) E2D_NOEXCEPT;
}; };
} }

View File

@ -89,6 +89,13 @@
#endif #endif
#if _MSC_VER >= 1900
# define E2D_NOEXCEPT noexcept
#else
# define E2D_NOEXCEPT throw()
#endif
#if _MSC_VER >= 1800 #if _MSC_VER >= 1800
# define E2D_DISABLE_COPY(Class) \ # define E2D_DISABLE_COPY(Class) \
Class(const Class &) = delete; \ Class(const Class &) = delete; \
@ -106,4 +113,4 @@
#ifdef min #ifdef min
# undef min # undef min
#endif #endif

View File

@ -34,6 +34,3 @@
#else #else
# pragma comment(lib, "Easy2Dw.lib") # pragma comment(lib, "Easy2Dw.lib")
#endif #endif
using namespace e2d;