nameof_module/README.md

156 lines
5.4 KiB
Markdown
Raw Permalink Normal View History

2022-04-30 15:41:39 +00:00
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://bit.ly/3OMysM8)
2022-04-19 18:47:42 +00:00
2018-04-10 19:32:51 +00:00
```text
_ _ __ _____
| \ | | / _| / ____|_ _
| \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
| . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
| |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
|_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
```
2018-03-27 08:04:47 +00:00
2019-07-31 20:14:32 +00:00
[![Github releases](https://img.shields.io/github/release/Neargye/nameof.svg)](https://github.com/Neargye/nameof/releases)
2020-12-29 18:45:54 +00:00
[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://conan.io/center/nameof)
2019-08-20 12:12:42 +00:00
[![Vcpkg package](https://img.shields.io/badge/Vcpkg-package-blueviolet)](https://github.com/microsoft/vcpkg/tree/master/ports/nameof)
2019-04-01 20:05:51 +00:00
[![License](https://img.shields.io/github/license/Neargye/nameof.svg)](LICENSE)
2021-06-26 08:42:57 +00:00
[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/PBBzVKlbMIfC3WOk)
2020-06-09 09:38:27 +00:00
[![Compiler explorer](https://img.shields.io/badge/compiler_explorer-online-blue.svg)](https://godbolt.org/z/s_ecko)
2018-03-27 09:03:12 +00:00
2019-05-25 16:16:25 +00:00
# Nameof C++
2019-03-21 16:21:01 +00:00
2019-07-31 20:14:32 +00:00
Header-only C++17 library provides nameof macros and functions to simply obtain the name of a variable, type, function, macro, and enum.
2019-03-21 16:21:01 +00:00
2018-03-27 08:04:47 +00:00
## Features
2019-03-20 16:41:51 +00:00
* C++17
2018-03-27 08:04:47 +00:00
* Header-only
* Dependency-free
* Compile-time
2019-03-26 10:00:48 +00:00
* Name of variable, member variable
* Name of type, variable type
2023-03-30 15:10:22 +00:00
* Name of member, pointer
2019-03-26 10:00:48 +00:00
* Name of function, member function
* Name of enum, enum variable
* Name of macro
* Enum to string
2018-03-27 08:04:47 +00:00
2019-10-03 13:25:50 +00:00
## Documentation
* [Reference](doc/reference.md)
2019-10-03 14:39:48 +00:00
* [Limitations](doc/limitations.md)
* [Integration](#Integration)
2019-10-03 13:25:50 +00:00
2018-05-01 13:19:34 +00:00
## [Examples](example/example.cpp)
2018-03-27 08:04:47 +00:00
2019-04-05 12:19:04 +00:00
* Nameof
2020-01-25 18:45:33 +00:00
2019-04-02 13:47:52 +00:00
```cpp
// Name of variable.
NAMEOF(somevar) -> "somevar"
2019-04-01 20:05:51 +00:00
2019-04-02 13:47:52 +00:00
// Name of member variable.
NAMEOF(person.address.zip_code) -> "zip_code"
2018-03-17 05:15:00 +00:00
2019-04-02 13:47:52 +00:00
// Name of function.
NAMEOF(foo<int, float>()) -> "foo"
// Name of member function.
NAMEOF(somevar.some_method()) -> "some_method"
2019-04-05 12:19:04 +00:00
NAMEOF(somevar.some_method<int>()) -> "some_method"
// Name of macro.
NAMEOF(__LINE__) -> "__LINE__"
NAMEOF(NAMEOF(structvar)) -> "NAMEOF"
2019-04-02 13:47:52 +00:00
```
2018-03-27 08:04:47 +00:00
2019-04-05 12:19:04 +00:00
* Nameof enum
2020-01-25 18:45:33 +00:00
2019-04-02 13:47:52 +00:00
```cpp
2020-07-03 13:18:05 +00:00
enum class Color { RED = 1, BLUE = 2, GREEN = 4 };
2020-06-26 16:37:48 +00:00
2019-04-02 13:47:52 +00:00
auto color = Color::RED;
// Name of enum variable.
NAMEOF_ENUM(color) -> "RED"
nameof::nameof_enum(color) -> "RED"
2019-07-25 14:53:23 +00:00
// Static storage enum variable to string.
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
2020-09-07 17:12:54 +00:00
NAMEOF_ENUM_CONST(Color::GREEN) -> "GREEN"
2019-07-25 14:53:23 +00:00
nameof::nameof_enum<Color::GREEN>() -> "GREEN"
2020-07-03 13:18:05 +00:00
// Enum flag variable to string.
NAMEOF_ENUM_FLAG(Color::GREEN | Color::BLUE) -> "GREEN|BLUE"
nameof::nameof_enum_flag<Color::GREEN>() -> "GREEN|BLUE"
2019-04-02 13:47:52 +00:00
```
2018-03-17 04:31:59 +00:00
2019-04-05 12:19:04 +00:00
* Nameof type
2020-01-25 18:45:33 +00:00
2019-04-02 13:47:52 +00:00
```cpp
2020-12-29 18:45:54 +00:00
const my::detail::SomeClass<int>& var_ref = var;
2019-04-02 13:47:52 +00:00
// Name of variable type.
2020-12-29 18:45:54 +00:00
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>&;
2019-04-02 13:47:52 +00:00
// Name of type.
2020-12-29 18:45:54 +00:00
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"
2019-04-02 13:47:52 +00:00
```
2018-03-27 14:00:06 +00:00
2019-04-08 18:02:53 +00:00
* Compile-time
2020-01-25 18:45:33 +00:00
2019-04-08 18:02:53 +00:00
```cpp
constexpr auto somevar_name = NAMEOF(somevar);
// somevar_name -> "somevar"
constexpr auto color_name = NAMEOF_ENUM(Color::BLUE); // or nameof::nameof_enum(Color::BLUE)
// color_name -> "BLUE"
2019-07-21 19:14:35 +00:00
constexpr auto var_type_name = NAMEOF_TYPE_EXPR(var); // or nameof::nameof_type<decltype(var)>()
2019-04-08 18:02:53 +00:00
// var_type_name -> "int"
2019-04-10 14:09:26 +00:00
constexpr auto type_name = NAMEOF_TYPE(T); // or nameof::nameof_type<T>()
2019-04-08 18:02:53 +00:00
// type_name -> "int"
```
2020-01-25 18:45:33 +00:00
## Remarks
* Before use, read the [limitations](doc/limitations.md) of functionality.
2018-04-10 19:32:51 +00:00
## Integration
2018-04-04 12:19:02 +00:00
2019-03-30 07:01:47 +00:00
You should add required file [nameof.hpp](include/nameof.hpp).
2018-04-04 12:19:02 +00:00
2019-08-29 18:24:23 +00:00
If you are using [vcpkg](https://github.com/Microsoft/vcpkg/) on your project for external dependencies, then you can use the [nameof package](https://github.com/microsoft/vcpkg/tree/master/ports/nameof).
2020-06-27 06:26:23 +00:00
If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `nameof/x.y.z` to your conan's requires, where `x.y.z` is the release version you want to use.
2019-08-29 18:24:23 +00:00
2021-04-11 15:01:30 +00:00
Archlinux users can install `nameof` by package manager AUR, using the following command: `yay -S nameof`.
2020-04-06 09:02:48 +00:00
Alternatively, you can use something like [CPM](https://github.com/TheLartians/CPM) which is based on CMake's `Fetch_Content` module.
```cmake
CPMAddPackage(
NAME nameof
GITHUB_REPOSITORY Neargye/nameof
2020-05-01 13:06:34 +00:00
GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use.
2020-04-06 09:02:48 +00:00
)
2020-06-03 10:00:31 +00:00
```
2020-04-06 09:02:48 +00:00
2018-04-10 19:32:51 +00:00
## Compiler compatibility
2023-03-30 15:10:22 +00:00
Check in the [reference](doc/reference.md) for each features.
2018-03-27 08:04:47 +00:00
2018-04-14 19:52:48 +00:00
## Licensed under the [MIT License](LICENSE)