diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69a35d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +### cpp gitignore ### +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +### CMake gitignore ### +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..33848b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 - 2018 Terik23 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d774ed --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# nameof(x) c++ + +Simple Example: +``` +#include +#include +#include +struct SomeStruct { + int SomeField; + void SomeMethod() { std::cout << "No called!" << std::endl; } +}; + +int someVar = 0; + +int main() { + SomeStruct someVar{1}; + + constexpr auto a = nameof_variable(someVar.SomeField); + constexpr auto b = nameof_variable((&someVar)->SomeField); + constexpr auto c = nameof_variable(someVar); + constexpr auto d = nameof_variable(::someVar); + constexpr auto e = nameof_variable(&SomeStruct::SomeMethod); + constexpr auto f = nameof_function(someVar.SomeMethod()); + constexpr auto g = nameof_type(SomeStruct); + + std::cout << a << std::endl; // SomeField + std::cout << b << std::endl; // SomeField + std::cout << c << std::endl; // someVar + std::cout << d << std::endl; // someVar + std::cout << e << std::endl; // SomeMethod + std::cout << f << std::endl; // SomeMethod() + std::cout << g << std::endl; // SomeStruct +} +``` diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..2821680 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.6.4) +project(nameof_example) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +include_directories(../src) + +set(SRC + example.cpp + ../src/nameof.hpp) + +add_executable(${PROJECT_NAME} ${SRC}) \ No newline at end of file diff --git a/example/example.cpp b/example/example.cpp new file mode 100644 index 0000000..736baf8 --- /dev/null +++ b/example/example.cpp @@ -0,0 +1,54 @@ +// MIT License +// +// Copyright(c) 2016 - 2018 Terik23 +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +// Simple Example. +#include +#include +#include + +struct SomeStruct { + int SomeField; + void SomeMethod() { std::cout << "No called!" << std::endl; } +}; + +int someVar = 0; + +int main() { + SomeStruct someVar{1}; + + constexpr auto a = nameof_variable(someVar.SomeField); + constexpr auto b = nameof_variable((&someVar)->SomeField); + constexpr auto c = nameof_variable(someVar); + constexpr auto d = nameof_variable(::someVar); + constexpr auto e = nameof_variable(&SomeStruct::SomeMethod); + constexpr auto f = nameof_function(someVar.SomeMethod()); + constexpr auto g = nameof_type(SomeStruct); + + std::cout << a << std::endl; // SomeField + std::cout << b << std::endl; // SomeField + std::cout << c << std::endl; // someVar + std::cout << d << std::endl; // someVar + std::cout << e << std::endl; // SomeMethod + std::cout << f << std::endl; // SomeMethod() + std::cout << g << std::endl; // SomeStruct +} \ No newline at end of file diff --git a/nameof.cpp b/src/nameof.hpp similarity index 72% rename from nameof.cpp rename to src/nameof.hpp index e8089c9..2b4681a 100644 --- a/nameof.cpp +++ b/src/nameof.hpp @@ -21,6 +21,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#pragma once + inline constexpr const char* template_nameof_(const char* name, size_t length) { return length == 0 ? name : (name[length - 1] == '.' || name[length - 1] == '>' || name[length - 1] == ':') @@ -39,34 +41,4 @@ template inline constexpr const char* template_nameof_type(const ch // Used to obtain the string name of a function. #define nameof_function(function) template_nameof_function(#function, sizeof(#function) / sizeof(char) - 1); if (false) (void)(function); -inline constexpr const char* template_nameof_function(const char* name, size_t length) { return template_nameof_(name, length); } - -// Example. -#include - -struct SomeStruct { - int SomeField; - void SomeMethod() { std::cout << "No called!" << std::endl; } -}; - -int someVar = 0; - -int main() { - SomeStruct someVar{1}; - - constexpr auto a = nameof_variable(someVar.SomeField); - constexpr auto b = nameof_variable((&someVar)->SomeField); - constexpr auto c = nameof_variable(someVar); - constexpr auto d = nameof_variable(::someVar); - constexpr auto e = nameof_variable(&SomeStruct::SomeMethod); - constexpr auto f = nameof_function(someVar.SomeMethod()); - constexpr auto g = nameof_type(SomeStruct); - - std::cout << a << std::endl; // SomeField - std::cout << b << std::endl; // SomeField - std::cout << c << std::endl; // someVar - std::cout << d << std::endl; // someVar - std::cout << e << std::endl; // SomeMethod - std::cout << f << std::endl; // SomeMethod() - std::cout << g << std::endl; // SomeStruct -} \ No newline at end of file +inline constexpr const char* template_nameof_function(const char* name, size_t length) { return template_nameof_(name, length); } \ No newline at end of file