OpenTTD Source: src/core/format.hpp Source File

OpenTTD Source 15.3

format.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef FORMAT_HPP
11#define FORMAT_HPP
12
13#include "../3rdparty/fmt/format.h"
14#include "convertible_through_base.hpp"
15
16template <typename E, typename Char> requires std::is_enum_v<E>
17 struct fmt::formatter<E, Char> : fmt::formatter<typename std::underlying_type_t<E>> {
18 using underlying_type = typename std::underlying_type_t<E>;
19 using parent = typename fmt::formatter<underlying_type>;
20
21 constexpr fmt::format_parse_context::iterator parse(fmt::format_parse_context &ctx)
22 {
23 return parent::parse(ctx);
24 }
25
26 fmt::format_context::iterator format(const E &e, fmt::format_context &ctx) const
27 {
28 return parent::format(underlying_type(e), ctx);
29 }
30};
31
32template <ConvertibleThroughBase T, typename Char>
33 struct fmt::formatter<T, Char> : fmt::formatter<typename T::BaseType> {
34 using underlying_type = typename T::BaseType;
35 using parent = typename fmt::formatter<underlying_type>;
36
37 constexpr fmt::format_parse_context::iterator parse(fmt::format_parse_context &ctx)
38 {
39 return parent::parse(ctx);
40 }
41
42 fmt::format_context::iterator format(const T &t, fmt::format_context &ctx) const
43 {
44 return parent::format(t.base(), ctx);
45 }
46};
47
48template <class... Args>
49void format_append(std::string &out, fmt::format_string<Args...> &&fmt, Args&&... args)
50{
51 fmt::format_to(std::back_inserter(out), std::forward<decltype(fmt)>(fmt), std::forward<decltype(args)>(args)...);
52}
53
54#endif /* FORMAT_HPP */
convertible_through_base.hpp
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.