/***************************************************************************** dbg(...) macro License (MIT): Copyright (c) 2019 David Peter 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. *****************************************************************************/ #ifndef DBG_MACRO_DBG_H #define DBG_MACRO_DBG_H #ifndef DBG_MACRO_NO_WARNING #pragma message("WARNING: the 'dbg.h' header is included in your code base") #endif // DBG_MACRO_NO_WARNING #include #include #include #include #include #include #include #include #include #include #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) #include #endif #if __cplusplus >= 201703L #include #include #endif namespace dbg_macro { #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) inline bool isColorizedOutputEnabled() { return isatty(fileno(stderr)); } #else inline bool isColorizedOutputEnabled() { return true; } #endif namespace pretty_function { // Compiler-agnostic version of __PRETTY_FUNCTION__ and constants to // extract the template argument in `type_name_impl` #if defined(__clang__) #define DBG_MACRO_PRETTY_FUNCTION __PRETTY_FUNCTION__ static constexpr size_t PREFIX_LENGTH = sizeof("const char *dbg_macro::type_name_impl() [T = ") - 1; static constexpr size_t SUFFIX_LENGTH = sizeof("]") - 1; #elif defined(__GNUC__) && !defined(__clang__) #define DBG_MACRO_PRETTY_FUNCTION __PRETTY_FUNCTION__ static constexpr size_t PREFIX_LENGTH = sizeof("const char* dbg_macro::type_name_impl() [with T = ") - 1; static constexpr size_t SUFFIX_LENGTH = sizeof("]") - 1; #elif defined(_MSC_VER) #define DBG_MACRO_PRETTY_FUNCTION __FUNCSIG__ static constexpr size_t PREFIX_LENGTH = sizeof("const char *__cdecl dbg_macro::type_name_impl<") - 1; static constexpr size_t SUFFIX_LENGTH = sizeof(">(void)") - 1; #else #error "This compiler is currently not supported by dbg_macro." #endif } // namespace pretty_function // Implementation of 'type_name()' template const char* type_name_impl() { return DBG_MACRO_PRETTY_FUNCTION; } template struct type_tag {}; template std::string get_type_name(type_tag) { namespace pf = pretty_function; std::string type = type_name_impl(); return type.substr(pf::PREFIX_LENGTH, type.size() - pf::PREFIX_LENGTH - pf::SUFFIX_LENGTH); } template std::string type_name() { if (std::is_volatile::value) { if (std::is_pointer::value) { return type_name::type>() + " volatile"; } else { return "volatile " + type_name::type>(); } } if (std::is_const::value) { if (std::is_pointer::value) { return type_name::type>() + " const"; } else { return "const " + type_name::type>(); } } if (std::is_pointer::value) { return type_name::type>() + "*"; } if (std::is_lvalue_reference::value) { return type_name::type>() + "&"; } if (std::is_rvalue_reference::value) { return type_name::type>() + "&&"; } return get_type_name(type_tag{}); } inline std::string get_type_name(type_tag) { return "short"; } inline std::string get_type_name(type_tag) { return "unsigned short"; } inline std::string get_type_name(type_tag) { return "long"; } inline std::string get_type_name(type_tag) { return "unsigned long"; } inline std::string get_type_name(type_tag) { return "std::string"; } template std::string get_type_name(type_tag>>) { return "std::vector<" + type_name() + ">"; } // Implementation of 'is_detected' to specialize for container-like types namespace detail_detector { struct nonesuch { nonesuch() = delete; ~nonesuch() = delete; nonesuch(nonesuch const&) = delete; void operator=(nonesuch const&) = delete; }; template using void_t = void; template class Op, class... Args> struct detector { using value_t = std::false_type; using type = Default; }; template class Op, class... Args> struct detector>, Op, Args...> { using value_t = std::true_type; using type = Op; }; } // namespace detail_detector template