streamable testing

main
Brett 2025-03-04 11:35:23 -05:00
parent a1c1f51cb4
commit 4e6863aafa
7 changed files with 111 additions and 50 deletions

View File

@ -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)

View File

@ -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

View File

@ -26,6 +26,8 @@
#include <cstring>
#include <functional>
#include <blt/logging/fmt_tokenizer.h>
#include <blt/std/utility.h>
#include <blt/meta/is_streamable.h>
namespace blt::logging
{
@ -104,9 +106,13 @@ namespace blt::logging
if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1)
stream << ' ';
}
} else
}
else
{
if constexpr (blt::meta::is_streamable_v<T>)
stream << t;
else
stream << "{INVALID TYPE}";
}
break;
}
@ -114,9 +120,13 @@ namespace blt::logging
if constexpr (std::is_arithmetic_v<T> || std::is_convertible_v<T, char>)
{
stream << static_cast<char>(t);
} else
}
else
{
if constexpr (blt::meta::is_streamable_v<T>)
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<T>)
stream << t;
else
stream << "{INVALID TYPE}";
}
break;
case fmt_type_t::TYPE:
stream << blt::type_string<T>();
break;
default:
handle_type(stream, type);
if constexpr (blt::meta::is_streamable_v<T>)
stream << t;
else
stream << "{INVALID TYPE}";
}
};
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#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<typename T>
class is_streamable
{
private:
template<typename Subs>
static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<Subs>(), std::true_type())
{
return std::declval<std::true_type>();
}
template<typename>
static auto test(...) -> std::false_type
{
return std::declval<std::false_type>();
}
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
template<class T>
inline constexpr bool is_streamable_v = is_streamable<T>::value;
}
#endif //BLT_META_IS_STREAMABLE_H

View File

@ -23,6 +23,7 @@
#include <utility>
#include <type_traits>
#include <ostream>
#include <blt/meta/is_streamable.h>
namespace blt::meta
{
@ -71,30 +72,6 @@ namespace blt::meta
template<typename Lambda>
lambda_helper(Lambda) -> lambda_helper<Lambda, decltype(&Lambda::operator())>;
// https://stackoverflow.com/questions/66397071/is-it-possible-to-check-if-overloaded-operator-for-type-or-class-exists
template<typename T>
class is_streamable
{
private:
template<typename Subs>
static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<Subs>(), std::true_type())
{
return std::declval<std::true_type>();
}
template<typename>
static auto test(...) -> std::false_type
{
return std::declval<std::false_type>();
}
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
template<class T>
inline constexpr bool is_streamable_v = is_streamable<T>::value;
template<typename T>
struct arrow_return
{

View File

@ -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;

View File

@ -18,6 +18,10 @@
#include <blt/logging/logging.h>
#include <blt/std/utility.h>
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");
}