2024-03-18 15:41:46 -03:00
|
|
|
#ifndef XNA_HELPERS_HPP
|
|
|
|
#define XNA_HELPERS_HPP
|
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
#include "default.hpp"
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
namespace xna {
|
2024-06-22 11:52:21 -03:00
|
|
|
//Class for helper functions
|
2024-06-01 20:45:00 -03:00
|
|
|
struct XnaHelper {
|
2024-06-22 11:52:21 -03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Smart Pointer Comparator
|
|
|
|
//
|
|
|
|
|
2024-06-01 20:45:00 -03:00
|
|
|
template<typename T> struct is_shared_ptr : std::false_type {};
|
|
|
|
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
|
2024-07-07 16:06:05 -03:00
|
|
|
template<typename T> struct is_weak_ptr : std::false_type {};
|
|
|
|
template<typename T> struct is_weak_ptr<std::weak_ptr<T>> : std::true_type {};
|
|
|
|
template<typename T> struct is_unique_ptr : std::false_type {};
|
|
|
|
template<typename T> struct is_unique_ptr<std::unique_ptr<T>> : std::true_type {};
|
|
|
|
|
|
|
|
//Returns true if the type is a smart pointer
|
|
|
|
template <typename T>
|
|
|
|
static constexpr bool IsSmartPoint() {
|
|
|
|
return is_shared_ptr<T>::value || is_unique_ptr<T>::value || is_weak_ptr<T>::value;
|
|
|
|
}
|
2024-06-01 20:45:00 -03:00
|
|
|
|
2024-06-22 11:52:21 -03:00
|
|
|
//Convert a string to wstring
|
2024-06-01 20:45:00 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:52:21 -03:00
|
|
|
//Convert a wstring to string
|
2024-06-01 20:45:00 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:52:21 -03:00
|
|
|
//Returns a hash reporting input values
|
2024-06-01 20:45:00 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:52:21 -03:00
|
|
|
//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
|
2024-06-01 20:45:00 -03:00
|
|
|
template<typename T>
|
2024-06-22 11:52:21 -03:00
|
|
|
static inline auto ReturnDefaultOrNull(const std::source_location location = std::source_location::current()) {
|
2024-07-07 16:06:05 -03:00
|
|
|
if constexpr (IsSmartPoint<T>())
|
2024-06-01 20:45:00 -03:00
|
|
|
return (T)nullptr;
|
2024-07-07 16:06:05 -03:00
|
|
|
else if constexpr (std::is_default_constructible<T>::value)
|
2024-06-01 20:45:00 -03:00
|
|
|
return T();
|
|
|
|
else
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::UNABLE_TO_BUILD_OBJECT, location);
|
2024-06-01 20:45:00 -03:00
|
|
|
}
|
|
|
|
};
|
2024-03-18 15:41:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|