Skip to content

Commit 2a1d1d9

Browse files
author
Yuuichi Asahi
committed
simplify check_precondition
1 parent a786585 commit 2a1d1d9

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

common/src/KokkosFFT_utils.hpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <Kokkos_Core.hpp>
99
#include <vector>
1010
#include <set>
11-
#include <string>
11+
#include <sstream>
1212
#include <algorithm>
1313
#include <numeric>
1414
#include "KokkosFFT_traits.hpp"
@@ -20,38 +20,38 @@
2020
__FUNCTION__)
2121
#else
2222
#include <source_location>
23-
#define KOKKOSFFT_EXPECTS(expression, msg) \
24-
KokkosFFT::Impl::check_precondition((expression), msg, \
25-
std::source_location::current())
23+
#define KOKKOSFFT_EXPECTS(expression, msg) \
24+
KokkosFFT::Impl::check_precondition( \
25+
(expression), msg, std::source_location::current().file_name(), \
26+
std::source_location::current().line(), \
27+
std::source_location::current().function_name(), \
28+
std::source_location::current().column())
2629
#endif
2730

2831
namespace KokkosFFT {
2932
namespace Impl {
3033

31-
#if defined(KOKKOS_ENABLE_CXX17)
32-
inline void check_precondition(const bool expression, const std::string& msg,
33-
const char* file_name, int line,
34-
const char* function_name) {
34+
inline void check_precondition(const bool expression,
35+
[[maybe_unused]] const std::string& msg,
36+
[[maybe_unused]] const char* file_name, int line,
37+
[[maybe_unused]] const char* function_name,
38+
[[maybe_unused]] const int column = -1) {
39+
// Quick return if possible
40+
if (expression) return;
41+
3542
std::stringstream ss("file: ");
36-
ss << file_name << '(' << line << ") `" << function_name << "`: " << msg
37-
<< '\n';
38-
if (!expression) {
39-
throw std::runtime_error(ss.str());
43+
if (column == -1) {
44+
// For C++ 17
45+
ss << file_name << '(' << line << ") `" << function_name << "`: " << msg
46+
<< '\n';
47+
} else {
48+
// For C++ 20 and later
49+
ss << file_name << '(' << line << ':' << column << ") `" << function_name
50+
<< "`: " << msg << '\n';
4051
}
52+
throw std::runtime_error(ss.str());
4153
}
42-
#else
43-
inline void check_precondition(const bool expression, const std::string& msg,
44-
std::source_location location) {
45-
std::stringstream ss("file: ");
46-
ss << location.file_name() << '(' << location.line() << ':'
47-
<< location.column() << ") `" << location.function_name() << "`: " << msg
48-
<< '\n';
4954

50-
if (!expression) {
51-
throw std::runtime_error(ss.str());
52-
}
53-
}
54-
#endif
5555
template <typename ViewType>
5656
auto convert_negative_axis(ViewType, int _axis = -1) {
5757
static_assert(Kokkos::is_view<ViewType>::value,

0 commit comments

Comments
 (0)