Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
swap.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::swap"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_SWAP_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_SWAP_HPP_
8
9// IWYU pragma: private, include "arene/base/utility.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/move.hpp"
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22namespace arene {
23namespace base {
24
25namespace swap_detail {
26
27/// @brief Poison pill definition of @c swap
28///
29/// This is to ensure that @c swap_fn_impl only looks for a function declaration
30/// and does not pick up some other global object named @c swap
31///
32// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
33template <class>
34auto swap() -> void = delete;
35// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
36
37/// @brief CPO implementation of @c arene::base::swap .
38/// @see arene::base::swap
39class swap_fn_impl {
40 public:
41 ///
42 /// @brief Overload for non-c-array types that provide ADL swap.
43 ///
44 /// @tparam T type of the first element to swap. Must satisfy <c>is_swappable_with_v<T, U></c>.
45 /// @tparam U type of the second element to swap. Must satisfy <c>is_swappable_with_v<T, U></c>.
46 /// @param lhs first element to swap.
47 /// @param rhs second element to swap.
48 /// @post @c lhs and @c rhs will have had their states exchanged via an unqualified call to @c swap.
49 ///
50 template <typename T, typename U, constraints<std::enable_if_t<is_adl_swappable_with_v<T&, U&>>> = nullptr>
51 constexpr void operator()(T& lhs, U& rhs) const noexcept(noexcept(swap(lhs, rhs))) {
52 swap(lhs, rhs);
53 }
54 ///
55 /// @brief Overload for non-c-array types that do not provide ADL swap.
56 ///
57 /// @tparam T type of the elements to swap. Must satisfy <c>is_swappable_v<T></c>.
58 /// @param lhs element to swap.
59 /// @param rhs element to swap.
60 /// @post @c lhs and @c rhs will have had their states exchanged by assigning to each other using a temp variable.
61 ///
62 template <
63 typename T,
65 nullptr>
66 constexpr void operator()(T& lhs, T& rhs) const noexcept(is_nothrow_default_swappable_v<T>) {
67 T temp{std::move(lhs)};
68 lhs = std::move(rhs);
69 rhs = std::move(temp);
70 }
71
72 ///
73 /// @brief Overload for c-array types.
74 ///
75 /// @tparam T element type of the left c-array to swap. Must satisfy <c>is_swappable_with_v<T, U></c>.
76 /// @tparam T element type of the right c-array to swap. Must satisfy <c>is_swappable_with_v<T, U></c>.
77 /// @param lhs left c-array of elements to swap.
78 /// @param rhs right c-array of elements to swap.
79 /// @post @c lhs and @c rhs will have their elements, in order, exchanged via calls to <c>arene::base::swap</c>.
80 ///
81 template <class T, class U, std::size_t N, constraints<std::enable_if_t<is_swappable_with_v<T&, U&>>> = nullptr>
82 // NOLINTNEXTLINE(hicpp-avoid-c-arrays) explicitly providing c-array support
83 constexpr void operator()(T (&lhs)[N], U (&rhs)[N]) const noexcept(is_nothrow_swappable_v<T>) {
84 for (std::size_t idx{}; idx < N; ++idx) {
85 (*this)(lhs[idx], rhs[idx]);
86 }
87 }
88};
89
90} // namespace swap_detail
91
92inline namespace swap_cpo {
93///
94/// @brief A Customization Point Object implementation of the @c constexpr compatible @c std::swap from C++17.
95///
96/// @tparam T type of the first element to swap.
97/// @tparam U type of the second element to swap.
98/// @param lhs element(s) to swap.
99/// @param rhs element(s) to swap.
100/// @post @c lhs and @c rhs will have had their states exchanged by one of the described methods.
101///
102/// Occupies a position between @c std::swap and <c>std::ranges::swap</c>. Like the latter, this is a _Customization
103/// Point Object_ (CPO) that picks up user-specified @c swap overloads using ADL, and can swap pairs of arguments which
104/// are not the same type when a custom ADL implementation exists for them. Unlike <c>std::ranges::swap</c> (but like
105/// <c>std::swap</c>), this function object is not invocable on rvalue operands.
106///
107/// As a CPO, @c arene::base::swap has the following properties:
108///
109/// 1. It supports user customizations directly, performing discovery of @c swap customizations automatically.
110/// Customizations are given the following preference:
111/// 1. ADL-discovered @c swap(lhs,rhs) , if it exists.
112/// 2. The default swap implementation, equivalent to @c std::swap .
113/// 2. It is a function object, so it can be used directly in APIs which consume functors without the need of a wrapper.
114/// 3. It will never be discoverable via ADL. This avoids ambiguous overload resolution problems with types in
115/// @c arene::base which provide customizations of @c swap .
116///
117/// This allow simplification of user code. When using @c std::swap in a generic context, best practice requires the
118/// following pattern in order to allow ADL discovery of @c swap customizations while falling back to the generic @c
119/// @c std::swap implementation for the default case:
120///
121/// @snippet docs/examples/utility_examples.cpp swap_old_usage_example
122
123// parasoft-begin-suppress CERT_CPP-DCL56-a-3 "False positive: variable is initialized"
124/// This is easy to forget. With @c arene::base::swap , this becomes simply:
125///
126/// @snippet docs/examples/utility_examples.cpp swap_new_usage_example
127///
128///
129// Marked maybe unused because gcc doesn't see through this alias being called for some reason.
130ARENE_MAYBE_UNUSED static constexpr auto const& swap =
131 ::arene::base::detail::cpp14_inline_static_const<swap_detail::swap_fn_impl>::value;
132// parasoft-end-suppress CERT_CPP-DCL56-a-3
133} // namespace swap_cpo
134
135} // namespace base
136} // namespace arene
137
138#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_SWAP_HPP_
#define ARENE_MAYBE_UNUSED
An alias for [[maybe_unused]] , or a no-op if the attribute is not supported.
Definition maybe_unused.hpp:25
Definition swap.hpp:92
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr bool is_nothrow_swappable_v
Trait query indicating if a type can be noexcept-swapped.
constexpr bool is_nothrow_default_swappable_v
Trait query if a type can be noexcept swapped the default implementation.
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
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
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