Skip to content

Commit

Permalink
Wip v0.10.0 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neargye authored Dec 29, 2020
1 parent 308bbf9 commit 6fa49cc
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 603 deletions.
28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.8)

project(nameof VERSION "0.9.4" LANGUAGES CXX)
project(nameof VERSION "0.9.5" LANGUAGES CXX)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(IS_TOPLEVEL_PROJECT TRUE)
Expand All @@ -10,6 +10,7 @@ endif()

option(NAMEOF_OPT_BUILD_EXAMPLES "Build nameof examples" ${IS_TOPLEVEL_PROJECT})
option(NAMEOF_OPT_BUILD_TESTS "Build and perform nameof tests" ${IS_TOPLEVEL_PROJECT})
option(NAMEOF_OPT_INSTALL "Generate and install nameof target" ${IS_TOPLEVEL_PROJECT})

if(NAMEOF_OPT_BUILD_EXAMPLES)
add_subdirectory(example)
Expand All @@ -33,18 +34,21 @@ write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)

install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Config)
if(NAMEOF_OPT_INSTALL)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Config)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION lib/cmake/${PROJECT_NAME})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION lib/cmake/${PROJECT_NAME})

install(EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME})
install(EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME})

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include
DESTINATION .)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include
DESTINATION .)

export(EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::)
endif()

export(EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::)
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
```

[![Github releases](https://img.shields.io/github/release/Neargye/nameof.svg)](https://github.com/Neargye/nameof/releases)
[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://conan.io/center/nameof/0.9.4)
[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://conan.io/center/nameof)
[![Vcpkg package](https://img.shields.io/badge/Vcpkg-package-blueviolet)](https://github.com/microsoft/vcpkg/tree/master/ports/nameof)
[![License](https://img.shields.io/github/license/Neargye/nameof.svg)](LICENSE)
[![Build status](https://travis-ci.org/Neargye/nameof.svg?branch=master)](https://travis-ci.org/Neargye/nameof)
Expand Down Expand Up @@ -86,19 +86,29 @@ Header-only C++17 library provides nameof macros and functions to simply obtain
* Nameof type

```cpp
using T = const int&;
T var = 42;
const my::detail::SomeClass<int>& var_ref = var;
// Name of variable type.
NAMEOF_TYPE_EXPR(var) -> "int"
NAMEOF_FULL_TYPE_EXPR(var) -> "const int&"
nameof::nameof_type<decltype(var)>() -> "int"
nameof::nameof_full_type<decltype(var)>() -> "const int&"

NAMEOF_TYPE_EXPR(var_ref) -> "my::detail::SomeClass<int>"
nameof::nameof_type<decltype(var_ref)>() -> "my::detail::SomeClass<int>"
NAMEOF_FULL_TYPE_EXPR(var_ref) -> "const my::detail::SomeClass<int>&"
nameof::nameof_full_type<decltype(var_ref)>() -> "const my::detail::SomeClass<int>&"
NAMEOF_SHORT_TYPE_EXPR(var_ref) -> "SomeClass"
nameof::nameof_short_type<decltype(var_ref)>() -> "SomeClass"

using T = const my::detail::SomeClass<int>&;
// Name of type.
NAMEOF_TYPE(T) -> "int"
NAMEOF_FULL_TYPE(T) -> "const int&"
nameof::nameof_type<T>() -> "int"
nameof::nameof_full_type<T>() -> "const int&"
NAMEOF_TYPE(T) ->"my::detail::SomeClass<int>"
nameof::nameof_type<T>() -> "my::detail::SomeClass<int>"
NAMEOF_FULL_TYPE(T) -> "const my::detail::SomeClass<int>&"
nameof::nameof_full_type<T>() -> "const my::detail::SomeClass<int>&"
NAMEOF_SHORT_TYPE(T) -> "SomeClass"
nameof::nameof_short_type<T>() -> "SomeClass"

