From cd2c112a980c1eccb74bbd14266b2c7adc2f1ae3 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 21 Mar 2019 15:18:05 +0800 Subject: [PATCH] throw exceptions if load DLLs failed minor fixes --- easy2d-audio/audio-modules.cpp | 15 +- easy2d-audio/audio-modules.h | 4 +- easy2d/base/window.h | 1 + easy2d/common/Json.h | 72 --------- easy2d/common/String.h | 260 +++++++++++++++++++------------- easy2d/platform/Application.cpp | 3 +- easy2d/platform/Application.h | 5 +- easy2d/platform/modules.cpp | 3 +- easy2d/platform/modules.h | 2 +- easy2d/utils/Path.cpp | 30 +--- easy2d/utils/Path.h | 3 - 11 files changed, 175 insertions(+), 223 deletions(-) diff --git a/easy2d-audio/audio-modules.cpp b/easy2d-audio/audio-modules.cpp index 9c4e72d7..983f9aa5 100644 --- a/easy2d-audio/audio-modules.cpp +++ b/easy2d-audio/audio-modules.cpp @@ -29,9 +29,9 @@ namespace easy2d { const auto xaudio2_dll_names = { - L"xaudio2_9.dll", - L"xaudio2_8.dll", - L"xaudio2_7.dll" + L"xaudio2_9.dll", // for Windows 10 + L"xaudio2_8.dll", // for Windows 8 + L"xaudio2_7.dll" // for DirectX SDK }; for (const auto& name : xaudio2_dll_names) @@ -47,7 +47,8 @@ namespace easy2d if (!xaudio2) { - E2D_LOG(L"load xaudio2.dll failed"); + E2D_ERROR_LOG(L"Load xaudio2.dll failed"); + throw std::runtime_error("Load xaudio2.dll failed"); } } @@ -73,7 +74,8 @@ namespace easy2d } else { - E2D_LOG(L"load Mfplat.dll failed"); + E2D_LOG(L"Load Mfplat.dll failed"); + throw std::runtime_error("Load Mfplat.dll failed"); } mfreadwrite = LoadLibraryW(L"Mfreadwrite.dll"); @@ -87,7 +89,8 @@ namespace easy2d } else { - E2D_LOG(L"load Mfreadwrite.dll failed"); + E2D_LOG(L"Load Mfreadwrite.dll failed"); + throw std::runtime_error("Load Mfreadwrite.dll failed"); } } } diff --git a/easy2d-audio/audio-modules.h b/easy2d-audio/audio-modules.h index 872d12e5..059d1535 100644 --- a/easy2d-audio/audio-modules.h +++ b/easy2d-audio/audio-modules.h @@ -38,7 +38,7 @@ namespace easy2d typedef HRESULT(WINAPI *PFN_XAudio2Create)(IXAudio2**, UINT32, XAUDIO2_PROCESSOR); public: - static XAudio2& Get() + static inline XAudio2& Get() { static XAudio2 instance; return instance; @@ -65,7 +65,7 @@ namespace easy2d typedef HRESULT(WINAPI *PFN_MFCreateMFByteStreamOnStream)(IStream*, IMFByteStream**); public: - static MediaFoundation& Get() + static inline MediaFoundation& Get() { static MediaFoundation instance; return instance; diff --git a/easy2d/base/window.h b/easy2d/base/window.h index fefba623..18c4fe7f 100644 --- a/easy2d/base/window.h +++ b/easy2d/base/window.h @@ -19,6 +19,7 @@ // THE SOFTWARE. #pragma once +#include "../macros.h" #include "../common/helper.h" #include "../math/helper.h" diff --git a/easy2d/common/Json.h b/easy2d/common/Json.h index dddbcd25..df59d7f5 100644 --- a/easy2d/common/Json.h +++ b/easy2d/common/Json.h @@ -1216,35 +1216,11 @@ namespace easy2d return false; } - template ::value, int> = 0> - friend bool operator==(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs == basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator==(const _ScalarTy lhs, const basic_json& rhs) - { - return basic_json(lhs) == rhs; - } - friend bool operator!=(const basic_json& lhs, const basic_json& rhs) { return !(lhs == rhs); } - template ::value, int> = 0> - friend bool operator!=(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs != basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator!=(const _ScalarTy lhs, const basic_json& rhs) - { - return rhs != basic_json(lhs); - } - friend bool operator<(const basic_json& lhs, const basic_json& rhs) { const auto lhs_type = lhs.type(); @@ -1291,69 +1267,21 @@ namespace easy2d return false; } - template ::value, int> = 0> - friend bool operator<(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs < basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator<(const _ScalarTy lhs, const basic_json& rhs) - { - return basic_json(lhs) < rhs; - } - friend bool operator<=(const basic_json& lhs, const basic_json& rhs) { return !(rhs < lhs); } - template ::value, int> = 0> - friend bool operator<=(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs <= basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator<=(const _ScalarTy lhs, const basic_json& rhs) - { - return basic_json(lhs) <= rhs; - } - friend bool operator>(const basic_json& lhs, const basic_json& rhs) { return rhs < lhs; } - template ::value, int> = 0> - friend bool operator>(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs > basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator>(const _ScalarTy lhs, const basic_json& rhs) - { - return basic_json(lhs) > rhs; - } - friend bool operator>=(const basic_json& lhs, const basic_json& rhs) { return !(lhs < rhs); } - template ::value, int> = 0> - friend bool operator>=(const basic_json& lhs, const _ScalarTy rhs) - { - return lhs >= basic_json(rhs); - } - - template ::value, int> = 0> - friend bool operator>=(const _ScalarTy lhs, const basic_json& rhs) - { - return basic_json(lhs) >= rhs; - } - public: // output_adapter diff --git a/easy2d/common/String.h b/easy2d/common/String.h index 539282c1..273cbca0 100644 --- a/easy2d/common/String.h +++ b/easy2d/common/String.h @@ -19,10 +19,11 @@ // THE SOFTWARE. #pragma once -#include "../macros.h" #include +#include #include #include +#include namespace easy2d { @@ -91,7 +92,7 @@ namespace easy2d using const_iterator = iterator_impl; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - using traits = std::char_traits; + using char_traits = std::char_traits; using allocator = std::allocator; String(); @@ -110,6 +111,7 @@ namespace easy2d String(_Iter first, _Iter last) : String() { assign_iter(first, last); } inline const wchar_t* c_str() const { return const_str_ ? const_str_ : L""; } + inline const wchar_t* data() const { return const_str_ ? const_str_ : L""; } inline wchar_t at(size_t i) const { return (*this)[i]; } inline size_type size() const { return size_; } inline size_type length() const { return size(); } @@ -127,31 +129,31 @@ namespace easy2d String& append(size_type count, wchar_t ch); String& append(const wchar_t* cstr, size_type count); String& append(String const& other, size_type pos, size_type count = npos); - inline String& append(const wchar_t* cstr) { return append(cstr, traits::length(cstr)); } + inline String& append(const wchar_t* cstr) { return append(cstr, char_traits::length(cstr)); } inline String& append(std::wstring const& str) { return append(str.c_str()); } inline String& append(String const& other) { return append(other.const_str_, 0, npos); } size_type find(const wchar_t ch, size_type offset = 0) const; size_type find(const wchar_t* const str, size_type offset, size_type count) const; inline size_type find(String const& str, size_type offset = 0) const { return find(str.c_str(), offset, str.size()); } - inline size_type find(const wchar_t* const str, size_type offset = 0) const { return find(str, offset, traits::length(str)); } + inline size_type find(const wchar_t* const str, size_type offset = 0) const { return find(str, offset, char_traits::length(str)); } size_type find_first_of(const wchar_t* const str, size_type offset, size_type count) const; inline size_type find_first_of(const wchar_t ch, size_type offset = 0) const { return find(ch, offset); } inline size_type find_first_of(String const& str, size_type offset = 0) const { return find_first_of(str.c_str(), offset, str.size()); } - inline size_type find_first_of(const wchar_t* const str, size_type offset = 0) const { return find_first_of(str, offset, traits::length(str)); } + inline size_type find_first_of(const wchar_t* const str, size_type offset = 0) const { return find_first_of(str, offset, char_traits::length(str)); } size_type find_last_of(const wchar_t ch, size_type pos = npos) const; size_type find_last_of(const wchar_t* const str, size_type pos, size_type count) const; inline size_type find_last_of(String const& str, size_type pos = npos) const { return find_first_of(str.c_str(), pos, str.size()); } - inline size_type find_last_of(const wchar_t* const str, size_type pos = npos) const { return find_first_of(str, pos, traits::length(str)); } + inline size_type find_last_of(const wchar_t* const str, size_type pos = npos) const { return find_first_of(str, pos, char_traits::length(str)); } String& replace(size_type pos, size_type count, const wchar_t* cstr, size_type count2); String& replace(size_type pos, size_type count, size_type count2, const wchar_t ch); inline String& replace(size_type pos, size_type count, const String& str) { return replace(pos, count, str.c_str(), str.size()); } - inline String& replace(size_type pos, size_type count, const wchar_t* cstr) { return replace(pos, count, cstr, traits::length(cstr)); } + inline String& replace(size_type pos, size_type count, const wchar_t* cstr) { return replace(pos, count, cstr, char_traits::length(cstr)); } inline String& replace(const_iterator first, const_iterator last, const String& str) { return replace(first, last, str.c_str(), str.size()); } - inline String& replace(const_iterator first, const_iterator last, const wchar_t* cstr) { return replace(first, last, cstr, traits::length(cstr)); } + inline String& replace(const_iterator first, const_iterator last, const wchar_t* cstr) { return replace(first, last, cstr, char_traits::length(cstr)); } inline String& replace(const_iterator first, const_iterator last, const wchar_t* cstr, size_type count){ return replace(first - cbegin(), last - first, cstr, count); } inline String& replace(const_iterator first, const_iterator last, size_type count2, const wchar_t ch) { return replace(first - cbegin(), last - first, count2, ch); } @@ -174,7 +176,7 @@ namespace easy2d String& insert(size_type index, size_type count, wchar_t ch); String& insert(size_type index, const wchar_t* s, size_type count); String& insert(size_type index, const String& str, size_type off, size_type count = npos); - inline String& insert(size_type index, const wchar_t* s) { return insert(index, s, traits::length(s)); } + inline String& insert(size_type index, const wchar_t* s) { return insert(index, s, char_traits::length(s)); } inline String& insert(size_type index, const String& str) { return insert(index, str, 0, str.size()); } inline iterator insert(const_iterator pos, size_type count, wchar_t ch) { size_type off = pos - cbegin(); insert(off, count, ch); return begin().base() + off; } inline iterator insert(const_iterator pos, wchar_t ch) { return insert(pos, 1, ch); } @@ -281,9 +283,9 @@ namespace easy2d for (size_type index = 0; first != last; ++first, ++index) { - traits::assign(str_[index], traits::to_char_type(*first)); + char_traits::assign(str_[index], char_traits::to_char_type(*first)); } - traits::assign(str_[size_], value_type()); + char_traits::assign(str_[size_], value_type()); } private: @@ -344,8 +346,8 @@ namespace easy2d // operator<<>> for String // - std::basic_ostream& operator<<(std::basic_ostream& os, const String & str); - std::basic_istream& operator>>(std::basic_istream& is, String & str); + std::basic_ostream& operator<<(std::basic_ostream& os, const String & str); + std::basic_istream& operator>>(std::basic_istream& is, String & str); // // to_string functions @@ -450,12 +452,12 @@ namespace easy2d if (operable_) { - assign(cstr, traits::length(cstr)); + assign(cstr, char_traits::length(cstr)); } else { const_str_ = cstr; - size_ = traits::length(cstr); + size_ = char_traits::length(cstr); capacity_ = size_; } } @@ -475,19 +477,18 @@ namespace easy2d inline String::String(const char * cstr) : String() { - if (cstr) + if (cstr && cstr[0]) { - int len = ::MultiByteToWideChar(CP_ACP, 0, cstr, -1, nullptr, 0); - if (len) + size_t len; + errno_t ret = ::mbstowcs_s(&len, nullptr, 0, cstr, 0); + if (!ret) { - wchar_t* temp = allocate(len + 1); - temp[0] = 0; + str_ = allocate(len); + str_[0] = 0; - if (::MultiByteToWideChar(CP_ACP, 0, cstr, -1, temp, len + 1)) - { - str_ = temp; - capacity_ = size_ = static_cast(len); - } + ::mbstowcs_s(nullptr, str_, len, cstr, len - 1); + + capacity_ = size_ = static_cast(len - 1); } } } @@ -538,8 +539,8 @@ namespace easy2d } size_ = count; - traits::assign(str_, count, ch); - traits::assign(str_[size_], value_type()); + char_traits::assign(str_, count, ch); + char_traits::assign(str_[size_], value_type()); } else @@ -563,8 +564,8 @@ namespace easy2d } size_ = count; - traits::move(str_, cstr, size_); - traits::assign(str_[size_], value_type()); + char_traits::move(str_, cstr, size_); + char_traits::assign(str_[size_], value_type()); } else { @@ -591,7 +592,7 @@ namespace easy2d size_type new_size = size_ - count; iterator erase_at = begin().base() + offset; - traits::move(erase_at.base(), erase_at.base() + count, new_size - offset + 1); + char_traits::move(erase_at.base(), erase_at.base() + count, new_size - offset + 1); return (*this); } @@ -618,18 +619,18 @@ namespace easy2d wchar_t* new_ptr = allocate(capacity_ + 1); wchar_t* const insert_at = new_ptr + index; - traits::move(new_ptr, old_ptr, index); // (0) - (index) - traits::assign(insert_at, count, ch); // (index) - (index + count) - traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) + char_traits::move(new_ptr, old_ptr, index); // (0) - (index) + char_traits::assign(insert_at, count, ch); // (index) - (index + count) + char_traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) - deallocate(str_, old_capacity); + deallocate(str_, old_capacity + 1); str_ = new_ptr; } else { wchar_t* const insert_at = old_ptr + index; - traits::move(insert_at + count, old_ptr + index, suffix_size); - traits::assign(insert_at, count, ch); + char_traits::move(insert_at + count, old_ptr + index, suffix_size); + char_traits::assign(insert_at, count, ch); } return (*this); @@ -658,18 +659,18 @@ namespace easy2d wchar_t* new_ptr = allocate(capacity_ + 1); wchar_t* const insert_at = new_ptr + index; - traits::move(new_ptr, old_ptr, index); // (0) - (index) - traits::move(insert_at, cstr, count); // (index) - (index + count) - traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) + char_traits::move(new_ptr, old_ptr, index); // (0) - (index) + char_traits::move(insert_at, cstr, count); // (index) - (index + count) + char_traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) - deallocate(str_, old_capacity); + deallocate(str_, old_capacity + 1); str_ = new_ptr; } else { wchar_t* const insert_at = old_ptr + index; - traits::move(insert_at + count, old_ptr + index, suffix_size); - traits::move(insert_at, cstr, count); + char_traits::move(insert_at + count, old_ptr + index, suffix_size); + char_traits::move(insert_at, cstr, count); } return (*this); @@ -700,18 +701,18 @@ namespace easy2d wchar_t* new_ptr = allocate(capacity_ + 1); wchar_t* const insert_at = new_ptr + index; - traits::move(new_ptr, old_ptr, index); // (0) - (index) - traits::move(insert_at, str.begin().base() + off, count); // (index) - (index + count) - traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) + char_traits::move(new_ptr, old_ptr, index); // (0) - (index) + char_traits::move(insert_at, str.begin().base() + off, count); // (index) - (index + count) + char_traits::move(insert_at + count, old_ptr + index, suffix_size); // (index + count) - (old_size - index) - deallocate(str_, old_capacity); + deallocate(str_, old_capacity + 1); str_ = new_ptr; } else { wchar_t* const insert_at = old_ptr + index; - traits::move(insert_at + count, old_ptr + index, suffix_size); - traits::move(insert_at, str.begin().base() + off, count); + char_traits::move(insert_at + count, old_ptr + index, suffix_size); + char_traits::move(insert_at, str.begin().base() + off, count); } return (*this); @@ -725,9 +726,9 @@ namespace easy2d size_t new_cap = new_size + 1; wchar_t* new_str = allocate(new_cap); - traits::move(new_str, str_, size_); - traits::assign(new_str + size_, count, ch); - traits::assign(new_str[new_size], value_type()); + char_traits::move(new_str, str_, size_); + char_traits::assign(new_str + size_, count, ch); + char_traits::assign(new_str[new_size], value_type()); destroy(); @@ -745,9 +746,9 @@ namespace easy2d size_t new_cap = new_size + 1; wchar_t* new_str = allocate(new_cap); - traits::move(new_str, str_, size_); - traits::move(new_str + size_, cstr, count); - traits::assign(new_str[new_size], value_type()); + char_traits::move(new_str, str_, size_); + char_traits::move(new_str + size_, cstr, count); + char_traits::assign(new_str[new_size], value_type()); destroy(); @@ -770,9 +771,9 @@ namespace easy2d size_t new_cap = new_size + 1; wchar_t* new_str = allocate(new_cap); - traits::move(new_str, str_, size_); - traits::move(new_str + size_, other.begin().base() + pos, count); - traits::assign(new_str[new_size], value_type()); + char_traits::move(new_str, str_, size_); + char_traits::move(new_str + size_, other.begin().base() + pos, count); + char_traits::assign(new_str[new_size], value_type()); destroy(); @@ -790,7 +791,7 @@ namespace easy2d check_operability(); wchar_t* new_str = allocate(new_cap); - traits::move(new_str, str_, capacity_); + char_traits::move(new_str, str_, capacity_); destroy(); @@ -814,10 +815,10 @@ namespace easy2d inline int String::compare(const wchar_t * const str) const { size_type count1 = size(); - size_type count2 = traits::length(str); + size_type count2 = char_traits::length(str); size_type rlen = std::min(count1, count2); - int ret = traits::compare(const_str_, str, rlen); + int ret = char_traits::compare(const_str_, str, rlen); if (ret != 0) return ret; @@ -835,7 +836,7 @@ namespace easy2d if (offset >= size_) return String::npos; - const_iterator citer = traits::find(cbegin().base() + offset, size_, ch); + const_iterator citer = char_traits::find(cbegin().base() + offset, size_, ch); return citer ? (citer - cbegin()) : String::npos; } @@ -843,7 +844,7 @@ namespace easy2d { if (offset >= size_) return String::npos; - return __string_details::TraitsFind(const_str_, size_, offset, str, count); + return __string_details::TraitsFind(const_str_, size_, offset, str, count); } inline String::size_type String::find_first_of(const wchar_t * const str, size_type offset, size_type count) const @@ -869,7 +870,7 @@ namespace easy2d if (pos == 0 || pos > size_ || pos == npos) return npos; - return __string_details::TraitsFindLastOf(const_str_, size_, pos, str, count); + return __string_details::TraitsFindLastOf(const_str_, size_, pos, str, count); } inline String & String::replace(size_type pos, size_type count, const wchar_t * cstr, size_type count2) @@ -880,7 +881,7 @@ namespace easy2d count = clamp_suffix_size(pos, count); if (count == count2) { - traits::move(str_ + pos, cstr, count2); + char_traits::move(str_ + pos, cstr, count2); return (*this); } @@ -897,7 +898,7 @@ namespace easy2d capacity_ = size_; new_ptr = allocate(capacity_ + 1); - traits::move(new_ptr, old_ptr, pos); + char_traits::move(new_ptr, old_ptr, pos); } else { @@ -905,12 +906,12 @@ namespace easy2d } wchar_t* const insert_at = (new_ptr ? new_ptr : old_ptr) + pos; - traits::move(insert_at, cstr, count2); - traits::move(insert_at + count2, old_ptr + count, suffix_size); + char_traits::move(insert_at, cstr, count2); + char_traits::move(insert_at + count2, old_ptr + count, suffix_size); if (new_ptr) { - deallocate(str_, old_capacity); + deallocate(str_, old_capacity + 1); str_ = new_ptr; } @@ -925,7 +926,7 @@ namespace easy2d count = clamp_suffix_size(pos, count); if (count == count2) { - traits::assign(str_ + pos, count2, ch); + char_traits::assign(str_ + pos, count2, ch); return (*this); } @@ -942,7 +943,7 @@ namespace easy2d capacity_ = size_; new_ptr = allocate(capacity_ + 1); - traits::move(new_ptr, old_ptr, pos); + char_traits::move(new_ptr, old_ptr, pos); } else { @@ -950,12 +951,12 @@ namespace easy2d } wchar_t* const insert_at = (new_ptr ? new_ptr : old_ptr) + pos; - traits::assign(insert_at, count2, ch); - traits::move(insert_at + count2, old_ptr + count, suffix_size); + char_traits::assign(insert_at, count2, ch); + char_traits::move(insert_at + count2, old_ptr + count, suffix_size); if (new_ptr) { - deallocate(str_, old_capacity); + deallocate(str_, old_capacity + 1); str_ = new_ptr; } @@ -970,28 +971,24 @@ namespace easy2d check_offset(pos); count = clamp_suffix_size(pos, count); - traits::move(cstr, cbegin().base() + pos, count); + char_traits::move(cstr, cbegin().base() + pos, count); return count; } inline std::string String::to_string() const { - std::string ret; - if (const_str_) + if (const_str_ && size_) { - int len = ::WideCharToMultiByte(CP_ACP, 0, const_str_, -1, nullptr, 0, nullptr, FALSE); - if (len) + size_t len; + errno_t ret = ::wcstombs_s(&len, nullptr, 0, const_str_, 0); + if (!ret) { - char* str_tmp = new char[len + 1]; - str_tmp[0] = 0; - - len = ::WideCharToMultiByte(CP_ACP, 0, const_str_, -1, str_tmp, len + 1, nullptr, FALSE); - - ret = str_tmp; - delete[] str_tmp; + std::string ret(len - 1, '\0'); + ::wcstombs_s(nullptr, &ret[0], len, const_str_, len - 1); + return ret; } } - return ret; + return std::string(); } inline std::wstring String::to_wstring() const @@ -1014,7 +1011,7 @@ namespace easy2d { if (operable_ && str_) { - deallocate(str_, capacity_); + deallocate(str_, capacity_ + 1); } else { @@ -1077,45 +1074,100 @@ namespace easy2d // details of operator<<>> // - inline std::basic_ostream& - operator<<(std::basic_ostream& os, const String & str) + inline std::basic_ostream& operator<<(std::basic_ostream& os, const String & str) { - return os << str.c_str(); + using ostream = std::basic_ostream; + using size_type = String::size_type; + using traits = String::char_traits; + + const typename ostream::sentry ok(os); + std::ios_base::iostate state = std::ios_base::goodbit; + + if (!ok) + { + state |= std::ios_base::badbit; + } + else + { + const auto str_size = str.size(); + size_type pad = (os.width() <= 0 || static_cast(os.width()) <= str_size) ? 0 : static_cast(os.width()) - str_size; + + try + { + if ((os.flags() & std::ios_base::adjustfield) != std::ios_base::left) + { + for (; 0 < pad; --pad) + { + if (traits::eq_int_type(traits::eof(), os.rdbuf()->sputc(os.fill()))) + { + state |= std::ios_base::badbit; + break; + } + } + } + + if (state == std::ios_base::goodbit + && os.rdbuf()->sputn(str.data(), (std::streamsize)str_size) != (std::streamsize)str_size) + { + state |= std::ios_base::badbit; + } + else + { + for (; 0 < pad; --pad) + { + if (traits::eq_int_type(traits::eof(), os.rdbuf()->sputc(os.fill()))) + { + state |= std::ios_base::badbit; + break; + } + } + } + os.width(0); + } + catch (...) + { + os.setstate(std::ios_base::badbit, true); + } + } + + os.setstate(state); + return (os); } - inline std::basic_istream& - operator>>(std::basic_istream& is, String & str) + inline std::basic_istream& operator>>(std::basic_istream& is, String & str) { - using Ctype = std::ctype; - using IStream = std::basic_istream; - using SizeType = String::size_type; + using ctype = std::ctype; + using istream = std::basic_istream; + using size_type = String::size_type; + using traits = String::char_traits; - std::ios_base::iostate state = std::ios_base::goodbit; bool changed = false; + const typename istream::sentry ok(is); + std::ios_base::iostate state = std::ios_base::goodbit; - if (IStream::sentry(is)) + if (ok) { - const Ctype& ctype_fac = std::use_facet(is.getloc()); + const ctype& ctype_fac = std::use_facet(is.getloc()); str.erase(); try { - SizeType size = (0 < is.width() && static_cast(is.width()) < str.max_size()) ? static_cast(is.width()) : str.max_size(); - String::traits::int_type meta = is.rdbuf()->sgetc(); + size_type size = (0 < is.width() && static_cast(is.width()) < str.max_size()) ? static_cast(is.width()) : str.max_size(); + traits::int_type meta = is.rdbuf()->sgetc(); for (; 0 < size; --size, meta = is.rdbuf()->snextc()) { - if (String::traits::eq_int_type(String::traits::eof(), meta)) + if (traits::eq_int_type(traits::eof(), meta)) { state |= std::ios_base::eofbit; break; } - else if (ctype_fac.is(Ctype::space, String::traits::to_char_type(meta))) + else if (ctype_fac.is(ctype::space, traits::to_char_type(meta))) { break; } else { - str.push_back(String::traits::to_char_type(meta)); + str.push_back(traits::to_char_type(meta)); changed = true; } } @@ -1220,7 +1272,7 @@ namespace easy2d static_assert(std::is_integral<_Ty>::value, "_Ty must be integral"); using _UTy = std::make_unsigned_t<_Ty>; - using _Elem = String::traits::char_type; + using _Elem = String::char_traits::char_type; _Elem buffer[21]; _Elem* const buffer_end = std::end(buffer); diff --git a/easy2d/platform/Application.cpp b/easy2d/platform/Application.cpp index 8fbad7df..e9a3e220 100644 --- a/easy2d/platform/Application.cpp +++ b/easy2d/platform/Application.cpp @@ -35,12 +35,11 @@ namespace easy2d { - Application::Application(String const& app_name) + Application::Application() : end_(true) , inited_(false) , main_window_(nullptr) , time_scale_(1.f) - , app_name_(app_name) { ::CoInitialize(nullptr); diff --git a/easy2d/platform/Application.h b/easy2d/platform/Application.h index 81786be9..6311d54e 100644 --- a/easy2d/platform/Application.h +++ b/easy2d/platform/Application.h @@ -60,9 +60,7 @@ namespace easy2d : protected Noncopyable { public: - Application( - String const& app_name = L"Easy2dGame" - ); + Application(); virtual ~Application(); @@ -146,7 +144,6 @@ namespace easy2d bool end_; bool inited_; float time_scale_; - String app_name_; ScenePtr curr_scene_; ScenePtr next_scene_; diff --git a/easy2d/platform/modules.cpp b/easy2d/platform/modules.cpp index ad16f83e..975a285f 100644 --- a/easy2d/platform/modules.cpp +++ b/easy2d/platform/modules.cpp @@ -38,7 +38,8 @@ namespace easy2d } else { - E2D_LOG(L"load shlapi.dll failed"); + E2D_ERROR_LOG(L"Load shlapi.dll failed"); + throw std::runtime_error("Load shlapi.dll failed"); } } } diff --git a/easy2d/platform/modules.h b/easy2d/platform/modules.h index 9b677d33..9e98b687 100644 --- a/easy2d/platform/modules.h +++ b/easy2d/platform/modules.h @@ -40,7 +40,7 @@ namespace easy2d typedef IStream*(WINAPI *PFN_SHCreateMemStream)(const BYTE*, UINT); public: - static Shlwapi& Get() + static inline Shlwapi& Get() { static Shlwapi instance; return instance; diff --git a/easy2d/utils/Path.cpp b/easy2d/utils/Path.cpp index 4067c4ae..a3348aa1 100644 --- a/easy2d/utils/Path.cpp +++ b/easy2d/utils/Path.cpp @@ -19,8 +19,7 @@ // THE SOFTWARE. #include "Path.h" -#include "File.h" -#include "../base/window.h" +#include "../platform/modules.h" #include namespace easy2d @@ -54,30 +53,6 @@ namespace easy2d } } - - String const& Path::GetDataPath() - { - static String data_path; - if (data_path.empty()) - { - // 设置数据的保存路径 - String local_app_data_path = Path::GetLocalAppDataPath(); - - if (!local_app_data_path.empty()) - { - data_path.append(local_app_data_path).append(L"\\Easy2DGameData\\"); - - File file(data_path); - if (!file.Exists() && !CreateFolder(data_path)) - { - data_path = L""; - } - } - data_path.append(L"Data.ini"); - } - return data_path; - } - String const& Path::GetTemporaryPath() { static String temp_path; @@ -90,8 +65,7 @@ namespace easy2d { temp_path.append(path).append(L"\\Easy2DGameTemp\\"); - File file(temp_path); - if (!file.Exists() && !CreateFolder(temp_path)) + if (!modules::Shlwapi::Get().PathFileExistsW(temp_path.c_str()) && !CreateFolder(temp_path)) { temp_path = L""; } diff --git a/easy2d/utils/Path.h b/easy2d/utils/Path.h index d2aa2fb8..cfe37762 100644 --- a/easy2d/utils/Path.h +++ b/easy2d/utils/Path.h @@ -28,9 +28,6 @@ namespace easy2d class E2D_API Path { public: - // 获取数据的默认保存路径 - static String const& GetDataPath(); - // 获取临时文件目录 static String const& GetTemporaryPath();