-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from iboB/dev
- Loading branch information
Showing
22 changed files
with
292 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Borislav Stanimirov | ||
// SPDX-License-Identifier: MIT | ||
// | ||
#pragma once | ||
#include <typeinfo> | ||
#include <string> | ||
#include <itlib/strutil.hpp> | ||
|
||
#if defined(__GNUC__) | ||
# include <cstdlib> | ||
# include <cxxabi.h> | ||
#endif | ||
|
||
namespace dynamix::util { | ||
inline std::string get_type_name_from_typeid(const char* typeid_name) { | ||
#if defined(__GNUC__) // __GNUC__ is defined with clang | ||
// use cxxabi to unmangle the gcc typeid name | ||
int cxa_demangle_status = 0; | ||
char* demangled = abi::__cxa_demangle(typeid_name, nullptr, nullptr, &cxa_demangle_status); | ||
std::string ret = demangled; | ||
free(demangled); | ||
return ret; | ||
#elif defined(_MSC_VER) | ||
std::string_view sv_name = typeid_name; | ||
|
||
// msvc typeid names are "class x" or "struct x" instead of "x", | ||
// so we just offset the pointer appropriately | ||
// C++20 use std::string_view::starts_with | ||
if (itlib::starts_with(sv_name, "enum ")) return typeid_name + 5; | ||
if (itlib::starts_with(sv_name, "class ")) return typeid_name + 6; | ||
if (itlib::starts_with(sv_name, "struct ")) return typeid_name + 7; | ||
|
||
return typeid_name; | ||
#else | ||
# error "getting typenames with typeid hasn't been tested on compilers other than gcc, clang, and msvc" | ||
#endif | ||
} | ||
|
||
template <typename T> | ||
std::string get_type_name_from_typeid() { | ||
return get_type_name_from_typeid(typeid(T).name()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Borislav Stanimirov | ||
// SPDX-License-Identifier: MIT | ||
// | ||
#include <dynamix/bits/type_name_from_typeid.hpp> | ||
|
||
#include <doctest/doctest.h> | ||
|
||
struct sfoo {}; | ||
class cbar {}; | ||
|
||
namespace test { | ||
struct a_struct {}; | ||
namespace inner { | ||
class some_class {}; | ||
} | ||
enum myenum { a, b, c }; | ||
} | ||
|
||
TEST_CASE("type_name_from_typeid") { | ||
using dynamix::util::get_type_name_from_typeid; | ||
|
||
CHECK(get_type_name_from_typeid<sfoo>() == "sfoo"); | ||
CHECK(get_type_name_from_typeid<cbar>() == "cbar"); | ||
CHECK(get_type_name_from_typeid<unsigned int>() == "unsigned int"); | ||
CHECK(get_type_name_from_typeid<test::a_struct>() == "test::a_struct"); | ||
CHECK(get_type_name_from_typeid<test::inner::some_class>() == "test::inner::some_class"); | ||
CHECK(get_type_name_from_typeid<test::myenum>() == "test::myenum"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.