streamable testing
parent
a1c1f51cb4
commit
4e6863aafa
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
set(BLT_VERSION 5.1.6)
|
set(BLT_VERSION 5.1.7)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,8 @@ namespace blt::logging
|
||||||
HEX_FLOAT, // 'a'
|
HEX_FLOAT, // 'a'
|
||||||
EXPONENT, // 'e'
|
EXPONENT, // 'e'
|
||||||
FIXED_POINT, // 'f'
|
FIXED_POINT, // 'f'
|
||||||
GENERAL // 'g'
|
GENERAL, // 'g'
|
||||||
|
TYPE // 't'
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fmt_spec_t
|
struct fmt_spec_t
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <blt/logging/fmt_tokenizer.h>
|
#include <blt/logging/fmt_tokenizer.h>
|
||||||
|
#include <blt/std/utility.h>
|
||||||
|
#include <blt/meta/is_streamable.h>
|
||||||
|
|
||||||
namespace blt::logging
|
namespace blt::logging
|
||||||
{
|
{
|
||||||
|
@ -65,22 +67,22 @@ namespace blt::logging
|
||||||
{
|
{
|
||||||
switch (type.sign)
|
switch (type.sign)
|
||||||
{
|
{
|
||||||
case fmt_sign_t::SPACE:
|
case fmt_sign_t::SPACE:
|
||||||
if constexpr (std::is_arithmetic_v<T>)
|
if constexpr (std::is_arithmetic_v<T>)
|
||||||
{
|
{
|
||||||
if (type.type != fmt_type_t::BINARY && t >= 0)
|
if (type.type != fmt_type_t::BINARY && t >= 0)
|
||||||
stream << ' ';
|
stream << ' ';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case fmt_sign_t::PLUS:
|
case fmt_sign_t::PLUS:
|
||||||
if constexpr (std::is_arithmetic_v<T>)
|
if constexpr (std::is_arithmetic_v<T>)
|
||||||
{
|
{
|
||||||
if (t >= 0)
|
if (t >= 0)
|
||||||
stream << '+';
|
stream << '+';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case fmt_sign_t::MINUS:
|
case fmt_sign_t::MINUS:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
switch (type.type)
|
switch (type.type)
|
||||||
{
|
{
|
||||||
|
@ -104,9 +106,13 @@ namespace blt::logging
|
||||||
if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1)
|
if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1)
|
||||||
stream << ' ';
|
stream << ' ';
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
stream << t;
|
if constexpr (blt::meta::is_streamable_v<T>)
|
||||||
|
stream << t;
|
||||||
|
else
|
||||||
|
stream << "{INVALID TYPE}";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -114,9 +120,13 @@ namespace blt::logging
|
||||||
if constexpr (std::is_arithmetic_v<T> || std::is_convertible_v<T, char>)
|
if constexpr (std::is_arithmetic_v<T> || std::is_convertible_v<T, char>)
|
||||||
{
|
{
|
||||||
stream << static_cast<char>(t);
|
stream << static_cast<char>(t);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
stream << t;
|
if constexpr (blt::meta::is_streamable_v<T>)
|
||||||
|
stream << t;
|
||||||
|
else
|
||||||
|
stream << "{INVALID TYPE}";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::GENERAL:
|
case fmt_type_t::GENERAL:
|
||||||
|
@ -127,14 +137,24 @@ namespace blt::logging
|
||||||
else
|
else
|
||||||
fixed(stream);
|
fixed(stream);
|
||||||
stream << t;
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
handle_type(stream, type);
|
handle_type(stream, type);
|
||||||
stream << t;
|
if constexpr (blt::meta::is_streamable_v<T>)
|
||||||
|
stream << t;
|
||||||
|
else
|
||||||
|
stream << "{INVALID TYPE}";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
@ -23,6 +23,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <blt/meta/is_streamable.h>
|
||||||
|
|
||||||
namespace blt::meta
|
namespace blt::meta
|
||||||
{
|
{
|
||||||
|
@ -71,30 +72,6 @@ namespace blt::meta
|
||||||
template<typename Lambda>
|
template<typename Lambda>
|
||||||
lambda_helper(Lambda) -> lambda_helper<Lambda, decltype(&Lambda::operator())>;
|
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>
|
template<typename T>
|
||||||
struct arrow_return
|
struct arrow_return
|
||||||
{
|
{
|
||||||
|
|
|
@ -372,6 +372,10 @@ namespace blt::logging
|
||||||
case 'G':
|
case 'G':
|
||||||
m_spec.type = fmt_type_t::GENERAL;
|
m_spec.type = fmt_type_t::GENERAL;
|
||||||
break;
|
break;
|
||||||
|
case 't':
|
||||||
|
case 'T':
|
||||||
|
m_spec.type = fmt_type_t::TYPE;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Invalid type " << value;
|
ss << "Invalid type " << value;
|
||||||
|
|
|
@ -18,6 +18,10 @@
|
||||||
#include <blt/logging/logging.h>
|
#include <blt/logging/logging.h>
|
||||||
#include <blt/std/utility.h>
|
#include <blt/std/utility.h>
|
||||||
|
|
||||||
|
struct some_silly_type_t
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
blt::logging::println("This is a println!");
|
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 binary with space {: #b}", 6969421);
|
||||||
blt::logging::println("This is a println with octal {:#o}", 6669);
|
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 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");
|
// blt::logging::println("This is println {}\twith a std::endl in the middle of it");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue