Skip to content

Commit

Permalink
fix: 更新meojson,修复log类型推导错误
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Oct 20, 2023
1 parent 9e93f82 commit fcb825e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
41 changes: 23 additions & 18 deletions 3rdparty/include/meojson/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,9 @@ namespace literals
template <typename string_t = default_string_t>
const basic_value<string_t> invalid_value();

template <bool loose, typename any_t, typename string_t = default_string_t>
basic_value<string_t> serialize(any_t&& arg);
template <bool stream, typename any_t, typename string_t = default_string_t,
typename streamer_t = _serialization_helper::string_streamer<string_t>>
basic_value<string_t> serialize(any_t&& arg, streamer_t&& streamer = streamer_t());

// ******************************
// * basic_value impl *
Expand Down Expand Up @@ -2565,34 +2566,38 @@ namespace _serialization_helper
template <typename T>
constexpr bool is_sequence_container<T> = is_container<T> && !is_associative_container<T>;

template <typename input_t, typename string_t>
MEOJSON_INLINE string_t to_stream_string(input_t&& arg)
template <typename string_t>
struct string_streamer
{
using char_t = typename string_t::value_type;
using ostringstream_t = std::basic_ostringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;

ostringstream_t os;
os << std::forward<input_t>(arg);
return std::move(os).str();
}
template <typename input_t>
string_t operator()(input_t&& arg) const
{
ostringstream_t os;
os << std::forward<input_t>(arg);
return std::move(os).str();
}
};

template <bool loose, typename input_t, typename string_t>
MEOJSON_INLINE string_t to_string(input_t&& arg)
template <bool stream, typename input_t, typename string_t, typename streamer_t>
MEOJSON_INLINE string_t to_string(input_t&& arg, streamer_t&& streamer)
{
if constexpr (std::is_constructible_v<string_t, input_t>) {
return arg;
}
else if constexpr (loose) {
return to_stream_string<input_t, string_t>(std::forward<input_t>(arg));
else if constexpr (stream) {
return streamer(std::forward<input_t>(arg));
}
else {
static_assert(!sizeof(input_t), "Unable to convert type to string.");
}
}
}

template <bool loose, typename any_t, typename string_t>
MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg)
template <bool stream, typename any_t, typename string_t, typename streamer_t>
MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg, streamer_t&& streamer)
{
using namespace _serialization_helper;

Expand All @@ -2610,7 +2615,7 @@ MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg)
for (auto&& val : arg) {
using value_t = decltype(val);

result.emplace(serialize<loose, value_t, string_t>(std::forward<value_t>(val)));
result.emplace(serialize<stream, value_t, string_t>(std::forward<value_t>(val), streamer));
}
return result;
}
Expand All @@ -2620,13 +2625,13 @@ MEOJSON_INLINE basic_value<string_t> serialize(any_t&& arg)
using key_t = decltype(key);
using value_t = decltype(val);

result.emplace(to_string<loose, key_t, string_t>(std::forward<key_t>(key)),
serialize<loose, value_t, string_t>(std::forward<value_t>(val)));
result.emplace(to_string<stream, key_t, string_t>(std::forward<key_t>(key), streamer),
serialize<stream, value_t, string_t>(std::forward<value_t>(val), streamer));
}
return result;
}
else {
return to_string<loose, any_t, string_t>(std::forward<any_t>(arg));
return to_string<stream, any_t, string_t>(std::forward<any_t>(arg), streamer);
}
}

Expand Down
42 changes: 21 additions & 21 deletions source/include/Utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,33 @@ class MAA_UTILS_API Logger
}

private:
template <typename T>
decltype(auto) to_stream(T&& value)
struct string_streamer
{
if constexpr (std::same_as<std::filesystem::path, std::decay_t<T>>) {
return path_to_utf8_string(std::forward<T>(value));
}
else if constexpr (std::same_as<std::wstring, std::decay_t<T>>) {
return from_u16(std::forward<T>(value));
}
else if constexpr (has_output_operator<T>) {
return std::forward<T>(value);
}
else if constexpr (std::is_constructible_v<json::array, T>) {
return json::array(std::forward<T>(value));
template <typename T>
std::string operator()(T&& value) const
{
if constexpr (std::same_as<std::filesystem::path, std::decay_t<T>>) {
return path_to_utf8_string(std::forward<T>(value));
}
else if constexpr (std::same_as<std::wstring, std::decay_t<T>>) {
return from_u16(std::forward<T>(value));
}
else if constexpr (std::is_constructible_v<json::array, T>) {
return json::array(std::forward<T>(value)).to_string()
}
else if constexpr (std::is_constructible_v<json::object, T>) {
return json::object(std::forward<T>(value)).to_string();
}
else {
return json::serialize<true>(std::forward<T>(value), *this);
}
}
else if constexpr (std::is_constructible_v<json::object, T>) {
return json::object(std::forward<T>(value));
}
else {
return json::serialize<true>(std::forward<T>(value));
}
}
};

template <typename T>
void stream(T&& value)
{
auto&& content = to_stream(std::forward<T>(value));
auto&& content = string_streamer()(std::forward<T>(value));

#ifdef MAA_DEBUG
cout_buffer_ << content << sep_.str;
Expand Down

0 comments on commit fcb825e

Please sign in to comment.