LeviLamina
Loading...
Searching...
No Matches
ToString.h
1#pragma once
2
3#include <format>
4#include <ostream>
5#include <string>
6
7#include "fmt/core.h"
8#include "fmt/format.h"
9
10#include "ll/api/base/Concepts.h"
11
12namespace nonstd {
13
14template <ll::concepts::Stringable T>
15std::string to_string(T const& t) {
16 if constexpr (requires { t.toString(); }) {
17 return t.toString();
18 } else if constexpr (requires { t.to_string(); }) {
19 return t.to_string();
20 } else {
21 static_assert(ll::traits::always_false<T>, "T must be a stringable type");
22 }
23}
24
25} // namespace nonstd
26
27template <ll::concepts::Stringable T>
28std::ostream& operator<<(std::ostream& os, T const& t) {
29 return os << nonstd::to_string(t);
30}
31
32// fmt support
33template <ll::concepts::Stringable T>
34struct fmt::formatter<T> : fmt::formatter<std::string> {
35 template <class FormatContext>
36 auto format(T const& t, FormatContext& ctx) const {
37 return formatter<std::string>::format(nonstd::to_string(t), ctx);
38 }
39};
40
41// std::format support
42template <ll::concepts::Stringable T>
43struct std::formatter<T> : std::formatter<std::string> {
44 template <class FormatContext>
45 auto format(T const& t, FormatContext& ctx) const {
46 return std::formatter<std::string>::format(nonstd::to_string(t), ctx);
47 }
48};
Definition ctx.h:5