Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
copy_if.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_ALGORITHM_COPY_IF_HPP_
6
#
define
INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_COPY_IF_HPP_
7
8
// IWYU pragma: private, include "arene/base/algorithm.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
13
#
include
"arene/base/constraints/constraints.hpp"
14
#
include
"arene/base/stdlib_choice/declval.hpp"
15
#
include
"arene/base/stdlib_choice/enable_if.hpp"
16
#
include
"arene/base/stdlib_choice/is_assignable.hpp"
17
#
include
"arene/base/stdlib_choice/iterator_traits.hpp"
18
#
include
"arene/base/type_traits/is_invocable.hpp"
19
#
include
"arene/base/type_traits/iterator_category_traits.hpp"
20
#
include
"arene/base/utility/make_subrange.hpp"
21
// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23
namespace
arene
{
24
namespace
base
{
25
26
// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
27
/// @brief Copies the elements in the range, defined by <tt>[first, last)</tt>,
28
/// to another range beginning at @c output. Only copies the elements for which
29
/// the predicate @c pred returns true. The relative order of the elements that
30
/// are copied is preserved.
31
/// @tparam InputIterator The type of the Source iterator
32
/// @tparam OutputIterator The type of the Destination iterator
33
/// @tparam Predicate Function to test whether a value might be copied
34
/// @param first Start of the range of elements to copy from
35
/// @param last End (non-inclusive) of the range of elements to copy from
36
/// @param output Start of the range of elements to copy into
37
/// @param pred Unary predicate which returns @c true for the required elements
38
/// @return OutputIterator for the last inserted item
39
template
<
class
InputIterator
,
class
OutputIterator
,
class
Predicate
,
40
constraints
<
std
::
enable_if_t
<
base
::
is_input_iterator_v
<
InputIterator
>>,
41
std
::
enable_if_t
<
base
::
is_output_iterator_v
<
OutputIterator
>>,
42
std
::
enable_if_t
<
base
::
is_invocable_r_v
<
43
bool
,
Predicate
&,
typename
std
::
iterator_traits
<
InputIterator
>::
value_type
>>> =
nullptr
>
44
// clang-format off
45
constexpr
auto
copy_if
(
// CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1) CODEQLFP(A15-4-4)
46
InputIterator
first
,
InputIterator
last
,
OutputIterator
output
,
47
Predicate
pred
)
noexcept
(
noexcept
(*
output
++) &&
// CODEQLFP(EXP52-CPP)
48
noexcept
(++
first
) &&
// CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1)
49
noexcept
(*
first
) &&
// CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1)
50
noexcept
(
first
!=
last
) &&
// CODEQLFP(A5-2-6)
51
std
::
is_nothrow_assignable
<
decltype
(*
std
::
declval
<
OutputIterator
&>()),
52
typename
std
::
iterator_traits
<
InputIterator
>::
reference
>::
53
value
&&
// CODEQLFP(A5-1-1) CODEQLFP(M5-3-1)
54
base
::
is_nothrow_invocable_v
<
//
55
Predicate
&,
56
typename
std
::
iterator_traits
<
InputIterator
>::
value_type
>) ->
OutputIterator
// CODEQLFP(M5-3-1) CODEQLFP(A5-2-6)
57
// clang-format on
58
{
59
// parasoft-begin-suppress AUTOSAR-A7_1_5-a "False positive: 'element' has return type of range element, which may
60
// be non-fundamental"
61
for
(
auto
&
element
:
arene
::
base
::
make_subrange
(
first
,
last
)) {
// CODEQLFP(A7-1-1) CODEQLFP(A7-1-2)
62
if
(
pred
(
element
)) {
// CODEQLFP(M0-1-2)
63
// parasoft-begin-suppress AUTOSAR-M5_0_15-a "These are iterator types, so incrementing is ok"
64
// parasoft-begin-suppress AUTOSAR-M5_2_10-a "idiomatic iterator operations permitted by M5-2-10 Permit #1"
65
*
output
++ =
element
;
// CODEQLFP(CTR55-CPP) CODEQLFP(A4-7-1) CODEQLFP(M5-2-10)
66
// parasoft-end-suppress AUTOSAR-M5_0_15-a
67
// parasoft-end-suppress AUTOSAR-M5_2_10-a
68
}
69
}
// CODEQLFP(A5-0-2)
70
// parasoft-end-suppress AUTOSAR-A7_1_5-a
71
72
return
output
;
73
}
74
// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
75
76
}
// namespace base
77
}
// namespace arene
78
79
#
endif
// INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_COPY_IF_HPP_
arene::base
Definition
array_exceptions_disabled.cpp:11
arene
Copyright 2026, Toyota Motor Corporation.
Definition
array_exceptions_disabled.cpp:10
arene
base
algorithm
copy_if.hpp
Generated by
1.13.2