numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
iteration_parameter.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 <string>
24#include <utility>
25
26#include <fmt/base.h>
27#include <fmt/format.h>
28
35
37
38namespace impl {
39
42
43} // namespace impl
44
53template <typename Algorithm>
55public:
61 virtual void format_label_to(fmt::memory_buffer& buffer) const = 0;
62
68 virtual void format_value_to(fmt::memory_buffer& buffer) const = 0;
69
75 virtual void format_summary_to(fmt::memory_buffer& buffer) const = 0;
76
83 virtual void format_value_to(
84 fmt::memory_buffer& buffer, Algorithm* algorithm) const = 0;
85
92 virtual void format_summary_to(
93 fmt::memory_buffer& buffer, Algorithm* algorithm) const = 0;
94
100 [[nodiscard]] virtual auto label() const noexcept -> const std::string& = 0;
101
104 auto operator=(const iteration_parameter_base&)
105 -> iteration_parameter_base& = delete;
106 auto operator=(iteration_parameter_base&&)
107 -> iteration_parameter_base& = delete;
108
112 virtual ~iteration_parameter_base() noexcept = default;
113
114protected:
118 iteration_parameter_base() noexcept = default;
119};
120
134template <typename Algorithm,
135 concepts::formattable_iteration_parameter_value Value,
136 concepts::iteration_parameter_value<Algorithm, Value> ParameterValue>
137class iteration_parameter final : public iteration_parameter_base<Algorithm> {
138public:
145 iteration_parameter(std::string label, ParameterValue value)
146 : label_(std::move(label)), value_(std::move(value)) {}
147
153 void format_label_to(fmt::memory_buffer& buffer) const override {
154 fmt::format_to(std::back_inserter(buffer), "{0: >{1}}", label_, width_);
155 }
156
162 void format_value_to(fmt::memory_buffer& buffer) const override {
163 formatter_.format_with_alignment(value_.get(), width_, buffer);
164 }
165
171 void format_summary_to(fmt::memory_buffer& buffer) const override {
172 buffer.append(label_);
173 buffer.push_back('=');
174 formatter_.format(value_.get(), buffer);
175 }
176
184 fmt::memory_buffer& buffer, Algorithm* algorithm) const override {
185 formatter_.format_with_alignment(value_.get(algorithm), width_, buffer);
186 }
187
195 fmt::memory_buffer& buffer, Algorithm* algorithm) const override {
196 buffer.append(label_);
197 buffer.push_back('=');
198 formatter_.format(value_.get(algorithm), buffer);
199 }
200
206 [[nodiscard]] auto label() const noexcept -> const std::string& override {
207 return label_;
208 }
209
217 if (value <= 0) [[unlikely]] {
218 throw invalid_argument("Width must be a positive number.");
219 }
220 width_ = value;
221 return this;
222 }
223
229 [[nodiscard]] auto width() const -> index_type { return width_; }
230
237 return formatter_;
238 }
239
240private:
242 std::string label_;
243
245 ParameterValue value_;
246
249
252};
253
254} // namespace num_collect::logging::iterations
Class of exception on invalid arguments.
Definition exception.h:85
virtual void format_summary_to(fmt::memory_buffer &buffer) const =0
Format the summary.
virtual auto label() const noexcept -> const std::string &=0
Get the label of this parameter.
virtual void format_label_to(fmt::memory_buffer &buffer) const =0
Format the label with alignment.
virtual void format_value_to(fmt::memory_buffer &buffer) const =0
Format the value with alignment.
virtual void format_summary_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const =0
Format the summary.
virtual void format_value_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const =0
Format the value with alignment.
Class of the formatter of parameter values in iterations.
void format_label_to(fmt::memory_buffer &buffer) const override
Format the label with alignment.
iteration_parameter(std::string label, ParameterValue value)
Constructor.
void format_summary_to(fmt::memory_buffer &buffer) const override
Format the summary.
auto label() const noexcept -> const std::string &override
Get the label of this parameter.
void format_summary_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const override
Format the summary.
void format_value_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const override
Format the value with alignment.
void format_value_to(fmt::memory_buffer &buffer) const override
Format the value with alignment.
auto formatter() -> iteration_parameter_formatter< Value > &
Access the formatter.
auto width(index_type value) -> iteration_parameter *
Set width.
Definition of exceptions.
Definition of formattable_iteration_parameter_value concept.
Definition of index_type type.
Definition of iteration_parameter_formatter class.
Definition of iteration_parameter_formatter class.
Definition of iteration_parameter_value concept.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr index_type iteration_parameter_default_width
Default width in .num_collect::logging::iterations::iteration_parameter.
STL namespace.