Skip to content

Commit 0cd302c

Browse files
authored
Add TypeID (#1)
* update readme and add coverage to tests target * default initialize TypeID values * include ostream for windows * test gcc with disabled optimizations * another test * try with gcc-9 * use gcc-9 for install test too * add test notes to readme
1 parent 2cfc4ad commit 0cd302c

File tree

9 files changed

+54
-9
lines changed

9 files changed

+54
-9
lines changed

.github/workflows/install.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
rm -rf build
2121
2222
- name: configure
23-
run: CC=gcc-8 CXX=g++-8 cmake -Htest -Bbuild -DTEST_INSTALLED_VERSION=1
23+
run: CC=gcc-9 CXX=g++-9 cmake -Htest -Bbuild -DTEST_INSTALLED_VERSION=1
2424

2525
- name: build
2626
run: cmake --build build --config Debug -j4

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v1
1616

1717
- name: configure
18-
run: CC=gcc-8 CXX=g++-8 cmake -Htest -Bbuild -DENABLE_TEST_COVERAGE=1
18+
run: CC=gcc-9 CXX=g++-9 cmake -Htest -Bbuild -DENABLE_TEST_COVERAGE=1
1919

2020
- name: build
2121
run: cmake --build build --config Debug -j4

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
44

55
# Note: update this to your new project's name and version
66
project(StaticTypeInfo
7-
VERSION 1.0
7+
VERSION 1.1
88
LANGUAGES CXX
99
)
1010

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ A simple C++17 compile-time type info library.
1111

1212
## API
1313

14-
The header-only library exposes two `constexpr` functions: `constexpr string_view getTypeName<T>()` and `constexpr TypeIndex getTypeIndex<T>()`.
15-
The TypeIndex may be used as a key in any hash-based container.`
14+
The header-only library exposes two main `constexpr` functions: `constexpr TypeName getTypeName<T>()` and `constexpr TypeIndex getTypeIndex<T>()`.
15+
The TypeIndex may be used as a key in any hash-based container.
1616

1717
```cpp
1818
#include <iostream>
@@ -29,6 +29,11 @@ void example() {
2929
constexpr auto intIdx = getTypeIndex<int>();
3030
constexpr auto floatIdx = getTypeIndex<float>();
3131
static_assert(intIdx != floatIdx);
32+
33+
// combine both in a single type
34+
constexpr auto typeID = getTypeID<int>();
35+
static_assert(typeID.name == "int");
36+
static_assert(typeID.index == getTypeIndex<int>());
3237
}
3338
```
3439

@@ -37,14 +42,18 @@ void example() {
3742
The type name is extracted from the macro `__PRETTY_FUNCTION__` (clang/gcc) or `__FUNCSIG__` (on MSVC) inside a probe function and converted to a `string_view` using the `constexpr` constructor.
3843
The type index is a 64 bit fnv1a hash of the type name.
3944

40-
## How integrate
45+
## Compatibility
46+
47+
The library has been tested with AppleClang 11, Visual Studio 16 2019, and gcc-9.
48+
49+
## How to integrate
4150

4251
Use [CPM.cmake](https://github.com/TheLartians/CPM.cmake) to easily add the headers to your CMake project.
4352

4453
```cmake
4554
CPMAddPackage(
4655
NAME StaticTypeInfo
47-
VERSION 1.0
56+
VERSION 1.1
4857
GIT_REPOSITORY https://github.com/TheLartians/StaticTypeInfo
4958
)
5059

include/static_type_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
22

3+
#include <static_type_info/type_id.h>
34
#include <static_type_info/type_index.h>
45
#include <static_type_info/type_name.h>

include/static_type_info/type_id.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <static_type_info/type_index.h>
4+
#include <static_type_info/type_name.h>
5+
6+
namespace static_type_info {
7+
8+
struct TypeID {
9+
TypeName name = TypeName();
10+
TypeIndex index = TypeIndex();
11+
};
12+
13+
template <class T> constexpr TypeID getTypeID() {
14+
return TypeID{getTypeName<T>(), getTypeIndex<T>()};
15+
}
16+
17+
} // namespace static_type_info

test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ doctest_discover_tests(StaticTypeIndexTests)
6565
# ---- code coverage ----
6666

6767
if (ENABLE_TEST_COVERAGE)
68-
target_compile_options(StaticTypeInfo INTERFACE -O0 -g -fprofile-arcs -ftest-coverage --coverage)
69-
target_link_options(StaticTypeInfo INTERFACE "--coverage")
68+
target_compile_options(StaticTypeIndexTests PUBLIC -O0 -g -fprofile-arcs -ftest-coverage --coverage)
69+
target_link_options(StaticTypeIndexTests PUBLIC "--coverage")
7070
endif()

test/source/example.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ void example() {
1515
constexpr auto intIdx = getTypeIndex<int>();
1616
constexpr auto floatIdx = getTypeIndex<float>();
1717
static_assert(intIdx != floatIdx);
18+
19+
// combine both in a single type
20+
constexpr auto typeID = getTypeID<int>();
21+
static_assert(typeID.name == "int");
22+
static_assert(typeID.index == intIdx);
1823
}
1924
// clang-format on
2025

test/source/type_id.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <doctest/doctest.h>
2+
#include <static_type_info/type_id.h>
3+
4+
#include <ostream>
5+
6+
TEST_CASE_TEMPLATE("TypeID", T, int, float, double) {
7+
using namespace static_type_info;
8+
constexpr auto typeID = getTypeID<T>();
9+
static_assert(typeID.name == getTypeName<T>());
10+
static_assert(typeID.index == getTypeIndex<T>());
11+
CHECK(typeID.name == getTypeName<T>());
12+
CHECK(typeID.index == getTypeIndex<T>());
13+
}

0 commit comments

Comments
 (0)