Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
monostate.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_MONOSTATE_MONOSTATE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MONOSTATE_MONOSTATE_HPP_
7
8// IWYU pragma: private, include "arene/base/monostate.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12
14#include "arene/base/stdlib_choice/cstddef.hpp"
15#include "arene/base/stdlib_choice/hash.hpp"
16#include "arene/base/stdlib_choice/is_void.hpp"
19
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
23
24namespace arene {
25namespace base {
26
27/// @brief A monostate type that is a well-behaved empty value for variants.
28struct monostate {
29 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
30 /// @brief All monostates are equal.
31 /// @return Always returns @c true
32 ARENE_NODISCARD friend constexpr auto operator==(monostate, monostate) noexcept -> bool { return true; }
33
34 /// @brief All monostates are not not equal.
35 /// @return Always returns @c false
36 ARENE_NODISCARD friend constexpr auto operator!=(monostate, monostate) noexcept -> bool { return false; }
37
38 /// @brief A monostate is never less than another monostate.
39 /// @return Always returns @c false
40 ARENE_NODISCARD friend constexpr auto operator<(monostate, monostate) noexcept -> bool { return false; }
41
42 /// @brief A monostate is always less-equal than another monostate as all are equal.
43 /// @return Always returns @c true
44 ARENE_NODISCARD friend constexpr auto operator<=(monostate, monostate) noexcept -> bool { return true; }
45
46 /// @brief A monostate is never greater than another monostate
47 /// @return Always returns @c false
48 ARENE_NODISCARD friend constexpr auto operator>(monostate, monostate) noexcept -> bool { return false; }
49
50 /// @brief A monostate is always greater-equal than another monostate as all are equal
51 /// @return Always returns @c true
52 ARENE_NODISCARD friend constexpr auto operator>=(monostate, monostate) noexcept -> bool { return true; }
53 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
54};
55
56/// @brief A metafunction that is an identity for all types except for void types.
57/// @details In the case of a void type, this results in @c arene::base::monostate with the same cv-qualifiers.
58/// @tparam T The parameter type.
59template <class T>
61 typename static_if<std::is_void<T>::value>::template then_apply_else<give_cv_to_t, T, T, monostate>;
62
63} // namespace base
64} // namespace arene
65
66// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "False positive: specialization of standard templates is permitted"
67// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "False positive: specialization of standard templates is permitted"
68namespace std {
69// parasoft-begin-suppress AUTOSAR-A11_0_2-a-2 "False positive: must follow the primary template"
70/// @brief Implementation of std::hash for arene::base::monostate.
71template <>
72struct hash<arene::base::monostate> {
73 /// @brief The result type.
75 /// @brief The argument type.
77
78 /// @brief Compute the hash of a monostate instance.
79 /// @return The hash of a monostate instance.
80 constexpr auto operator()(arene::base::monostate const&) const noexcept -> std::size_t {
81 constexpr std::size_t monostate_hash{0xEF0B0666}; // an arbitrary constant
82 return monostate_hash;
83 }
84};
85// parasoft-end-suppress AUTOSAR-A11_0_2-a-2
86} // namespace std
87// parasoft-end-suppress CERT_CPP-DCL58-a-2
88// parasoft-end-suppress AUTOSAR-A17_6_1-a-2
89
90#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MONOSTATE_MONOSTATE_HPP_
Definition accumulate.hpp:37
typename static_if< std::is_void< T >::value >::template then_apply_else< give_cv_to_t, T, T, monostate > monostate_identity
A metafunction that is an identity for all types except for void types.
Definition monostate.hpp:60
Definition accumulate.hpp:36
STL namespace.
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26
A monostate type that is a well-behaved empty value for variants.
Definition monostate.hpp:28
ARENE_NODISCARD friend constexpr auto operator==(monostate, monostate) noexcept -> bool
All monostates are equal.
Definition monostate.hpp:32
ARENE_NODISCARD friend constexpr auto operator<(monostate, monostate) noexcept -> bool
A monostate is never less than another monostate.
Definition monostate.hpp:40
ARENE_NODISCARD friend constexpr auto operator!=(monostate, monostate) noexcept -> bool
All monostates are not not equal.
Definition monostate.hpp:36
ARENE_NODISCARD friend constexpr auto operator<=(monostate, monostate) noexcept -> bool
A monostate is always less-equal than another monostate as all are equal.
Definition monostate.hpp:44
ARENE_NODISCARD friend constexpr auto operator>=(monostate, monostate) noexcept -> bool
A monostate is always greater-equal than another monostate as all are equal.
Definition monostate.hpp:52
ARENE_NODISCARD friend constexpr auto operator>(monostate, monostate) noexcept -> bool
A monostate is never greater than another monostate.
Definition monostate.hpp:48
A metaprogramming facility for selecting a branch statically without evaluating both branches apriori...
Definition static_if.hpp:48
arene::base::monostate argument_type
The argument type.
Definition monostate.hpp:76
constexpr auto operator()(arene::base::monostate const &) const noexcept -> std::size_t
Compute the hash of a monostate instance.
Definition monostate.hpp:80
std::size_t result_type
The result type.
Definition monostate.hpp:74
hash function primary template
Definition hash.hpp:179