nameof_module/README.md

146 lines
5.4 KiB
Markdown
Raw Permalink Normal View History

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)
2024-01-01 22:44:56 +00:00
[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://conan.io/center/recipes/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)
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)
2024-01-31 14:26:39 +00:00
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://stand-with-ukraine.pp.ua)
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
2024-01-01 22:44:56 +00:00
If you like this project, please consider donating to one of the funds that help victims of the war in Ukraine: https://u24.gov.ua.
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
2024-01-01 22:44:56 +00:00
## [Features & 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"
2023-05-26 13:40:33 +00:00
// Obtains full name of variable, function, macro.
NAMEOF_FULL(somevar.some_method<int>()) -> "some_method<int>"
// Obtains raw name of variable, function, macro.
NAMEOF_RAW(somevar.some_method<int>()) -> "somevar.some_method<int>()"
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
2023-05-26 13:40:33 +00:00
// Enum flags variable to string.
2020-07-03 13:18:05 +00:00
NAMEOF_ENUM_FLAG(Color::GREEN | Color::BLUE) -> "GREEN|BLUE"
nameof::nameof_enum_flag<Color::GREEN | Color::BLUE>() -> "GREEN|BLUE"
2023-05-26 13:40:33 +00:00
// Obtains name of enum variable or default value if enum variable out of range.
NAMEOF_ENUM_OR(Color::GREEN) -> "GREEN"
NAMEOF_ENUM_OR((Color)0, "none") -> "none"
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"
2018-03-27 14:00:06 +00:00
2023-05-26 13:40:33 +00:00
struct A {
int this_is_the_name;
};
// Obtains name of member.
NAMEOF_MEMBER(&A::this_is_the_name) -> "this_is_the_name"
nameof::nameof_member(&A::this_is_the_name) -> "this_is_the_name"
int someglobalvariable = 0;
// Obtains name of a function, a global or class static variable.
NAMEOF_POINTER(&someglobalconstvariable) == "someglobalconstvariable"
nameof::nameof_pointer(&someglobalconstvariable) == "someglobalconstvariable"
constexpr auto global_ptr = &someglobalvariable;
NAMEOF_POINTER(global_ptr) == "someglobalconstvariable"
nameof::nameof_pointer(global_ptr) == "someglobalconstvariable"
2019-04-08 18:02:53 +00:00
```
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)