NeoN
A framework for CFD software
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <stdexcept>
8#include <utility>
9#include <source_location>
10#include <sstream>
11#include <string_view>
13#include "NeoN/core/error.hpp"
14
15namespace NeoN
16{
33class Error : public std::exception
34{
35public:
36
44 Error(const std::string& file, int line, const std::string& what)
45 : what_(file + ":" + std::to_string(line) + ": " + what)
46 {}
47
52 virtual const char* what() const noexcept override { return what_.c_str(); }
53
54private:
55
56 const std::string what_;
57};
58
59
69{
70public:
71
83 const std::string& file,
84 int line,
85 const std::string& func,
86 localIdx lengthA,
87 localIdx lengthB,
88 const std::string& clarification
89 )
90 : Error(
91 file,
92 line,
93 func + ": Trying to perform binary operation " + " " + std::to_string(lengthA) + ", "
94 + std::to_string(lengthB) + " " + clarification
95 )
96 {}
97};
98
99
106#define NeoN_ASSERT_EQUAL_LENGTH(_op1, _op2) \
107 auto s1 = static_cast<localIdx>(_op1.size()); \
108 auto s2 = static_cast<localIdx>(_op2.size()); \
109 if (s1 != s2) \
110 { \
111 throw ::NeoN::DimensionMismatch( \
112 __FILE__, __LINE__, __func__, s1, s2, "expected equal dimensions" \
113 ); \
114 }
115
127inline void validateRange(std::pair<localIdx, localIdx> range, sizeType size)
128{
129 const auto begin = range.first;
130 const auto end = range.second;
131
132 if (begin < 0 || begin > end || static_cast<sizeType>(end) > size)
133 {
134 throw std::out_of_range("The chosen range is not valid!");
135 }
136}
137
151{
152 if (!std::in_range<localIdx>(value))
153 {
154 throw std::length_error("Value cannot be represented by localIdx!");
155 }
156 return static_cast<localIdx>(value);
157}
158
159inline void validateInput(
160 bool condition,
161 std::string_view message,
162 const std::source_location location = std::source_location::current()
163)
164{
165 if (condition)
166 {
167 return;
168 }
169 std::ostringstream error;
170 error << "Invalid input: " << message << "\nFile: \n"
171 << location.file_name() << "\nLine: " << location.line();
172
173 throw NeoN::NeoNException(error.str());
174}
175
176} // namespace NeoN
DimensionMismatch(const std::string &file, int line, const std::string &func, localIdx lengthA, localIdx lengthB, const std::string &clarification)
Error(const std::string &file, int line, const std::string &what)
virtual const char * what() const noexcept override
Custom exception class for NeoN.
Definition error.hpp:45
Integer types used throughout NeoN.
Definition array.hpp:18
localIdx toLocalIdx(sizeType value)
void validateRange(std::pair< localIdx, localIdx > range, sizeType size)
std::size_t sizeType
Definition label.hpp:56
void validateInput(bool condition, std::string_view message, const std::source_location location=std::source_location::current())
int32_t localIdx
Definition label.hpp:50