nameof.cxx 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +185 -0
- package/nameof.hpp +1254 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 - 2024 Daniil Goncharov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
[](https://github.com/Neargye/nameof/releases)
|
|
2
|
+
[](https://conan.io/center/recipes/nameof)
|
|
3
|
+
[](https://github.com/microsoft/vcpkg/tree/master/ports/nameof)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://godbolt.org/z/s_ecko)
|
|
6
|
+
[](https://stand-with-ukraine.pp.ua)
|
|
7
|
+
|
|
8
|
+
# Nameof C++
|
|
9
|
+
|
|
10
|
+
Header-only C++17 library provides nameof macros and functions to simply obtain the name of a variable, type, function, macro, and enum.
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
* [Reference](https://github.com/Neargye/nameof/blob/master/doc/reference.md)
|
|
17
|
+
* [Limitations](https://github.com/Neargye/nameof/blob/master/doc/limitations.md)
|
|
18
|
+
* [Installation](#Installation)
|
|
19
|
+
|
|
20
|
+
## [Features & Examples](https://github.com/Neargye/nameof/blob/master/example/example.cpp)
|
|
21
|
+
|
|
22
|
+
* Nameof
|
|
23
|
+
|
|
24
|
+
```cpp
|
|
25
|
+
// Name of variable.
|
|
26
|
+
NAMEOF(somevar) -> "somevar"
|
|
27
|
+
|
|
28
|
+
// Name of member variable.
|
|
29
|
+
NAMEOF(person.address.zip_code) -> "zip_code"
|
|
30
|
+
|
|
31
|
+
// Name of function.
|
|
32
|
+
NAMEOF(foo<int, float>()) -> "foo"
|
|
33
|
+
|
|
34
|
+
// Name of member function.
|
|
35
|
+
NAMEOF(somevar.some_method()) -> "some_method"
|
|
36
|
+
NAMEOF(somevar.some_method<int>()) -> "some_method"
|
|
37
|
+
|
|
38
|
+
// Name of macro.
|
|
39
|
+
NAMEOF(__LINE__) -> "__LINE__"
|
|
40
|
+
NAMEOF(NAMEOF(structvar)) -> "NAMEOF"
|
|
41
|
+
|
|
42
|
+
// Obtains full name of variable, function, macro.
|
|
43
|
+
NAMEOF_FULL(somevar.some_method<int>()) -> "some_method<int>"
|
|
44
|
+
|
|
45
|
+
// Obtains raw name of variable, function, macro.
|
|
46
|
+
NAMEOF_RAW(somevar.some_method<int>()) -> "somevar.some_method<int>()"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
* Nameof enum
|
|
50
|
+
|
|
51
|
+
```cpp
|
|
52
|
+
enum class Color { RED = 1, BLUE = 2, GREEN = 4 };
|
|
53
|
+
|
|
54
|
+
auto color = Color::RED;
|
|
55
|
+
// Name of enum variable.
|
|
56
|
+
NAMEOF_ENUM(color) -> "RED"
|
|
57
|
+
nameof::nameof_enum(color) -> "RED"
|
|
58
|
+
|
|
59
|
+
// Static storage enum variable to string.
|
|
60
|
+
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
|
|
61
|
+
NAMEOF_ENUM_CONST(Color::GREEN) -> "GREEN"
|
|
62
|
+
nameof::nameof_enum<Color::GREEN>() -> "GREEN"
|
|
63
|
+
|
|
64
|
+
// Enum flags variable to string.
|
|
65
|
+
NAMEOF_ENUM_FLAG(Color::GREEN | Color::BLUE) -> "GREEN|BLUE"
|
|
66
|
+
nameof::nameof_enum_flag<Color::GREEN | Color::BLUE>() -> "GREEN|BLUE"
|
|
67
|
+
|
|
68
|
+
// Obtains name of enum variable or default value if enum variable out of range.
|
|
69
|
+
NAMEOF_ENUM_OR(Color::GREEN) -> "GREEN"
|
|
70
|
+
NAMEOF_ENUM_OR((Color)0, "none") -> "none"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
* Nameof type
|
|
74
|
+
|
|
75
|
+
```cpp
|
|
76
|
+
const my::detail::SomeClass<int>& var_ref = var;
|
|
77
|
+
// Name of variable type.
|
|
78
|
+
NAMEOF_TYPE_EXPR(var_ref) -> "my::detail::SomeClass<int>"
|
|
79
|
+
nameof::nameof_type<decltype(var_ref)>() -> "my::detail::SomeClass<int>"
|
|
80
|
+
NAMEOF_FULL_TYPE_EXPR(var_ref) -> "const my::detail::SomeClass<int>&"
|
|
81
|
+
nameof::nameof_full_type<decltype(var_ref)>() -> "const my::detail::SomeClass<int>&"
|
|
82
|
+
NAMEOF_SHORT_TYPE_EXPR(var_ref) -> "SomeClass"
|
|
83
|
+
nameof::nameof_short_type<decltype(var_ref)>() -> "SomeClass"
|
|
84
|
+
|
|
85
|
+
using T = const my::detail::SomeClass<int>&;
|
|
86
|
+
// Name of type.
|
|
87
|
+
NAMEOF_TYPE(T) ->"my::detail::SomeClass<int>"
|
|
88
|
+
nameof::nameof_type<T>() -> "my::detail::SomeClass<int>"
|
|
89
|
+
NAMEOF_FULL_TYPE(T) -> "const my::detail::SomeClass<int>&"
|
|
90
|
+
nameof::nameof_full_type<T>() -> "const my::detail::SomeClass<int>&"
|
|
91
|
+
NAMEOF_SHORT_TYPE(T) -> "SomeClass"
|
|
92
|
+
nameof::nameof_short_type<T>() -> "SomeClass"
|
|
93
|
+
|
|
94
|
+
my::detail::Base* ptr = new my::detail::Derived();
|
|
95
|
+
// Name of type, using rtti.
|
|
96
|
+
NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived"
|
|
97
|
+
NAMEOF_FULL_TYPE_RTTI(*ptr) -> "volatile const my::detail::Derived&"
|
|
98
|
+
NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived"
|
|
99
|
+
|
|
100
|
+
struct A {
|
|
101
|
+
int this_is_the_name;
|
|
102
|
+
};
|
|
103
|
+
// Obtains name of member.
|
|
104
|
+
NAMEOF_MEMBER(&A::this_is_the_name) -> "this_is_the_name"
|
|
105
|
+
nameof::nameof_member(&A::this_is_the_name) -> "this_is_the_name"
|
|
106
|
+
|
|
107
|
+
int someglobalvariable = 0;
|
|
108
|
+
// Obtains name of a function, a global or class static variable.
|
|
109
|
+
NAMEOF_POINTER(&someglobalconstvariable) == "someglobalconstvariable"
|
|
110
|
+
nameof::nameof_pointer(&someglobalconstvariable) == "someglobalconstvariable"
|
|
111
|
+
|
|
112
|
+
constexpr auto global_ptr = &someglobalvariable;
|
|
113
|
+
NAMEOF_POINTER(global_ptr) == "someglobalconstvariable"
|
|
114
|
+
nameof::nameof_pointer(global_ptr) == "someglobalconstvariable"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Remarks
|
|
118
|
+
|
|
119
|
+
* Before use, read the [limitations](https://github.com/Neargye/nameof/blob/master/doc/limitations.md) of functionality.
|
|
120
|
+
|
|
121
|
+
## Integration
|
|
122
|
+
|
|
123
|
+
Run:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
$ npm i nameof.cxx
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
And then include `nameof.hpp` as follows:
|
|
130
|
+
|
|
131
|
+
```cxx
|
|
132
|
+
// main.cxx
|
|
133
|
+
#include <nameof.hpp>
|
|
134
|
+
|
|
135
|
+
int main() { /* ... */ }
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Finally, compile while adding the path `node_modules/nameof.cxx` to your compiler's include paths.
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
$ clang++ -std=c++17 -I./node_modules/nameof.cxx main.cxx # or, use g++
|
|
142
|
+
$ g++ -std=c++17 -I./node_modules/nameof.cxx main.cxx
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
You may also use a simpler approach with the [cpoach](https://www.npmjs.com/package/cpoach.sh) tool, which automatically adds the necessary include paths of all the installed dependencies for your project.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
$ cpoach clang++ -std=c++17 main.cxx # or, use g++
|
|
149
|
+
$ cpoach g++ -std=c++17 main.cxx
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
Alternatively, you should add required file [nameof.hpp](nameof.hpp).
|
|
155
|
+
|
|
156
|
+
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).
|
|
157
|
+
|
|
158
|
+
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.
|
|
159
|
+
|
|
160
|
+
Archlinux users can install `nameof` by package manager AUR, using the following command: `yay -S nameof`.
|
|
161
|
+
|
|
162
|
+
Alternatively, you can use something like [CPM](https://github.com/TheLartians/CPM) which is based on CMake's `Fetch_Content` module.
|
|
163
|
+
|
|
164
|
+
```cmake
|
|
165
|
+
CPMAddPackage(
|
|
166
|
+
NAME nameof
|
|
167
|
+
GITHUB_REPOSITORY Neargye/nameof
|
|
168
|
+
GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use.
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Compiler compatibility
|
|
173
|
+
|
|
174
|
+
Check in the [reference](https://github.com/Neargye/nameof/blob/master/doc/reference.md) for each features.
|
|
175
|
+
|
|
176
|
+
## Licensed under the [MIT License](LICENSE)
|
|
177
|
+
|
|
178
|
+
<br>
|
|
179
|
+
<br>
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
[](https://wolfram77.github.io)<br>
|
|
183
|
+
[](https://github.com/Neargye/nameof)
|
|
184
|
+
[](https://nodef.github.io)
|
|
185
|
+

|