#ifndef XNA_HELPERS_HPP #define XNA_HELPERS_HPP #include "default.hpp" namespace xna { //Class for helper functions struct XnaHelper { // // Smart Pointer Comparator // template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type {}; template struct is_weak_ptr : std::false_type {}; template struct is_weak_ptr> : std::true_type {}; template struct is_unique_ptr : std::false_type {}; template struct is_unique_ptr> : std::true_type {}; //Returns true if the type is a smart pointer template static constexpr bool IsSmartPoint() { return is_shared_ptr::value || is_unique_ptr::value || is_weak_ptr::value; } //Convert a string to wstring 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; } //Convert a wstring to string 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; } //Returns a hash reporting input values template static constexpr void HashCombine(std::size_t& seed, const T& v) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } //Returns null if the type is a smart pointer or default value if the type has a default constructor. //Throws an exception if the object cannot be created template static inline auto ReturnDefaultOrNull(const std::source_location location = std::source_location::current()) { if constexpr (IsSmartPoint()) return (T)nullptr; else if constexpr (std::is_default_constructible::value) return T(); else Exception::Throw(Exception::UNABLE_TO_BUILD_OBJECT, location); } }; } #endif