add E2D_ASSERT

This commit is contained in:
Haibo 2018-11-22 23:48:40 +08:00 committed by Nomango
parent 406ea51b57
commit a29584f756
12 changed files with 51 additions and 49 deletions

View File

@ -32,8 +32,7 @@ namespace easy2d
, times_(0)
, total_times_(times)
{
if (!action)
logs::Warningln("Loop action contains a null action");
E2D_ASSERT(action && "Loop action contains a null action");
action_ = action;
}

View File

@ -43,8 +43,7 @@ namespace easy2d
void ActionManager::AddAction(spAction const& action)
{
if (!action)
logs::Warningln("AddAction failed, action is nullptr");
E2D_ASSERT(action && "AddAction failed, NULL pointer exception");
if (action)
{

View File

@ -42,8 +42,7 @@ namespace easy2d
void EventDispatcher::AddListener(spEventListener const & listener)
{
if (!listener)
logs::Warningln("AddListener failed, action is nullptr");
E2D_ASSERT(listener && "AddListener failed, NULL pointer exception");
if (listener)
{

View File

@ -57,8 +57,7 @@ namespace easy2d
void Frames::Add(spImage const& frame)
{
if (!frame)
logs::Warningln("Frames::Add failed, frame is nullptr.");
E2D_ASSERT(frame && "Frames::Add failed, NULL pointer exception");
if (frame)
{

View File

@ -156,8 +156,7 @@ namespace easy2d
void Game::EnterScene(spScene const & scene)
{
if (!scene)
logs::Warningln("Game::EnterScene failed, scene is nullptr");
E2D_ASSERT(scene && "Game::EnterScene failed, NULL pointer exception");
if (curr_scene_ == scene || next_scene_ == scene)
return;
@ -450,22 +449,13 @@ namespace easy2d
LRESULT CALLBACK Game::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
LRESULT result = 0;
bool was_handled = false;
Game * game = reinterpret_cast<Game*>(
static_cast<LONG_PTR>(::GetWindowLongW(hwnd, GWLP_USERDATA))
);
if (game)
{
was_handled = game->HandleMessage(hwnd, msg, wparam, lparam);
}
if (game && game->HandleMessage(hwnd, msg, wparam, lparam))
return 0;
if (!was_handled)
{
result = ::DefWindowProcW(hwnd, msg, wparam, lparam);
}
return result;
return ::DefWindowProcW(hwnd, msg, wparam, lparam);
}
}

View File

@ -71,7 +71,11 @@ namespace easy2d
if (geometry_ && geometry_->geo_)
{
auto graphics = Graphics::Instance();
graphics->SetTransform(geometry_->GetTransformMatrix() * GetTransformMatrix());
if (geometry_->GetTransformMatrix().IsIdentity())
graphics->SetTransform(GetTransformMatrix());
else
graphics->SetTransform(geometry_->GetTransformMatrix() * GetTransformMatrix());
graphics->FillGeometry(
geometry_->geo_,

View File

@ -388,8 +388,7 @@ namespace easy2d
void Node::AddChild(spNode const& child, int z_order)
{
if (!child)
logs::Warningln("Node::AddChild failed, child is nullptr");
E2D_ASSERT(child && "Node::AddChild failed, NULL pointer exception");
if (child)
{
@ -472,8 +471,7 @@ namespace easy2d
bool Node::RemoveChild(spNode const& child)
{
if (!child)
logs::Warningln("Node::RemoveChild failed, child is nullptr");
E2D_ASSERT(child && "Node::RemoveChild failed, NULL pointer exception");
if (children_.IsEmpty())
return false;

View File

@ -42,8 +42,7 @@ namespace easy2d
void TaskManager::AddTask(spTask const& task)
{
if (!task)
logs::Warningln("AddTask failed, task is nullptr");
E2D_ASSERT(task && "AddTask failed, NULL pointer exception");
if (task)
{

View File

@ -24,7 +24,6 @@
#include "ObjectBase.h"
#include "intrusive/SmartPointer.hpp"
#include "d2dres.hpp"
#include <assert.h>
#ifndef E2D_DECLARE_SMART_PTR
#define E2D_DECLARE_SMART_PTR(class_name)\
@ -92,4 +91,20 @@ namespace easy2d
E2D_DECLARE_NS_SMART_PTR(ui, Button);
E2D_DECLARE_NS_SMART_PTR(ui, Menu);
template <typename Dest, typename Src>
inline Dest* SafeCast(Src* ptr)
{
if (!ptr)
return nullptr;
#ifdef E2D_DEBUG
Dest* cast = dynamic_cast<Dest*>(ptr);
E2D_ASSERT(cast);
return cast;
#endif
return static_cast<Dest*>(ptr);
}
}

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include <stdexcept>
#include "../macros.h"
#include <functional>
#undef DEBUG_CHECK_LIST
@ -156,7 +156,7 @@ namespace easy2d
while (tmp != child)
{
if (tmp == last_)
throw std::logic_error("The node to be removed is not in this list");
E2D_ASSERT(false && "The node to be removed is not in this list");
tmp = tmp->next_;
}
#endif
@ -218,14 +218,14 @@ namespace easy2d
for (size_t i = 0; i < size; ++i)
{
if (i == 0)
temp_vec[i]->prev_ = ItemType();
temp_vec[i]->prev_ = nullptr;
else
{
temp_vec[i]->prev_ = temp_vec[i - 1];
temp_vec[i - 1]->next_ = temp_vec[i];
}
if (i == size - 1)
temp_vec[i]->next_ = ItemType();
temp_vec[i]->next_ = nullptr;
else
{
temp_vec[i]->next_ = temp_vec[i + 1];
@ -257,13 +257,11 @@ namespace easy2d
if (p)
{
if (p->prev_ != tmp)
throw std::logic_error("Check list failed");
E2D_ASSERT(p->prev_ == tmp && "Check list failed");
}
else
{
if (tmp != last_)
throw std::logic_error("Check list failed");
E2D_ASSERT(tmp == last_ && "Check list failed");
}
} while (p);
}

View File

@ -22,14 +22,6 @@
#include "../macros.h"
#include <utility>
#ifndef E2D_INTRUSIVE_PTR_ASSERT
# ifdef E2D_DEBUG
# define E2D_INTRUSIVE_PTR_ASSERT(expr, msg) do { if (!(expr)) throw std::runtime_error(msg); } while(0);
# else
# define E2D_INTRUSIVE_PTR_ASSERT __noop
# endif
#endif
namespace easy2d
{
namespace intrusive
@ -82,19 +74,19 @@ namespace easy2d
inline Type* operator ->() const
{
E2D_INTRUSIVE_PTR_ASSERT(ptr_ != nullptr, "Invalid pointer");
E2D_ASSERT(ptr_ != nullptr, "Invalid pointer");
return ptr_;
}
inline Type& operator *() const
{
E2D_INTRUSIVE_PTR_ASSERT(ptr_ != nullptr, "Invalid pointer");
E2D_ASSERT(ptr_ != nullptr, "Invalid pointer");
return *ptr_;
}
inline Type** operator &()
{
E2D_INTRUSIVE_PTR_ASSERT(ptr_ == nullptr, "Memory leak");
E2D_ASSERT(ptr_ == nullptr, "Memory leak");
return &ptr_;
}

View File

@ -82,6 +82,7 @@
#include <wincodec.h>
// C++ RunTime Header Files
#include <cstdlib>
#include <string>
#include <map>
#include <set>
@ -97,3 +98,12 @@
# define E2D_NOEXCEPT throw()
# define E2D_CONSTEXPR const
#endif
#ifndef E2D_ASSERT
# ifdef E2D_DEBUG
# define E2D_ASSERT(expr) if (!(expr)) { ::OutputDebugStringA("[easy2d] Assert failed: " #expr "\n"); abort(); }
# else
# define E2D_ASSERT __noop
# endif
#endif