my::detail::Base* ptr = new my::detail::Derived();
// Name of type, using rtti.
NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived"
NAMEOF_FULL_TYPE_RTTI(*ptr) -> "volatile const my::detail::Derived&"
NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived"
```
* Compile-time
Expand Down
81 changes: 15 additions & 66 deletions doc/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@

## Nameof Type

* To check is nameof type supported compiler use macro `NAMEOF_TYPE_SUPPORTED` or constexpr constant `nameof::is_nameof_type_supported`.

* This library uses a compiler-specific hack (based on `__PRETTY_FUNCTION__` / `__FUNCSIG__`), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 7.

* Nameof type returns compiler-specific type name.
* nameof_type and nameof_type_rtti returns compiler-specific type name.

* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
* To check is nameof_type supported compiler use macro `NAMEOF_TYPE_SUPPORTED` or constexpr constant `nameof::is_nameof_type_supported`.</br>
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.

## Nameof Enum

* Do not use [nameof](https://github.com/Neargye/nameof) and [magic_enum](https://github.com/Neargye/magic_enum) in the same project to get enum name.
* To check is nameof_type_rtti supported compiler use macro `NAMEOF_TYPE_RTTI_SUPPORTED` or constexpr constant `nameof::is_nameof_type_rtti_supported`.</br>
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.

* To check is nameof enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.
## Nameof Enum

* This library uses a compiler-specific hack (based on `__PRETTY_FUNCTION__` / `__FUNCSIG__`), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9.

* Enum can't reflect if the enum is a forward declaration.
* Do not use [nameof](https://github.com/Neargye/nameof) and [magic_enum](https://github.com/Neargye/magic_enum) in the same project to get enum name.

* To check is nameof_enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.</br>
If nameof_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_ENUM_NO_CHECK_SUPPORT`.

* Enum value must be in range `[NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]`.

Expand All @@ -40,74 +41,22 @@
#include <nameof.hpp>
```

* If need another range for specific enum type, add specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace nameof`.
* If need another range for specific enum type, add specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace nameof::customize`.

```cpp
#include <nameof.hpp>

enum class number { one = 100, two = 200, three = 300 };

namespace nameof {
template <>
struct enum_range<number> {
struct nameof::customize::enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
} // namespace nameof
```

* Nameof enum won't work if a value is aliased, work with enum-aliases is compiler-implementation-defined.

```cpp
enum ShapeKind {
ConvexBegin = 0,
Box = 0, // Won't work.
Sphere = 1,
ConvexEnd = 2,
Donut = 2, // Won't work too.
Banana = 3,
COUNT = 4,
};
// nameof::nameof_enum(ShapeKind::Box) -> "ConvexBegin" or ""
// NAMEOF_ENUM(ShapeKind::Box) -> "ConvexBegin" or ""
```
One of the possible workaround the issue:
```cpp
enum ShapeKind {
// Convex shapes, see ConvexBegin and ConvexEnd below.
Box = 0,
Sphere = 1,
// Non-convex shapes.
Donut = 2,
Banana = 3,
COUNT = Banana + 1,
// Non-reflected aliases.
ConvexBegin = Box,
ConvexEnd = Sphere + 1,
};
// nameof::nameof_enum(ShapeKind::Box) -> "Box"
// NAMEOF_ENUM(ShapeKind::Box) -> "Box"
// Non-reflected aliases.
// nameof::nameof_enum(ShapeKind::ConvexBegin) -> "Box"
// NAMEOF_ENUM(ShapeKind::ConvexBegin) -> "Box"
```

* If you hit a message like this:

```text
[...]
note: constexpr evaluation hit maximum step limit; possible infinite loop?
```

Change the limit for the number of constexpr evaluated:
* MSVC `/constexpr:depthN`, `/constexpr:stepsN` <https://docs.microsoft.com/en-us/cpp/build/reference/constexpr-control-constexpr-evaluation>
* Clang `-fconstexpr-depth=N`, `-fconstexpr-steps=N` <https://clang.llvm.org/docs/UsersManual.html#controlling-implementation-limits>
* GCC `-fconstexpr-depth=N`, `-fconstexpr-loop-limit=N`, `-fconstexpr-ops-limit=N` <https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/C_002b_002b-Dialect-Options.html>
* Won't work if a value is aliased, work with enum-aliases is compiler-implementation-defined.

* Won't work if the enum is a forward declaration.

* Intellisense Visual Studio may have some problems analyzing `nameof`.
Loading

0 comments on commit 6fa49cc

Please sign in to comment.