File: mptExceptionText.h

package info (click to toggle)
libopenmpt 0.4.3-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,724 kB
  • sloc: cpp: 99,820; sh: 4,503; ansic: 3,449; makefile: 480
file content (76 lines) | stat: -rw-r--r-- 1,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * 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
{

template <typename T> T get_exception_text_impl(const std::exception & e) noexcept
{
	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");
	}
}

template <typename T> inline T get_exception_text(const std::exception & e) noexcept
{
	return mpt::get_exception_text_impl<T>(e);
}
template <> inline std::string get_exception_text<std::string>(const std::exception & e) noexcept
{
	return mpt::get_exception_text_impl<std::string>(e);
}
#if defined(MPT_ENABLE_CHARSET_LOCALE)
template <> inline mpt::lstring get_exception_text<mpt::lstring>(const std::exception & e) noexcept
{
	return mpt::ToLocale(mpt::CharsetLocaleOrUTF8, mpt::get_exception_text_impl<std::string>(e));
}
#endif
#if MPT_WSTRING_FORMAT
template <> inline std::wstring get_exception_text<std::wstring>(const std::exception & e) noexcept
{
	return mpt::ToWide(mpt::CharsetLocaleOrUTF8, mpt::get_exception_text_impl<std::string>(e));
}
#endif
#if MPT_USTRING_MODE_UTF8
template <> inline mpt::ustring get_exception_text<mpt::ustring>(const std::exception & e) noexcept
{
	return mpt::ToUnicode(mpt::CharsetLocaleOrUTF8, mpt::get_exception_text_impl<std::string>(e));
}
#endif

} // namespace mpt



OPENMPT_NAMESPACE_END