numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
precondition.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 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_view>
24#include <utility>
25
26#include <fmt/base.h>
27#include <fmt/format.h>
28
33
34namespace num_collect::inline base::impl {
35
44[[noreturn]] inline void handle_precondition_failure(
45 util::source_info_view source, std::string_view condition,
46 const logging::logger& logger, std::string_view description) {
47 fmt::memory_buffer buffer;
48 fmt::format_to(std::back_inserter(buffer),
49 "Precondition failed: {} (Condition: {})", description, condition);
50 logging::log_and_throw<precondition_not_satisfied>(
51 source, logger, std::string_view(buffer.data(), buffer.size()));
52}
53
61[[noreturn]] inline void handle_precondition_failure(
62 util::source_info_view source, std::string_view condition,
63 std::string_view description) {
64 logging::logger logger;
65 handle_precondition_failure(source, condition, logger, description);
66}
67
79template <typename... Args>
80 requires(sizeof...(Args) > 0)
81[[noreturn]] inline void handle_precondition_failure(
82 util::source_info_view source, std::string_view condition,
83 const logging::logger& logger,
84 fmt::format_string<Args...> description_format,
85 Args&&... description_args) {
86 fmt::memory_buffer buffer;
87 fmt::format_to(std::back_inserter(buffer), description_format,
88 std::forward<Args>(description_args)...);
89 handle_precondition_failure(source, condition, logger,
90 std::string_view(buffer.data(), buffer.size()));
91}
92
103template <typename... Args>
104 requires(sizeof...(Args) > 0)
105[[noreturn]] inline void handle_precondition_failure(
106 util::source_info_view source, std::string_view condition,
107 fmt::format_string<Args...> description_format,
108 Args&&... description_args) {
109 logging::logger logger;
110 handle_precondition_failure(source, condition, logger, description_format,
111 std::forward<Args>(description_args)...);
112}
113
114} // namespace num_collect::inline base::impl
115
116// clang-format off
136// clang-format on
137#define NUM_COLLECT_PRECONDITION(CONDITION, ...) /* NOLINT */ \
138 do { \
139 if (!(CONDITION)) [[unlikely]] { \
140 ::num_collect::base::impl::handle_precondition_failure( \
141 ::num_collect::util::source_info_view(), #CONDITION, \
142 __VA_ARGS__); \
143 } \
144 } while (false)
Definition of exceptions.
Definition of logger class.
Definition of macros for logging.
void handle_precondition_failure(util::source_info_view source, std::string_view condition, const logging::logger &logger, std::string_view description)
Handle a failure of a precondition.
Definition of source_info_view class.