Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
expect.hpp
Go to the documentation of this file.
1
// Copyright 2024, Toyota Motor Corporation
2
//
3
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5
#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_EXPECT_HPP_
6
#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_EXPECT_HPP_
7
8
// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
9
#include "
arene/base/compiler_support/platform_queries/has_builtin.hpp
"
10
#include "arene/base/stdlib_choice/forward.hpp"
11
// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
12
13
namespace
arene
{
14
namespace
base
{
15
namespace
expect_detail {
16
/// @brief The type of the return from @c check
17
// parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "__builtin_expect requires a long parameter"
18
// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented above"
19
// NOLINTNEXTLINE(google-runtime-int)
20
using
check_return = long;
21
// parasoft-end-suppress AUTOSAR-A2_7_3
22
// parasoft-end-suppress AUTOSAR-A3_9_1-b-2
23
24
/// @brief Simple wrapper function that returns its argument cast to @c long, for use in contract checks as an argument
25
/// to @c __builtin_expect
26
/// @tparam T The type of the argument
27
/// @param arg The value to return
28
/// @return @c arg cast to @c bool and then cast to @c long
29
// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented above"
30
template
<
typename
T>
31
inline
constexpr
auto
check(T&& arg)
noexcept
-> check_return {
32
return
static_cast<
check_return
>
(
static_cast<
bool
>
(
std::forward<T>
(arg)));
33
}
34
// parasoft-end-suppress AUTOSAR-A2_7_3
35
}
// namespace expect_detail
36
}
// namespace base
37
}
// namespace arene
38
39
// parasoft-begin-suppress AUTOSAR-A16_0_1-d-2 "Conditional defines permitted by A16-0-1 Permit #2"
40
// parasoft-begin-suppress AUTOSAR-A16_0_1-a-2 "Conditional defines permitted by A16-0-1 Permit #2"
41
42
/// @brief A macro to use in an @c if statement to declare that the expression is expected to be @c true more often than
43
/// it is @c false.
44
///
45
/// Usage: @c if(ARENE_EXPECT(the_answer == 42)) { ... }
46
///
47
/// @param ... The expression to evaluate
48
/// @pre expression must be convertible to @c bool
49
50
// parasoft-begin-suppress AUTOSAR-M16_0_7-a-2 "False positive: not used as a macro"
51
#if ARENE_HAS_BUILTIN(__builtin_expect)
52
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
53
#define ARENE_EXPECT(...) (__builtin_expect(::arene::base::expect_detail::check((__VA_ARGS__)), 1) != 0)
54
#else
55
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
56
#define ARENE_EXPECT(...) (::arene::base::expect_detail::check((__VA_ARGS__)) != 0)
57
#endif
58
// parasoft-end-suppress AUTOSAR-M16_0_7-a-2
59
60
#endif
// INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_EXPECT_HPP_
has_builtin.hpp
arene::base
Definition
accumulate.hpp:37
arene
Definition
accumulate.hpp:36
std::forward
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition
forward.hpp:24
arene
base
compiler_support
expect.hpp
Generated by
1.13.2