based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 510a7d2b0afcfb88e852707aeebb2fed23a8176f
parent 32754b4b4d6c0686ad66e1ff868f0d32ef2f33ee
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Wed, 4 Jun 2025 12:20:39 +0200

Add format and streaming for custom types

Diffstat:
A include/based/format.hpp | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 files changed, 178 insertions(+), 0 deletions(-)


diff --git a/ include/based/format.hpp b/ include/based/format.hpp

@@ -0,0 +1,178 @@

#pragma once

#include <format>
#include <ostream>

#include "based/char/character.hpp"
#include "based/types/types.hpp"

template<>
struct std::formatter<based::character>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::character value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.chr());
}
};

template<>
struct std::formatter<based::i8>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::i8 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::i16>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::i16 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::i32>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::i32 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::i64>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::i64 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::u8>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::u8 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::u16>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::u16 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::u32>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::u32 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

template<>
struct std::formatter<based::u64>
{
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}

static auto format(based::u64 value, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}", value.value);
}
};

inline std::ostream& operator<<(std::ostream& ost, based::character value)
{
return ost << value.chr();
}

inline std::ostream& operator<<(std::ostream& ost, based::i8 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::i16 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::i32 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::i64 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::u8 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::u16 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::u32 value)
{
return ost << value.value;
}

inline std::ostream& operator<<(std::ostream& ost, based::u64 value)
{
return ost << value.value;
}