//******************************************************
// std::string, std::to_string を TCHAR に対応させる
//
// https://stackoverflow.com/questions/34961274/implement-to-xstring-to-unite-to-string-and-to-wstring
// https://qiita.com/AinoMegumi/items/660364aaadf6708fb1bd
//******************************************************

#pragma once
#include <string>
#include <tchar.h>

//
// tchar の std::tstring を定義する
//
namespace std {
	typedef std::basic_string<TCHAR> tstring;
}

//
// TCHAR に対応可能な std::to_xstring を定義する
// ex. TCHAR hoge = std::to_xstring<TCHAR, int>(10);
//
namespace std {
	template <typename CharT, typename T>
	typename std::enable_if<std::is_same<CharT, char>::value, std::string>::type
		to_xstring(const T &t)
	{
		return std::to_string(t);
	}


	template <typename CharT, typename T>
	typename std::enable_if<!std::is_same<CharT, char>::value, std::wstring>::type
		to_xstring(const T &t)
	{
		return std::to_wstring(t);
	}
}

//
// 各種数値を TCHAR に変換する std::to_tstring を定義する
// ex. int num = std::to_tstring(10);
namespace std {
	inline std::tstring to_tstring(long double _Val) { return std::to_xstring<TCHAR, long double>(_Val); }
	inline std::tstring to_tstring(double _Val) { return std::to_xstring<TCHAR, double>(_Val); }
	inline std::tstring to_tstring(float _Val) { return std::to_xstring<TCHAR, float>(_Val); }
	inline std::tstring to_tstring(unsigned long long _Val) { return std::to_xstring<TCHAR, unsigned long long>(_Val); }
	inline std::tstring to_tstring(long long _Val) { return std::to_xstring<TCHAR, long long>(_Val); }
	inline std::tstring to_tstring(unsigned long _Val) { return std::to_xstring<TCHAR, unsigned long>(_Val); }
	inline std::tstring to_tstring(long _Val) { return std::to_xstring<TCHAR, long>(_Val); }
	inline std::tstring to_tstring(unsigned int _Val) { return std::to_xstring<TCHAR, unsigned int>(_Val); }
	inline std::tstring to_tstring(int _Val) { return std::to_xstring<TCHAR, int>(_Val); }
}

// char を TCHAR に変換する maxcount は _countof(dest)
inline void char2tchar(TCHAR* dest, const char* src, size_t maxcount)
{
#ifdef UNICODE
	MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, src, strlen(src), dest, maxcount);
#else
	strcpy_s(dest, maxcount, src);
#endif
}

// TCHAR を char に変換する maxcount は _countof(dest) もしくは sizeof(dest)
inline void tchar2char(char* dest, const TCHAR *src, size_t maxcount)
{
#ifdef UNICODE
	WideCharToMultiByte(CP_ACP, 0, src, -1, dest, maxcount, NULL, NULL);
#else
	strcpy_s(dest, maxcount, src);
#endif
}