Loading [MathJax]/extensions/tex2jax.js
MeshKernel
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages Concepts
Formatting.hpp
1//---- GPL ---------------------------------------------------------------------
2//
3// Copyright (C) Stichting Deltares, 2011-2023.
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation version 3.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17// contact: delft3d.support@deltares.nl
18// Stichting Deltares
19// P.O. Box 177
20// 2600 MH Delft, The Netherlands
21//
22// All indications and logos of, and references to, "Delft3D" and "Deltares"
23// are registered trademarks of Stichting Deltares, and remain the property of
24// Stichting Deltares. All rights reserved.
25//
26//------------------------------------------------------------------------------
27
28#pragma once
29
30#include <concepts>
31#include <string>
32#include <vector>
33
34#if USE_LIBFMT
35#include <fmt/format.h>
36namespace fmt_ns = fmt;
37#else
38#include <format>
39namespace fmt_ns = std;
40#endif
41
44template <typename T>
45concept FormattableType = std::floating_point<T> ||
46 std::integral<T> ||
47 std::same_as<T, std::string>;
48
53template <typename T>
54 requires FormattableType<T>
55struct fmt_ns::formatter<std::vector<T>> : fmt_ns::formatter<T>
56{
62 template <typename FormatContext>
63 auto format(std::vector<T> const& vector,
64 FormatContext& format_context) const
65 {
66 // get ref to the output buffer
67 auto&& out = format_context.out();
68 // write opening braces (must be escaped)
69 fmt_ns::format_to(out, "{{");
70 bool first = true;
71 for (auto const& item : vector)
72 {
73 if (first)
74 {
75 first = false;
76 }
77 else
78 {
79 fmt_ns::format_to(out, ", ");
80 }
81 fmt_ns::formatter<T>::format(item, format_context);
82 }
83 // write closing braces and return
84 return fmt_ns::format_to(out, "}}");
85 }
86};
Defines types that can be formatted.
Definition Formatting.hpp:45