Fix MingW and MSVC builds: return a uniform type name across platforms.

next
Erik Hofman 4 years ago
parent e58ca605b6
commit 9cb98475b2

@ -50,6 +50,7 @@ if(ENABLE_TESTS)
add_simgear_autotest(test_expressions expression_test.cxx) add_simgear_autotest(test_expressions expression_test.cxx)
add_simgear_autotest(test_shared_ptr shared_ptr_test.cpp) add_simgear_autotest(test_shared_ptr shared_ptr_test.cpp)
add_simgear_autotest(test_commands test_commands.cxx) add_simgear_autotest(test_commands test_commands.cxx)
add_simgear_autotest(test_typeid test_typeid.cxx)
endif(ENABLE_TESTS) endif(ENABLE_TESTS)
add_boost_test(function_list add_boost_test(function_list

@ -4,7 +4,7 @@
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
#if __linux__ #ifndef _MSC_VER
#include <cxxabi.h> #include <cxxabi.h>
#endif #endif
@ -15,8 +15,10 @@ namespace simgear
template <typename T> template <typename T>
std::string getTypeName(void) std::string getTypeName(void)
{ {
#ifdef _WIN32 #ifdef _MSC_VER
std::string name = typeid(T).name(); const char *type = typeid(T).name();
const char *tn = strchr(type, ' ');
std::string name = tn ? tn+1 : type;
#else // __linux__ #else // __linux__
int error = 0; int error = 0;
char *type = abi::__cxa_demangle(typeid(T).name(), 0, 0, &error); char *type = abi::__cxa_demangle(typeid(T).name(), 0, 0, &error);

@ -0,0 +1,41 @@
////////////////////////////////////////////////////////////////////////
// Test harness.
////////////////////////////////////////////////////////////////////////
#include <simgear_config.h>
#include <simgear/compiler.h>
#include <iostream>
#include <simgear/misc/test_macros.hxx>
#include "intern.hxx"
class Base {};
class Derived : public Base {};
struct Base2 { virtual void foo() {} }; // polymorphic
struct Derived2 : Base2 {};
template <typename T>
std::string Type(T type) {
return simgear::getTypeName<T>();
}
int main(int ac, char ** av)
{
Derived d1;
Base& b1 = d1;
Derived2 d2;
Base2& b2 = d2;
int i;
SG_VERIFY(Type(d1) == "Derived");
SG_VERIFY(Type(b1) == "Base");
SG_VERIFY(Type(d2) == "Derived2");
SG_VERIFY(Type(b2) == "Base2");
SG_VERIFY(Type(i) == "int");
std::cout << "all tests passed" << std::endl;
return 0;
}
Loading…
Cancel
Save