numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
iteration_parameter_formatter.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <iterator>
23#include <optional>
24#include <string_view>
25
26#include <fmt/base.h>
27#include <fmt/format.h>
28
35
37
38namespace impl {
39
45
46} // namespace impl
47
58template <base::concepts::formattable Value>
60public:
64 iteration_parameter_formatter() noexcept = default;
65
72 void format(const Value& value, fmt::memory_buffer& buffer) const {
73 fmt::format_to(std::back_inserter(buffer), "{0}", value);
74 }
75
83 void format_with_alignment(const Value& value, index_type width,
84 fmt::memory_buffer& buffer) const {
85 fmt::format_to(std::back_inserter(buffer), "{0: >{1}}", value, width);
86 }
87};
88
99template <concepts::formattable_real_scalar Value>
101public:
105 iteration_parameter_formatter() noexcept = default;
106
113 void format(const Value& value, fmt::memory_buffer& buffer) const {
114 fmt::format_to(
115 std::back_inserter(buffer), "{0:.{1}}", value, precision_);
116 }
117
125 void format_with_alignment(const Value& value, index_type width,
126 fmt::memory_buffer& buffer) const {
127 fmt::format_to(std::back_inserter(buffer), "{0: >{1}.{2}}", value,
128 width, precision_);
129 }
130
138 if (value <= 0) [[unlikely]] {
139 throw invalid_argument("Precision must be a positive number.");
140 }
141 precision_ = value;
142 return *this;
143 }
144
150 [[nodiscard]] auto precision() const -> index_type { return precision_; }
151
152private:
156};
157
168template <concepts::formattable_iteration_parameter_value Value>
169class iteration_parameter_formatter<std::optional<Value>>
170 : public iteration_parameter_formatter<Value> {
171public:
175 iteration_parameter_formatter() noexcept = default;
176
183 void format(
184 const std::optional<Value>& value, fmt::memory_buffer& buffer) const {
185 if (value) {
187 } else {
188 buffer.append(null_string);
189 }
190 }
191
199 void format_with_alignment(const std::optional<Value>& value,
200 index_type width, fmt::memory_buffer& buffer) const {
201 if (value) {
203 *value, width, buffer);
204 } else {
205 fmt::format_to(
206 std::back_inserter(buffer), "{0: >{1}}", null_string, width);
207 }
208 }
209
210private:
212 static constexpr std::string_view null_string{"null"};
213};
214
215} // namespace num_collect::logging::iterations
Class of exception on invalid arguments.
Definition exception.h:85
void format_with_alignment(const Value &value, index_type width, fmt::memory_buffer &buffer) const
Format a value with alignment.
auto precision(index_type value) -> iteration_parameter_formatter &
Set precision.
void format_with_alignment(const std::optional< Value > &value, index_type width, fmt::memory_buffer &buffer) const
Format a value with alignment.
Class of the formatter of parameter values in iterations.
Definition of exceptions.
Definition of formattable concept.
Definition of formattable_iteration_parameter_value concept.
Definition of formattable_real_scalar class.
Definition of index_type type.
Definition of iteration_parameter_formatter class.
Namespace of fmt library.
Definition log_level.h:114
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr index_type iteration_parameter_formatter_default_precision
Default precision of floating-point values in num_collect::logging::iterations::iteration_parameter_f...
STL namespace.