From bf11282ccd5c04294d086cb161be912ea851d729 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 21 Mar 2019 14:14:11 +0800 Subject: [PATCH] add Object dumps functions --- easy2d/2d/ActionManager.cpp | 3 +- easy2d/2d/ActionManager.h | 4 +- easy2d/base/EventDispatcher.cpp | 3 +- easy2d/base/EventDispatcher.h | 2 +- easy2d/base/Object.cpp | 26 ++++++++- easy2d/base/Object.h | 18 +++++-- easy2d/base/logs.h | 24 ++++----- easy2d/common/Json.h | 96 ++++++++++++++++++++------------- easy2d/common/String.h | 57 +++++++++++++++++--- 9 files changed, 165 insertions(+), 68 deletions(-) diff --git a/easy2d/2d/ActionManager.cpp b/easy2d/2d/ActionManager.cpp index 6db8438f..96740662 100644 --- a/easy2d/2d/ActionManager.cpp +++ b/easy2d/2d/ActionManager.cpp @@ -41,7 +41,7 @@ namespace easy2d } } - void ActionManager::AddAction(ActionPtr const& action) + ActionPtr ActionManager::AddAction(ActionPtr const& action) { E2D_ASSERT(action && "AddAction failed, NULL pointer exception"); @@ -49,6 +49,7 @@ namespace easy2d { actions_.PushBack(action); } + return action; } ActionPtr ActionManager::GetAction(String const & name) diff --git a/easy2d/2d/ActionManager.h b/easy2d/2d/ActionManager.h index d1ff8cee..07c5290f 100644 --- a/easy2d/2d/ActionManager.h +++ b/easy2d/2d/ActionManager.h @@ -28,8 +28,8 @@ namespace easy2d using Actions = IntrusiveList; public: - // 执行动作 - void AddAction( + // 添加动作 + ActionPtr AddAction( ActionPtr const& action ); diff --git a/easy2d/base/EventDispatcher.cpp b/easy2d/base/EventDispatcher.cpp index 981f00d2..9fff4452 100644 --- a/easy2d/base/EventDispatcher.cpp +++ b/easy2d/base/EventDispatcher.cpp @@ -40,7 +40,7 @@ namespace easy2d } } - void EventDispatcher::AddListener(EventListenerPtr const & listener) + EventListenerPtr EventDispatcher::AddListener(EventListenerPtr const & listener) { E2D_ASSERT(listener && "AddListener failed, NULL pointer exception"); @@ -48,6 +48,7 @@ namespace easy2d { listeners_.PushBack(listener); } + return listener; } void EventDispatcher::AddListener(UINT type, EventCallback callback, String const& name) diff --git a/easy2d/base/EventDispatcher.h b/easy2d/base/EventDispatcher.h index 6f882fcc..4b5d9c83 100644 --- a/easy2d/base/EventDispatcher.h +++ b/easy2d/base/EventDispatcher.h @@ -29,7 +29,7 @@ namespace easy2d public: // 添加监听器 - void AddListener( + EventListenerPtr AddListener( EventListenerPtr const& listener ); diff --git a/easy2d/base/Object.cpp b/easy2d/base/Object.cpp index ceb62ac3..3dbe186f 100644 --- a/easy2d/base/Object.cpp +++ b/easy2d/base/Object.cpp @@ -19,19 +19,24 @@ // THE SOFTWARE. #include "Object.h" +#include "logs.h" +#include namespace easy2d { namespace { - bool tracing_leaks = true; + bool tracing_leaks = false; Array tracing_objects; } + unsigned int Object::last_object_id = 0; + Object::Object() : tracing_leak_(false) , user_data_(nullptr) , name_(nullptr) + , id_(++last_object_id) { #ifdef E2D_DEBUG @@ -83,6 +88,13 @@ namespace easy2d *name_ = name; } + String Object::DumpObject() + { + String name = typeid(*this).name(); + return String::format(L"{ class=\"%s\" id=%d refcount=%d name=\"%s\" }", + name.c_str(), GetObjectID(), GetRefCount(), GetName().c_str()); + } + void Object::StartTracingLeaks() { tracing_leaks = true; @@ -93,7 +105,17 @@ namespace easy2d tracing_leaks = false; } - Array const& easy2d::Object::__GetTracingObjects() + void Object::DumpTracingObjects() + { + E2D_LOG(L"-------------------------- All Objects --------------------------"); + for (const auto object : tracing_objects) + { + E2D_LOG(object->DumpObject().c_str()); + } + E2D_LOG(L"------------------------- Total size: %d -------------------------", tracing_objects.size()); + } + + Array& easy2d::Object::__GetTracingObjects() { return tracing_objects; } diff --git a/easy2d/base/Object.h b/easy2d/base/Object.h index 9a76f13b..82e936af 100644 --- a/easy2d/base/Object.h +++ b/easy2d/base/Object.h @@ -43,17 +43,24 @@ namespace easy2d void SetName(String const& name); - inline String GetName() const { if (name_) return *name_; return String(); } + inline String GetName() const { if (name_) return *name_; return String(); } - inline bool IsName(String const& name) const { return (name_ && (*name_ == name)) || (name.empty() && !name_); } + inline bool IsName(String const& name) const { return name_ ? (*name_ == name) : name.empty(); } + inline unsigned int GetObjectID() const { return id_; } + + String DumpObject(); + + public: static void StartTracingLeaks(); static void StopTracingLeaks(); - static Array const& __GetTracingObjects(); + static void DumpTracingObjects(); + + public: + static Array& __GetTracingObjects(); - protected: static void __AddObjectToTracingList(Object*); static void __RemoveObjectFromTracingList(Object*); @@ -62,5 +69,8 @@ namespace easy2d bool tracing_leak_; void* user_data_; String* name_; + + const unsigned int id_; + static unsigned int last_object_id; }; } diff --git a/easy2d/base/logs.h b/easy2d/base/logs.h index 96240d75..c1ced9f2 100644 --- a/easy2d/base/logs.h +++ b/easy2d/base/logs.h @@ -93,56 +93,56 @@ namespace easy2d inline void Print(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - Output(std::wcout, stdout_white, nullptr, format, std::forward<_Args&&>(args)...); + Output(std::wcout, stdout_white, nullptr, format, std::forward<_Args>(args)...); } template inline void Println(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - OutputLine(std::wcout, stdout_white, nullptr, format, std::forward<_Args&&>(args)...); + OutputLine(std::wcout, stdout_white, nullptr, format, std::forward<_Args>(args)...); } template inline void Message(const wchar_t * format, _Args&&... args) const { using namespace __console_colors; - Output(std::wcout, stdout_blue, nullptr, format, std::forward<_Args&&>(args)...); + Output(std::wcout, stdout_blue, nullptr, format, std::forward<_Args>(args)...); } template inline void Messageln(const wchar_t * format, _Args&&... args) const { using namespace __console_colors; - OutputLine(std::wcout, stdout_blue, nullptr, format, std::forward<_Args&&>(args)...); + OutputLine(std::wcout, stdout_blue, nullptr, format, std::forward<_Args>(args)...); } template inline void Warning(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - Output(std::wcerr, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args&&>(args)...); + Output(std::wcerr, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args>(args)...); } template inline void Warningln(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - OutputLine(std::wcerr, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args&&>(args)...); + OutputLine(std::wcerr, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args>(args)...); } template inline void Error(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - Output(std::wcerr, stderr_red_bg, L"Error: ", format, std::forward<_Args&&>(args)...); + Output(std::wcerr, stderr_red_bg, L"Error: ", format, std::forward<_Args>(args)...); } template inline void Errorln(const wchar_t* format, _Args&&... args) const { using namespace __console_colors; - OutputLine(std::wcerr, stderr_red_bg, L"Error: ", format, std::forward<_Args&&>(args)...); + OutputLine(std::wcerr, stderr_red_bg, L"Error: ", format, std::forward<_Args>(args)...); } private: @@ -154,7 +154,7 @@ namespace easy2d if (!enabled_) return; - Output(os, color, prompt, format, std::forward<_Args&&>(args)...); + Output(os, color, prompt, format, std::forward<_Args>(args)...); os << std::endl; ::OutputDebugStringW(L"\r\n"); @@ -166,7 +166,7 @@ namespace easy2d if (!enabled_) return; - std::wstring output = MakeOutputString(prompt, format, std::forward<_Args&&>(args)...); + std::wstring output = MakeOutputString(prompt, format, std::forward<_Args>(args)...); os << color << output; ::OutputDebugStringW(output.c_str()); @@ -179,8 +179,8 @@ namespace easy2d { static wchar_t temp_buffer[1024 * 3 + 1]; - const auto len = ::_scwprintf(format, std::forward<_Args&&>(args)...); - ::swprintf_s(temp_buffer, len + 1, format, std::forward<_Args&&>(args)...); + const auto len = ::_scwprintf(format, std::forward<_Args>(args)...); + ::swprintf_s(temp_buffer, len + 1, format, std::forward<_Args>(args)...); std::wstringstream ss; ss << Logger::OutPrefix; diff --git a/easy2d/common/Json.h b/easy2d/common/Json.h index 2557bae7..dddbcd25 100644 --- a/easy2d/common/Json.h +++ b/easy2d/common/Json.h @@ -337,7 +337,7 @@ namespace easy2d inline JsonValue& operator=(JsonValue && other) { - JsonValue{ std::forward(other) }.swap(*this); + JsonValue{ std::forward(other) }.swap(*this); return (*this); } }; @@ -355,8 +355,7 @@ namespace easy2d template < typename _CompatibleTy, - enable_if_t::value, int> = 0 - > + enable_if_t::value, int> = 0> basic_json(const _CompatibleTy& value) { value_.type = JsonType::String; @@ -461,37 +460,6 @@ namespace easy2d return string_type(); } - inline size_type size() const - { - switch (type()) - { - case JsonType::Null: - return 0; - case JsonType::Array: - return value_.data.vector->size(); - case JsonType::Object: - return value_.data.object->size(); - default: - return 1; - } - } - - inline bool empty() const - { - if (is_null()) - return true; - - if (is_object()) - return value_.data.object->empty(); - - if (is_array()) - return value_.data.vector->empty(); - - return false; - } - - inline void clear() { value_ = nullptr; } - inline void swap(basic_json& rhs) { value_.swap(rhs.value_); } public: @@ -693,9 +661,9 @@ namespace easy2d public: // operator= functions - inline basic_json& operator=(basic_json other) + inline basic_json& operator=(basic_json const& other) { - other.swap(*this); + value_ = other.value_; return (*this); } @@ -770,7 +738,7 @@ namespace easy2d { throw json_invalid_key(); } - return iter.second; + return iter->second; } template @@ -801,7 +769,7 @@ namespace easy2d { throw json_invalid_key(); } - return iter.second; + return iter->second; } public: @@ -862,6 +830,8 @@ namespace easy2d template struct iterator_impl { + friend _Ty; + using value_type = _Ty; using difference_type = std::ptrdiff_t; using iterator_category = std::bidirectional_iterator_tag; @@ -1147,6 +1117,56 @@ namespace easy2d inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } inline const_reverse_iterator crend() const { return rend(); } + public: + inline size_type size() const + { + switch (type()) + { + case JsonType::Null: + return 0; + case JsonType::Array: + return value_.data.vector->size(); + case JsonType::Object: + return value_.data.object->size(); + default: + return 1; + } + } + + inline bool empty() const + { + if (is_null()) + return true; + + if (is_object()) + return value_.data.object->empty(); + + if (is_array()) + return value_.data.vector->empty(); + + return false; + } + + template + inline const_iterator find(_Kty && key) const + { + if (is_object()) + { + const_iterator iter; + iter.it_.object_iter = value_.data.object->find(std::forward<_Kty>(key)); + return iter; + } + return cend(); + } + + template + inline size_type count(_Kty && key) const + { + return is_object() ? value_.data.object->count(std::forward<_Kty>(key)) : 0; + } + + inline void clear() { value_ = nullptr; } + public: // compare functions diff --git a/easy2d/common/String.h b/easy2d/common/String.h index aa653d5f..539282c1 100644 --- a/easy2d/common/String.h +++ b/easy2d/common/String.h @@ -190,6 +190,20 @@ namespace easy2d void swap(String& rhs); size_t hash() const; + public: + static String parse(int val); + static String parse(unsigned int val); + static String parse(long val); + static String parse(unsigned long val); + static String parse(long long val); + static String parse(unsigned long long val); + static String parse(float val); + static String parse(double val); + static String parse(long double val); + + template + static String format(const wchar_t* const fmt, _Args&&... args); + public: inline iterator begin() { check_operability(); return iterator(str_); } inline const_iterator begin() const { return const_iterator(const_str_); } @@ -229,9 +243,6 @@ namespace easy2d inline String& operator=(String const& rhs) { if (this != &rhs) String{ rhs }.swap(*this); return *this; } inline String& operator=(String && rhs) { if (this != &rhs) String{ rhs }.swap(*this); return *this; } - inline bool operator!=(String const& rhs) { return compare(rhs) != 0; } - inline bool operator!=(const wchar_t* cstr) { return compare(cstr) != 0; } - public: static const String::size_type npos = static_cast(-1); @@ -299,8 +310,20 @@ namespace easy2d // inline bool operator==(String const& lhs, String const& rhs) { return lhs.compare(rhs) == 0; } - inline bool operator==(const wchar_t* lhs, String const& rhs) { return rhs.compare(rhs) == 0; } + inline bool operator==(const wchar_t* lhs, String const& rhs) { return rhs.compare(lhs) == 0; } inline bool operator==(String const& lhs, const wchar_t* rhs) { return lhs.compare(rhs) == 0; } + inline bool operator==(const char* lhs, String const& rhs) { return rhs.compare(String(lhs)) == 0; } + inline bool operator==(String const& lhs, const char* rhs) { return lhs.compare(String(rhs)) == 0; } + + // + // operator!= for String + // + + inline bool operator!=(String const& lhs, String const& rhs) { return lhs.compare(rhs) != 0; } + inline bool operator!=(const wchar_t* lhs, String const& rhs) { return rhs.compare(lhs) != 0; } + inline bool operator!=(String const& lhs, const wchar_t* rhs) { return lhs.compare(rhs) != 0; } + inline bool operator!=(const char* lhs, String const& rhs) { return rhs.compare(String(lhs)) != 0; } + inline bool operator!=(String const& lhs, const char* rhs) { return lhs.compare(String(rhs)) != 0; } // // operator+ for String @@ -343,7 +366,7 @@ namespace easy2d // template - inline String format_wstring(const wchar_t* const fmt, _Args&&... args); + String format_wstring(const wchar_t* const fmt, _Args&&... args); } namespace easy2d @@ -1030,6 +1053,26 @@ namespace easy2d } } + // + // details of String::parese + // + + inline String String::parse(int val) { return ::easy2d::to_wstring(val); } + inline String String::parse(unsigned int val) { return ::easy2d::to_wstring(val); } + inline String String::parse(long val) { return ::easy2d::to_wstring(val); } + inline String String::parse(unsigned long val) { return ::easy2d::to_wstring(val); } + inline String String::parse(long long val) { return ::easy2d::to_wstring(val); } + inline String String::parse(unsigned long long val) { return ::easy2d::to_wstring(val); } + inline String String::parse(float val) { return ::easy2d::to_wstring(val); } + inline String String::parse(double val) { return ::easy2d::to_wstring(val); } + inline String String::parse(long double val) { return ::easy2d::to_wstring(val); } + + template + inline String String::format(const wchar_t* const fmt, _Args&&... args) + { + return ::easy2d::format_wstring(fmt, std::forward<_Args>(args)...); + } + // // details of operator<<>> // @@ -1151,11 +1194,11 @@ namespace easy2d template inline String format_wstring(const wchar_t* const fmt, _Args&&... args) { - const auto len = static_cast(::_scwprintf(fmt, std::forward<_Args&&>(args)...)); + const auto len = static_cast(::_scwprintf(fmt, std::forward<_Args>(args)...)); if (len) { String str(len, L'\0'); - ::swprintf_s(&str[0], len + 1, fmt, std::forward<_Args&&>(args)...); + ::swprintf_s(&str[0], len + 1, fmt, std::forward<_Args>(args)...); return str; } return String{};