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

This commit is contained in:
Erik Hofman 2021-03-29 16:08:06 +02:00
parent e58ca605b6
commit 9cb98475b2
3 changed files with 47 additions and 3 deletions

View File

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

View File

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

View File

@ -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;
}