From 4e6863aafabaef2691df2a4495f2dc2aad1e3581 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 4 Mar 2025 11:35:23 -0500 Subject: [PATCH] streamable testing --- CMakeLists.txt | 2 +- include/blt/logging/fmt_tokenizer.h | 3 +- include/blt/logging/logging.h | 68 +++++++++++++++++++---------- include/blt/meta/is_streamable.h | 49 +++++++++++++++++++++ include/blt/meta/meta.h | 25 +---------- src/blt/logging/fmt_tokenizer.cpp | 4 ++ tests/logger_tests.cpp | 10 +++++ 7 files changed, 111 insertions(+), 50 deletions(-) create mode 100644 include/blt/meta/is_streamable.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 07c110e..14e8a8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 5.1.6) +set(BLT_VERSION 5.1.7) set(BLT_TARGET BLT) diff --git a/include/blt/logging/fmt_tokenizer.h b/include/blt/logging/fmt_tokenizer.h index b34724b..2a81740 100644 --- a/include/blt/logging/fmt_tokenizer.h +++ b/include/blt/logging/fmt_tokenizer.h @@ -55,7 +55,8 @@ namespace blt::logging HEX_FLOAT, // 'a' EXPONENT, // 'e' FIXED_POINT, // 'f' - GENERAL // 'g' + GENERAL, // 'g' + TYPE // 't' }; struct fmt_spec_t diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index 82649f9..6e1192f 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -26,6 +26,8 @@ #include #include #include +#include +#include namespace blt::logging { @@ -65,22 +67,22 @@ namespace blt::logging { switch (type.sign) { - case fmt_sign_t::SPACE: - if constexpr (std::is_arithmetic_v) - { - if (type.type != fmt_type_t::BINARY && t >= 0) - stream << ' '; - } - break; - case fmt_sign_t::PLUS: - if constexpr (std::is_arithmetic_v) - { - if (t >= 0) - stream << '+'; - } - break; - case fmt_sign_t::MINUS: - break; + case fmt_sign_t::SPACE: + if constexpr (std::is_arithmetic_v) + { + if (type.type != fmt_type_t::BINARY && t >= 0) + stream << ' '; + } + break; + case fmt_sign_t::PLUS: + if constexpr (std::is_arithmetic_v) + { + if (t >= 0) + stream << '+'; + } + break; + case fmt_sign_t::MINUS: + break; } switch (type.type) { @@ -104,9 +106,13 @@ namespace blt::logging if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1) stream << ' '; } - } else + } + else { - stream << t; + if constexpr (blt::meta::is_streamable_v) + stream << t; + else + stream << "{INVALID TYPE}"; } break; } @@ -114,9 +120,13 @@ namespace blt::logging if constexpr (std::is_arithmetic_v || std::is_convertible_v) { stream << static_cast(t); - } else + } + else { - stream << t; + if constexpr (blt::meta::is_streamable_v) + stream << t; + else + stream << "{INVALID TYPE}"; } break; case fmt_type_t::GENERAL: @@ -127,14 +137,24 @@ namespace blt::logging else fixed(stream); stream << t; - } else - { - stream << t; } + else + { + if constexpr (blt::meta::is_streamable_v) + stream << t; + else + stream << "{INVALID TYPE}"; + } + break; + case fmt_type_t::TYPE: + stream << blt::type_string(); break; default: handle_type(stream, type); - stream << t; + if constexpr (blt::meta::is_streamable_v) + stream << t; + else + stream << "{INVALID TYPE}"; } }; } diff --git a/include/blt/meta/is_streamable.h b/include/blt/meta/is_streamable.h new file mode 100644 index 0000000..27756a0 --- /dev/null +++ b/include/blt/meta/is_streamable.h @@ -0,0 +1,49 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_META_IS_STREAMABLE_H +#define BLT_META_IS_STREAMABLE_H + +namespace blt::meta +{ + // https://stackoverflow.com/questions/66397071/is-it-possible-to-check-if-overloaded-operator-for-type-or-class-exists + template + class is_streamable + { + private: + template + static auto test(int) -> decltype(std::declval() << std::declval(), std::true_type()) + { + return std::declval(); + } + + template + static auto test(...) -> std::false_type + { + return std::declval(); + } + + public: + static constexpr bool value = decltype(test(0))::value; + }; + + template + inline constexpr bool is_streamable_v = is_streamable::value; +} + +#endif //BLT_META_IS_STREAMABLE_H diff --git a/include/blt/meta/meta.h b/include/blt/meta/meta.h index 97b2d55..82c74e7 100644 --- a/include/blt/meta/meta.h +++ b/include/blt/meta/meta.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace blt::meta { @@ -71,30 +72,6 @@ namespace blt::meta template lambda_helper(Lambda) -> lambda_helper; - // https://stackoverflow.com/questions/66397071/is-it-possible-to-check-if-overloaded-operator-for-type-or-class-exists - template - class is_streamable - { - private: - template - static auto test(int) -> decltype(std::declval() << std::declval(), std::true_type()) - { - return std::declval(); - } - - template - static auto test(...) -> std::false_type - { - return std::declval(); - } - - public: - static constexpr bool value = decltype(test(0))::value; - }; - - template - inline constexpr bool is_streamable_v = is_streamable::value; - template struct arrow_return { diff --git a/src/blt/logging/fmt_tokenizer.cpp b/src/blt/logging/fmt_tokenizer.cpp index abdfcd4..05ea4b0 100644 --- a/src/blt/logging/fmt_tokenizer.cpp +++ b/src/blt/logging/fmt_tokenizer.cpp @@ -372,6 +372,10 @@ namespace blt::logging case 'G': m_spec.type = fmt_type_t::GENERAL; break; + case 't': + case 'T': + m_spec.type = fmt_type_t::TYPE; + break; default: std::stringstream ss; ss << "Invalid type " << value; diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index ca342d6..906e835 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -18,6 +18,10 @@ #include #include +struct some_silly_type_t +{ +}; + int main() { blt::logging::println("This is a println!"); @@ -39,5 +43,11 @@ int main() blt::logging::println("This is a println with binary with space {: #b}", 6969421); blt::logging::println("This is a println with octal {:#o}", 6669); blt::logging::println("This is a println with hexfloat {:a}", 402.4320); + blt::logging::println("This is a println with exponent {:e}", 44320902.4320); + blt::logging::println("This is a println with exponent {:e}", 9532434234042340); + blt::logging::println("This is a println with exponent {:g}", 953243.49); + blt::logging::println("This is a println with exponent {:g}", 953243324023403240.49); + blt::logging::println("This is a println with a char {:c}", 66); + blt::logging::println("This is a println with type {:t}", some_silly_type_t{}); // blt::logging::println("This is println {}\twith a std::endl in the middle of it"); }