nameof_module/README.md

139 lines
4.7 KiB
Markdown
Raw Normal View History

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)
2019-08-29 18:24:23 +00:00
[![Conan package](https://img.shields.io/badge/Conan-package-blueviolet)](https://bintray.com/neargye/conan-packages/nameof:neargye)
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)
2019-07-31 20:14:32 +00:00
[![Build status](https://travis-ci.org/Neargye/nameof.svg?branch=master)](https://travis-ci.org/Neargye/nameof)
2019-04-01 20:05:51 +00:00
[![Build status](https://ci.appveyor.com/api/projects/status/yq5fk0d9mwljbubt/branch/master?svg=true)](https://ci.appveyor.com/project/Neargye/nameof/branch/master)
2019-07-31 20:14:32 +00:00
[![Codacy badge](https://api.codacy.com/project/badge/Grade/1d06f3f07afe4f34acd29c0c8efa830b)](https://www.codacy.com/app/Neargye/nameof?utm_source=github.com&utm_medium=referral&utm_content=Neargye/nameof&utm_campaign=Badge_Grade)
2019-12-30 14:34:00 +00:00
[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/p4k7LC9w0BO4UJgo)
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
* 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
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.
NAMEOF_CONST_ENUM(Color::GREEN) -> "GREEN"
nameof::nameof_enum<Color::GREEN>() -> "GREEN"
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
2019-07-21 19:14:35 +00:00
using T = const int&;
2019-04-02 13:47:52 +00:00
T var = 42;
// Name of variable type.
2019-07-21 19:14:35 +00:00
NAMEOF_TYPE_EXPR(var) -> "int"
NAMEOF_FULL_TYPE_EXPR(var) -> "const int&"
2019-04-02 13:47:52 +00:00
nameof::nameof_type<decltype(var)>() -> "int"
2019-07-21 19:14:35 +00:00
nameof::nameof_full_type<decltype(var)>() -> "const int&"
2019-04-02 13:47:52 +00:00
// Name of type.
NAMEOF_TYPE(T) -> "int"
2019-07-21 19:14:35 +00:00
NAMEOF_FULL_TYPE(T) -> "const int&"
2019-04-02 13:47:52 +00:00
nameof::nameof_type<T>() -> "int"
2019-07-21 19:14:35 +00:00
nameof::nameof_full_type<T>() -> "const int&"
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).
If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `nameof/x.y.z@neargye/stable` to your conan's requires, where `x.y.z` is the release version you want to use.
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
)
2018-04-10 19:32:51 +00:00
## Compiler compatibility
2019-12-12 15:12:42 +00:00
* Clang/LLVM >= 5
2019-12-17 12:37:50 +00:00
* MSVC++ >= 14.11 / Visual Studio >= 2017
2019-10-25 10:43:37 +00:00
* Xcode >= 10
2019-04-02 13:47:52 +00:00
* GCC >= 7 (GCC >= 9 for NAMEOF_ENUM)
2018-03-27 08:04:47 +00:00
2018-04-14 19:52:48 +00:00
## Licensed under the [MIT License](LICENSE)