aboutsummaryrefslogtreecommitdiff
path: root/test/source/GetTypeName.h
blob: f844167e5fe0d5b61782cd320a8d745021dab1b1 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////


#ifndef GETTYPENAME_H
#define GETTYPENAME_H


#include <EABase/eabase.h>
#include <EASTL/type_traits.h>
#include <EASTL/string.h>
#include <stdlib.h>
#include <typeinfo>


///////////////////////////////////////////////////////////////////////////////
// EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE
//
// Defined as 0 or 1. The value depends on the compilation environment.
// Indicates if we can use system-provided abi::__cxa_demangle() at runtime.
//
#if !defined(EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE)
	#if (defined(EA_PLATFORM_LINUX) || defined(EA_PLATFORM_APPLE)) && defined(EA_PLATFORM_DESKTOP)
		#define EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE 1
	#else
		#define EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE 0
	#endif
#endif


#if EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE
	#include <cxxabi.h>
#elif EA_WINAPI_FAMILY_PARTITION(EA_WINAPI_PARTITION_DESKTOP)
	EA_DISABLE_ALL_VC_WARNINGS();
	#include <Windows.h>
	#include <DbgHelp.h>
	#pragma comment(lib, "dbghelp.lib")
	EA_RESTORE_ALL_VC_WARNINGS();
#endif


///////////////////////////////////////////////////////////////////////////////
// EASTLTEST_GETTYPENAME_AVAILABLE
//
// Defined as 0 or 1. The value depends on the compilation environment.
// Indicates if we can use system-provided abi::__cxa_demangle() at runtime.
//
#if !defined(EASTLTEST_GETTYPENAME_AVAILABLE)
	#if (EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE || EA_WINAPI_FAMILY_PARTITION(EA_WINAPI_PARTITION_DESKTOP)) && (!defined(EA_COMPILER_NO_RTTI) || defined(_MSC_VER)) // VC++ works without RTTI enabled.
		#define EASTLTEST_GETTYPENAME_AVAILABLE 1
	#else
		#define EASTLTEST_GETTYPENAME_AVAILABLE 0
	#endif
#endif


/// GetTypeName
///
/// Returns the type name of a templated type.
///
template <typename T>
eastl::string GetTypeName()
{
	eastl::string result;

	#if !defined(EA_COMPILER_NO_RTTI) || defined(_MSC_VER) // VC++ works without RTTI enabled.
		typedef typename eastl::remove_reference<T>::type TR;

		const char* pName = typeid(TR).name();

		#if EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE
			const char* pDemangledName = abi::__cxa_demangle(pName, NULL, NULL, NULL);

		#elif EA_WINAPI_FAMILY_PARTITION(EA_WINAPI_PARTITION_DESKTOP)
			char pDemangledName[1024];
			DWORD count = UnDecorateSymbolName(pName, pDemangledName, (DWORD)EAArrayCount(pDemangledName), UNDNAME_NO_THISTYPE | UNDNAME_NO_ACCESS_SPECIFIERS | UNDNAME_NO_MEMBER_TYPE);
			if(count == 0)
				pDemangledName[0] = 0;
		#else
			const char* pDemangledName = NULL;
		#endif

		if(pDemangledName && pDemangledName[0])
			result = pDemangledName;
		else
			result = pName;

		if(eastl::is_const<TR>::value)
			result += " const";

		if(eastl::is_volatile<TR>::value)
			result += " volatile";

		if(eastl::is_lvalue_reference<T>::value)
			result += "&";
		else if(eastl::is_rvalue_reference<T>::value)
			result += "&&";

		if(pDemangledName)
		{
			#if EASTL_LIBSTDCPP_DEMANGLE_AVAILABLE
				free((void*)(pDemangledName));
			#endif
		}
	#endif

	return result;
}


#endif // Header include guard