cog/Frameworks/OpenMPT.old/OpenMPT/common/mptExceptionText.h

77 lines
1.7 KiB
C
Raw Normal View History

2019-01-24 02:16:37 +00:00
/*
* mptExceptionText.h
* ------------------
* Purpose: Guess encoding of exception string
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "BuildSettings.h"
#include "mptException.h"
#include "mptString.h"
#include <exception>
OPENMPT_NAMESPACE_BEGIN
namespace mpt
{
2019-06-21 03:17:10 +00:00
template <typename T> T get_exception_text_impl(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
if(e.what() && (std::strlen(e.what()) > 0))
{
return T(e.what());
} else if(typeid(e).name() && (std::strlen(typeid(e).name()) > 0))
{
return T(typeid(e).name());
} else
{
return T("unknown exception");
}
}
2019-06-21 03:17:10 +00:00
template <typename T> inline T get_exception_text(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
return mpt::get_exception_text_impl<T>(e);
}
2019-06-21 03:17:10 +00:00
template <> inline std::string get_exception_text<std::string>(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
return mpt::get_exception_text_impl<std::string>(e);
}
#if defined(MPT_ENABLE_CHARSET_LOCALE)
2019-06-21 03:17:10 +00:00
template <> inline mpt::lstring get_exception_text<mpt::lstring>(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
2020-09-22 04:54:24 +00:00
return mpt::ToLocale(mpt::CharsetException, mpt::get_exception_text_impl<std::string>(e));
2019-01-24 02:16:37 +00:00
}
#endif
#if MPT_WSTRING_FORMAT
2019-06-21 03:17:10 +00:00
template <> inline std::wstring get_exception_text<std::wstring>(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
2020-09-22 04:54:24 +00:00
return mpt::ToWide(mpt::CharsetException, mpt::get_exception_text_impl<std::string>(e));
2019-01-24 02:16:37 +00:00
}
#endif
#if MPT_USTRING_MODE_UTF8
2019-06-21 03:17:10 +00:00
template <> inline mpt::ustring get_exception_text<mpt::ustring>(const std::exception & e)
2019-01-24 02:16:37 +00:00
{
2020-09-22 04:54:24 +00:00
return mpt::ToUnicode(mpt::CharsetException, mpt::get_exception_text_impl<std::string>(e));
2019-01-24 02:16:37 +00:00
}
#endif
} // namespace mpt
OPENMPT_NAMESPACE_END