diff --git a/src/util/format/format.h b/src/util/format/format.h index 5046fa0..1e54787 100644 --- a/src/util/format/format.h +++ b/src/util/format/format.h @@ -13,7 +13,6 @@ #include #include "util/format/formatter.h" -#include "util/format/toformat.h" namespace Util::Format { diff --git a/src/util/format/formatter.cpp b/src/util/format/formatter.cpp index ff07a24..6b8dd18 100644 --- a/src/util/format/formatter.cpp +++ b/src/util/format/formatter.cpp @@ -90,6 +90,4 @@ void Formatter::format(Builder& builder, std::nullptr_t) const Formatter::format(builder, 0); } -// Containers - } // namespace Util::Format diff --git a/src/util/format/formatter.h b/src/util/format/formatter.h index c80c232..c0d0d8d 100644 --- a/src/util/format/formatter.h +++ b/src/util/format/formatter.h @@ -114,4 +114,32 @@ struct Formatter> : Formatter { } }; +#define UTIL_FORMAT_FORMAT_AS_MAP(type) \ + template \ + struct Formatter> \ + : Formatter \ + , Formatter { \ + void format(Builder& builder, const type& value) const \ + { \ + builder.putString("{\n"); \ + auto last = value.end(); \ + for (auto it = value.begin(); it != last; ++it) { \ + builder.putString(R"( ")"); \ + Formatter::format(builder, it->first); \ + builder.putString(R"(": )"); \ + Formatter::format(builder, it->second); \ + \ + /* Add comma, except after the last element */ \ + if (std::next(it) != last) { \ + builder.putCharacter(','); \ + } \ + \ + builder.putCharacter('\n'); \ + } \ + builder.putCharacter('}'); \ + } \ + } + +UTIL_FORMAT_FORMAT_AS_MAP(std::map); +UTIL_FORMAT_FORMAT_AS_MAP(std::unordered_map); } // namespace Util::Format diff --git a/src/util/format/toformat.h b/src/util/format/toformat.h deleted file mode 100644 index 62dbfed..0000000 --- a/src/util/format/toformat.h +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2022 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#pragma once - -#include // nullptr_t, size_t -#include // int32_t, uint32_t, int64_t -#include // strlen -#include // next -#include -#include -#include -#include -#include // forward -#include - -#include "util/meta/odr.h" - -namespace Util::Format { - -namespace Detail { - -template -void format(FormatBuilder& builder, std::nullptr_t) -{ - builder.putString("(nil)"); -} - -template -void format(FormatBuilder& builder, T* pointer) -{ - builder.putPointer(static_cast(pointer)); -} - -template -void format(FormatBuilder& builder, bool boolean) -{ - builder.putString(boolean ? "true" : "false"); -} - -template -void format(FormatBuilder& builder, int32_t number) // int -{ - builder.putI32(number); -} - -template -void format(FormatBuilder& builder, uint32_t number) // unsigned int -{ - builder.putU32(number); -} - -template -void format(FormatBuilder& builder, int64_t number) // long int -{ - builder.putI64(number); -} - -template // uint64_t -void format(FormatBuilder& builder, size_t number) // long unsigned int -{ - builder.putU64(number); -} - -template -void format(FormatBuilder& builder, float number) -{ - builder.putF32(number); -} - -template -void format(FormatBuilder& builder, double number) -{ - builder.putF64(number); -} - -template -void format(FormatBuilder& builder, char character) -{ - builder.putCharacter(character); -} - -template -void format(FormatBuilder& builder, const char* string) -{ - builder.putString({ string, strlen(string) }); -} - -template -void format(FormatBuilder& builder, const std::string& string) -{ - builder.putString(string); -} - -template -void format(FormatBuilder& builder, std::string_view string) -{ - builder.putString(string); -} - -template -void format(FormatBuilder& builder, const std::vector& array) -{ - builder.putString("{\n"); - for (auto it = array.cbegin(); it != array.cend(); ++it) { - builder.putString(" "); - format(builder, *it); - - // Add comma, except after the last element - if (it != std::prev(array.end(), 1)) { - builder.putCharacter(','); - } - builder.putCharacter('\n'); - } - builder.putCharacter('}'); -} - -#define FORMAT_MAP \ - builder.putString("{\n"); \ - auto last = map.end(); \ - for (auto it = map.begin(); it != last; ++it) { \ - builder.putString(R"( ")"); \ - format(builder, it->first); \ - builder.putString(R"(": )"); \ - format(builder, it->second); \ - \ - /* Add comma, except after the last element */ \ - if (std::next(it) != last) { \ - builder.putCharacter(','); \ - } \ - \ - builder.putCharacter('\n'); \ - } \ - builder.putCharacter('}'); - -template -void format(FormatBuilder& builder, const std::map& map) -{ - FORMAT_MAP; -} - -template -void format(FormatBuilder& builder, const std::unordered_map& map) -{ - FORMAT_MAP; -} - -struct formatFunction { - template - auto operator()(FormatBuilder& builder, T&& value) const - { - return format(builder, std::forward(value)); - } -}; - -} // namespace Detail - -namespace { -constexpr const auto& _format = Util::Detail::staticConst; // NOLINT(misc-definitions-in-headers,clang-diagnostic-unused-variable) -} // namespace - -} // namespace Util::Format