Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
ebo_holder.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_TYPE_MANIPULATION_EBO_HOLDER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_EBO_HOLDER_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
11#include "arene/base/stdlib_choice/enable_if.hpp"
12#include "arene/base/stdlib_choice/forward.hpp"
13#include "arene/base/stdlib_choice/is_class.hpp"
14#include "arene/base/stdlib_choice/is_constructible.hpp"
15#include "arene/base/stdlib_choice/is_default_constructible.hpp"
16#include "arene/base/stdlib_choice/is_final.hpp"
17#include "arene/base/stdlib_choice/is_same.hpp"
18#include "arene/base/stdlib_choice/move.hpp"
19// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
20// IWYU pragma: no_include "arene/base/stdlib_choice/remove_reference.hpp"
23
24// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
25// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
26
27namespace arene {
28namespace base {
29
30// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: The single
31// argument template constructor of 'ebo_holder' does not hide copy/move
32// constructors via use of SFINAE."
33// parasoft-begin-suppress AUTOSAR-A10_2_1 "False positive: There is no inheritance, and therefore functions cannot be
34// hidden this way."
35
36/// @brief A class to hold an instance of a type that is likely to be empty, such as a comparator or allocator, making
37/// use of the empty base class optimization if possible
38/// @tparam Tag A tag type to indicate this instance, for classes that have multiple values to hold
39/// @tparam Value The type of the value to hold
40// parasoft-begin-suppress AUTOSAR-A12_1_5-a "False positive: nowhere to use a delegating constructor here"
41template <typename Tag, typename Value, bool = std::is_class<Value>::value && !std::is_final<Value>::value>
43 /// @brief The instance of the value
44 Value value_;
45
46 public:
47 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: 'value_type' does not hide anything"
48 /// @brief The type held by the EBO holder.
49 using value_type = Value;
50 // parasoft-end-suppress AUTOSAR-A2_10_1-d
51
52 /// @brief default constructor
53 /// @tparam T template parameter used to check constraints
54 ///
55 /// Value initializes the contained value.
56 template <class T = Value, constraints<std::enable_if_t<std::is_default_constructible<T>::value>> = nullptr>
57 constexpr ebo_holder() noexcept(std::is_nothrow_default_constructible<T>::value)
58 : value_{} {}
59
61 ARENE_IGNORE_ARMCLANG("-Wimplicit-int-float-conversion", "This type supports the same conversions as the Value");
62 /// @brief in-place constructor
63 /// @tparam Args types to construct from
64 /// @param args arguments to construct from
65 ///
66 /// Constructs value from @c args.
67 template <
68 class... Args,
70 std::enable_if_t<std::is_constructible<Value, Args&&...>::value>,
72 !std::is_same< //
75 >::value //
76 > //
77 > = nullptr>
78 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
79 constexpr explicit ebo_holder(Args&&... args) noexcept(std::is_nothrow_constructible<Value, Args&&...>::value)
80 : value_{std::forward<Args>(args)...} {}
82
83 // parasoft-begin-suppress AUTOSAR-A9_3_1-a "False positive: returns const handle"
84 // parasoft-begin-suppress AUTOSAR-M5_0_4-a "False positive: returns const handle"
85 // parasoft-begin-suppress AUTOSAR-M9_3_1-a-2 "False positive: returns const handle"
86 // parasoft-begin-suppress AUTOSAR-A9_3_1-b-2 "False positive: returns const handle"
87 /// @brief Get the value
88 /// @return const value_type& A reference to the held value
89 constexpr auto get_value(Tag) const& noexcept -> value_type const& { return value_; }
90 // parasoft-end-suppress AUTOSAR-M5_0_4-a
91 // parasoft-end-suppress AUTOSAR-A9_3_1-b-2
92 // parasoft-end-suppress AUTOSAR-M9_3_1-a-2
93 // parasoft-end-suppress AUTOSAR-A9_3_1-a
94
95 /// @brief Get the value
96 /// @return value_type& A reference to the held value
97 // parasoft-begin-suppress AUTOSAR-A9_3_1-a "This class doesn't contain/own the data, it wraps it with type info"
98 // parasoft-begin-suppress AUTOSAR-A9_3_1-b "This class doesn't contain/own the data, it wraps it with type info"
99 constexpr auto get_value(Tag) & noexcept -> value_type& { return value_; }
100 // parasoft-end-suppress AUTOSAR-A9_3_1-b
101 // parasoft-end-suppress AUTOSAR-A9_3_1-a
102
103 /// @brief Get the value
104 /// @return const value_type&& A reference to the held value
105 constexpr auto get_value(Tag) const&& noexcept -> value_type const&& { return std::move(value_); }
106
107 /// @brief Get the value
108 /// @return value_type&& A reference to the held value
109 constexpr auto get_value(Tag) && noexcept -> value_type&& { return std::move(value_); }
110};
111// parasoft-end-suppress AUTOSAR-A12_1_5-a
112
113/// @brief A class to hold a value, making use of the empty base class optimization if possible. This specialization is
114/// for types that can be used as a base class
115///
116/// @tparam Tag A tag type to indicate this instance, for classes that have multiple values to hold
117/// @tparam Value The type of the value to hold
118template <typename Tag, typename Value>
120 public:
121 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: 'value_type' does not hide anything"
122 /// @brief The type held by the EBO holder.
123 using value_type = Value;
124 // parasoft-end-suppress AUTOSAR-A2_10_1-d
125
126 // parasoft-begin-suppress AUTOSAR-A12_7_1-a "False positive: =default will have wrong semantics"
127
128 /// @brief default constructor
129 /// @tparam T template parameter used to check constraints
130 ///
131 /// Default constructs the contained value.
132 template <class T = Value, constraints<std::enable_if_t<std::is_default_constructible<T>::value>> = nullptr>
133 constexpr ebo_holder() noexcept(std::is_nothrow_default_constructible<T>::value)
134 : Value{} {}
135
136 // parasoft-end-suppress AUTOSAR-A12_7_1-a
137
138 /// @brief in-place constructor
139 /// @tparam Args types to construct from
140 /// @param args arguments to construct from
141 ///
142 /// Constructs value from @c args.
143 template <
144 class... Args,
146 std::enable_if_t<std::is_constructible<Value, Args&&...>::value>,
148 !std::is_same< //
149 type_list<ebo_holder>,
150 type_list<remove_cvref_t<Args>...> //
151 >::value //
152 > //
153 > = nullptr>
154 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
155 constexpr explicit ebo_holder(Args&&... args) noexcept(std::is_nothrow_constructible<Value, Args&&...>::value)
156 : Value{std::forward<Args>(args)...} {}
157
158 /// @brief Get the value
159 /// @return const value_type& A reference to the held value
160 constexpr auto get_value(Tag) const& noexcept -> value_type const& { return static_cast<value_type const&>(*this); }
161 /// @brief Get the value
162 /// @return value_type& A reference to the held value
163 constexpr auto get_value(Tag) & noexcept -> value_type& { return static_cast<value_type&>(*this); }
164 /// @brief Get the value
165 /// @return const value_type&& A reference to the held value
166 constexpr auto get_value(Tag) const&& noexcept -> value_type const&& {
167 return static_cast<value_type const&&>(*this);
168 }
169 /// @brief Get the value
170 /// @return value_type&& A reference to the held value
171 constexpr auto get_value(Tag) && noexcept -> value_type&& { return static_cast<value_type&&>(*this); }
172};
173
174// parasoft-end-suppress AUTOSAR-A14_5_1-a
175
176} // namespace base
177} // namespace arene
178
179// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
180
181#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_EBO_HOLDER_HPP_
constexpr auto get_value(Tag) &&noexcept -> value_type &&
Get the value.
Definition ebo_holder.hpp:171
constexpr auto get_value(Tag) const &&noexcept -> value_type const &&
Get the value.
Definition ebo_holder.hpp:166
constexpr auto get_value(Tag) &noexcept -> value_type &
Get the value.
Definition ebo_holder.hpp:163
Value value_type
The type held by the EBO holder.
Definition ebo_holder.hpp:123
constexpr ebo_holder() noexcept(std::is_nothrow_default_constructible< T >::value)
default constructor
Definition ebo_holder.hpp:133
constexpr ebo_holder(Args &&... args) noexcept(std::is_nothrow_constructible< Value, Args &&... >::value)
in-place constructor
Definition ebo_holder.hpp:155
constexpr auto get_value(Tag) const &noexcept -> value_type const &
Get the value.
Definition ebo_holder.hpp:160
constexpr auto get_value(Tag) &noexcept -> value_type &
Get the value.
Definition ebo_holder.hpp:99
ARENE_IGNORE_ARMCLANG("-Wimplicit-int-float-conversion", "This type supports the same conversions as the Value")
constexpr ebo_holder() noexcept(std::is_nothrow_default_constructible< T >::value)
default constructor
Definition ebo_holder.hpp:57
constexpr auto get_value(Tag) const &&noexcept -> value_type const &&
Get the value.
Definition ebo_holder.hpp:105
constexpr ebo_holder(Args &&... args) noexcept(std::is_nothrow_constructible< Value, Args &&... >::value)
in-place constructor
Definition ebo_holder.hpp:79
constexpr auto get_value(Tag) &&noexcept -> value_type &&
Get the value.
Definition ebo_holder.hpp:109
constexpr auto get_value(Tag) const &noexcept -> value_type const &
Get the value.
Definition ebo_holder.hpp:89
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types.
Definition is_constructible.hpp:29
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
Trait to detect if the two template parameters are the same or not. The value member is true if the t...
Definition is_same.hpp:35
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
STL namespace.
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
an empty struct that is a list of types - cheaper to instantiate than tuple
Definition type_list.hpp:19