diff --git a/framework/csharp/object.cpp b/framework/csharp/object.cpp index 5b41ccc..7a79f93 100644 --- a/framework/csharp/object.cpp +++ b/framework/csharp/object.cpp @@ -5,7 +5,7 @@ namespace xna { size_t Object::GetHashCode() const { size_t seed = 0; - XnaHHashCombine(seed, this); + XnaHelper::HashCombine(seed, this); return seed; } diff --git a/framework/csharp/type.cpp b/framework/csharp/type.cpp index 266ac6b..da90c2b 100644 --- a/framework/csharp/type.cpp +++ b/framework/csharp/type.cpp @@ -4,11 +4,11 @@ namespace xna { size_t Type::GetHashCode() const { size_t seed = 0; - XnaHHashCombine(seed, fullName); - XnaHHashCombine(seed, isClass); - XnaHHashCombine(seed, isEnum); - XnaHHashCombine(seed, isValueType); - XnaHHashCombine(seed, isPrimitive); + XnaHelper::HashCombine(seed, fullName); + XnaHelper::HashCombine(seed, isClass); + XnaHelper::HashCombine(seed, isEnum); + XnaHelper::HashCombine(seed, isValueType); + XnaHelper::HashCombine(seed, isPrimitive); return seed; } diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index dc6eedb..9833ef4 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -89,7 +89,7 @@ namespace xna { DXGI_ADAPTER_DESC1 desc; impl->dxadapter->GetDesc1(&desc); - String description = XnaHToString(desc.Description); + String description = XnaHelper::ToString(desc.Description); return description; } @@ -110,7 +110,7 @@ namespace xna { if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { pOutput->GetDesc(&outputDesc); - String deviceName = XnaHToString(outputDesc.DeviceName); + String deviceName = XnaHelper::ToString(outputDesc.DeviceName); pOutput->Release(); pOutput = nullptr; diff --git a/framework/platform-dx/soundeffect.cpp b/framework/platform-dx/soundeffect.cpp index a0331ac..f521c66 100644 --- a/framework/platform-dx/soundeffect.cpp +++ b/framework/platform-dx/soundeffect.cpp @@ -20,7 +20,7 @@ namespace xna { if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine) return; - const auto file = XnaHToWString(fileName); + const auto file = XnaHelper::ToWString(fileName); impl->_dxSoundEffect = unew(AudioEngine::impl->_dxAudioEngine.get(), file.c_str()); } diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 58202ae..218ef70 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -11,7 +11,7 @@ namespace xna { auto _this = device.shared_from_this(); auto texture2d = New(_this); ID3D11Resource* resource = nullptr; - auto wstr = XnaHToWString(fileName); + auto wstr = XnaHelper::ToWString(fileName); HRESULT result = DirectX::CreateWICTextureFromFile( device.impl->_device, diff --git a/inc/content/manager.hpp b/inc/content/manager.hpp index 13c2f4f..e455ce0 100644 --- a/inc/content/manager.hpp +++ b/inc/content/manager.hpp @@ -34,7 +34,7 @@ namespace xna { template auto Load(String const& assetName) { if (assetName.empty()) { - return ReturnDefaultOrNull(); + return XnaHelper::ReturnDefaultOrNull(); } auto obj2 = ReadAsset(assetName); @@ -48,7 +48,7 @@ namespace xna { auto input = OpenStream(assetName); if (input->IsClosed()) - return ReturnDefaultOrNull(); + return XnaHelper::ReturnDefaultOrNull(); auto contentReader = ContentReader::Create(this, input, assetName); diff --git a/inc/content/reader.hpp b/inc/content/reader.hpp index 52cf449..221dbad 100644 --- a/inc/content/reader.hpp +++ b/inc/content/reader.hpp @@ -75,7 +75,7 @@ namespace xna { const auto num = Read7BitEncodedInt(); if (num == 0) { - ReturnDefaultOrNull(); + XnaHelper::ReturnDefaultOrNull(); } const auto index = num - 1; @@ -83,7 +83,7 @@ namespace xna { if (index >= typeReaders.size()) { xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE); - ReturnDefaultOrNull(); + XnaHelper::ReturnDefaultOrNull(); } auto reader = typeReaders[index]; @@ -103,7 +103,7 @@ namespace xna { return objB; } - return ReturnDefaultOrNull(); + return XnaHelper::ReturnDefaultOrNull(); } template diff --git a/inc/helpers.hpp b/inc/helpers.hpp index d1af0c1..20b509b 100644 --- a/inc/helpers.hpp +++ b/inc/helpers.hpp @@ -3,47 +3,47 @@ #include #include +#include namespace xna { - inline std::wstring XnaHToWString(const std::string& str) - { - std::wstring wstr; - size_t size; - wstr.resize(str.length()); - mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size()); - return wstr; - } + struct XnaHelper { + template struct is_shared_ptr : std::false_type {}; + template struct is_shared_ptr> : std::true_type {}; - inline std::string XnaHToString(const std::wstring& wstr) - { - std::string str; - size_t size; - str.resize(wstr.length()); - wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size()); - return str; - } + static inline std::wstring ToWString(const std::string& str) + { + std::wstring wstr; + size_t size; + wstr.resize(str.length()); + mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size()); + return wstr; + } - template - constexpr void XnaHHashCombine(std::size_t& seed, const T& v) { - std::hash hasher; - seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); - } + static inline std::string ToString(const std::wstring& wstr) + { + std::string str; + size_t size; + str.resize(wstr.length()); + wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size()); + return str; + } - template struct is_shared_ptr : std::false_type {}; - template struct is_shared_ptr> : std::true_type {}; + template + static constexpr void HashCombine(std::size_t& seed, const T& v) { + std::hash hasher; + seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } - template - inline auto ReturnDefaultOrNull() { - if constexpr (is_shared_ptr::value) - return (T)nullptr; - else - return T(); - } - - /*template - inline auto ReturnAuto(T& value) { - return value; - }*/ + template + static inline auto ReturnDefaultOrNull() { + if constexpr (is_shared_ptr::value) + return (T)nullptr; + else if (std::is_default_constructible::value) + return T(); + else + throw std::runtime_error("Unable to build object"); + } + }; } #endif \ No newline at end of file