You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.4 KiB
118 lines
2.4 KiB
2 years ago
|
/*
|
||
|
* Copyright (C) 2022 Riyyi
|
||
|
*
|
||
|
* SPDX-License-Identifier: MIT
|
||
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <cstddef> // size_t
|
||
|
#include <cstdint> // int32_t, uint8_t, uint32_t, int64_t,
|
||
|
#include <string>
|
||
|
#include <string_view>
|
||
|
#include <vector>
|
||
|
|
||
|
#include "util/format/builder.h"
|
||
|
#include "util/format/parser.h"
|
||
|
|
||
|
namespace Util::Format {
|
||
|
|
||
|
|
||
|
template<typename T>
|
||
|
struct Formatter {
|
||
|
|
||
|
void format(Builder& builder, T value) const { (void)builder, (void)value; }
|
||
|
};
|
||
|
|
||
|
// Integral
|
||
|
|
||
|
template<>
|
||
|
void Formatter<int32_t>::format(Builder& builder, int32_t value) const;
|
||
|
|
||
|
template<>
|
||
|
void Formatter<uint32_t>::format(Builder& builder, uint32_t value) const;
|
||
|
|
||
|
template<>
|
||
|
void Formatter<int64_t>::format(Builder& builder, int64_t value) const;
|
||
|
|
||
|
template<>
|
||
|
void Formatter<size_t>::format(Builder& builder, size_t value) const; // uint64_t
|
||
|
|
||
|
// Floating point
|
||
|
|
||
|
template<>
|
||
|
void Formatter<float>::format(Builder& builder, float value) const;
|
||
|
|
||
|
template<>
|
||
|
void Formatter<double>::format(Builder& builder, double value) const;
|
||
|
|
||
|
// Char
|
||
|
|
||
|
template<>
|
||
|
void Formatter<char>::format(Builder& builder, char value) const;
|
||
|
|
||
|
template<>
|
||
|
void Formatter<bool>::format(Builder& builder, bool value) const;
|
||
|
|
||
|
// String
|
||
|
|
||
|
template<>
|
||
|
void Formatter<std::string_view>::format(Builder& builder, std::string_view value) const;
|
||
|
|
||
|
template<>
|
||
|
struct Formatter<std::string> : Formatter<std::string_view> {
|
||
|
};
|
||
|
|
||
|
template<>
|
||
|
struct Formatter<const char*> : Formatter<std::string_view> {
|
||
|
void format(Builder& builder, const char* value) const;
|
||
|
};
|
||
|
|
||
|
template<>
|
||
|
struct Formatter<char*> : Formatter<const char*> {
|
||
|
};
|
||
|
|
||
|
template<size_t N>
|
||
|
struct Formatter<char[N]> : Formatter<const char*> {
|
||
|
};
|
||
|
|
||
|
// Pointer
|
||
|
|
||
|
template<typename T>
|
||
|
struct Formatter<T*> {
|
||
|
void format(Builder& builder, T* value) const
|
||
|
{
|
||
|
value == nullptr
|
||
|
? builder.putString("0x0")
|
||
|
: builder.putPointer(static_cast<const void*>(value));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
template<>
|
||
|
struct Formatter<std::nullptr_t> : Formatter<const void*> {
|
||
|
void format(Builder& builder, std::nullptr_t) const;
|
||
|
};
|
||
|
|
||
|
// Container
|
||
|
|
||
|
template<typename T>
|
||
|
struct Formatter<std::vector<T>> : Formatter<T> {
|
||
|
void format(Builder& builder, const std::vector<T>& value) const
|
||
|
{
|
||
|
builder.putString("{\n");
|
||
|
for (auto it = value.cbegin(); it != value.cend(); ++it) {
|
||
|
builder.putString(" ");
|
||
|
Formatter<T>::format(builder, *it);
|
||
|
|
||
|
// Add comma, except after the last element
|
||
|
if (it != std::prev(value.end(), 1)) {
|
||
|
builder.putCharacter(',');
|
||
|
}
|
||
|
builder.putCharacter('\n');
|
||
|
}
|
||
|
builder.putCharacter('}');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace Util::Format
|