mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
49 lines
1.2 KiB
C++
49 lines
1.2 KiB
C++
#ifndef XNA_HELPERS_HPP
|
|
#define XNA_HELPERS_HPP
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
#include <stdexcept>
|
|
|
|
namespace xna {
|
|
struct XnaHelper {
|
|
template<typename T> struct is_shared_ptr : std::false_type {};
|
|
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
|
|
|
|
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;
|
|
}
|
|
|
|
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 <class T>
|
|
static constexpr void HashCombine(std::size_t& seed, const T& v) {
|
|
std::hash<T> hasher;
|
|
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
|
}
|
|
|
|
template<typename T>
|
|
static inline auto ReturnDefaultOrNull() {
|
|
if constexpr (is_shared_ptr<T>::value)
|
|
return (T)nullptr;
|
|
else if (std::is_default_constructible<T>::value)
|
|
return T();
|
|
else
|
|
throw std::runtime_error("Unable to build object");
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif |