HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
delayed_format.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#include <format>
6#include <tuple>
7#include "forward_value.hpp"
8#include "fixed_string.hpp"
9
10#pragma once
11
12namespace tt {
13
18template<basic_fixed_string Fmt, typename... Values>
20public:
21 static_assert(std::is_same_v<decltype(Fmt)::value_type, char>, "Fmt must be a basic_fixed_string<char>");
22
23 delayed_format(delayed_format &&) noexcept = default;
24 delayed_format(delayed_format const &) noexcept = default;
25 delayed_format &operator=(delayed_format &&) noexcept = default;
26 delayed_format &operator=(delayed_format const &) noexcept = default;
27
41 template<typename... Args>
42 delayed_format(Args const &...args) noexcept : _values(args...)
43 {
44 }
45
49 [[nodiscard]] std::string operator()() const noexcept
50 {
51 return std::apply(format_wrapper<Values const &...>, std::tuple_cat(std::tuple{Fmt.c_str()}, _values));
52 }
53
58 [[nodiscard]] std::string operator()(std::locale const &loc) const noexcept
59 {
60 return std::apply(format_locale_wrapper<Values const &...>, std::tuple_cat(std::tuple{loc, Fmt.c_str()}, _values));
61 }
62
63private:
64 std::tuple<Values...> _values;
65
66 template<typename... Args>
67 static std::string format_wrapper(char const *fmt, Args const &...args)
68 {
69 return std::format(fmt, args...);
70 }
71
72 template<typename... Args>
73 static std::string format_locale_wrapper(std::locale const &loc, char const *fmt, Args const &...args)
74 {
75 return std::format(loc, fmt, args...);
76 }
77};
78
79template<basic_fixed_string Fmt, typename... Args>
80delayed_format(Args &&...) -> delayed_format<Fmt, forward_value_t<Args>...>;
81
82} // namespace tt
Delayed formatting.
Definition delayed_format.hpp:19
delayed_format(Args const &...args) noexcept
Construct a delayed format.
Definition delayed_format.hpp:42
std::string operator()() const noexcept
Format now.
Definition delayed_format.hpp:49
std::string operator()(std::locale const &loc) const noexcept
Format now.
Definition delayed_format.hpp:58
T tuple_cat(T... args)