NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/nostd/internal/absl/utility/utility.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef OTABSL_UTILITY_UTILITY_H_
19 #define OTABSL_UTILITY_UTILITY_H_
20 
21 #include <cstddef>
22 #include <cstdlib>
23 #include <tuple>
24 #include <utility>
25 
26 #include "../base/config.h"
27 #include "../base/internal/inline_variable.h"
28 #include "../base/internal/invoke.h"
29 #include "../meta/type_traits.h"
30 
31 namespace absl {
33 
34 // integer_sequence
35 //
36 // Class template representing a compile-time integer sequence. An instantiation
37 // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
38 // type through its template arguments (which is a common need when
39 // working with C++11 variadic templates). `absl::OTABSL_OPTION_NAMESPACE_NAME::integer_sequence` is designed
40 // to be a drop-in replacement for C++14's `std::integer_sequence`.
41 //
42 // Example:
43 //
44 // template< class T, T... Ints >
45 // void user_function(integer_sequence<T, Ints...>);
46 //
47 // int main()
48 // {
49 // // user_function's `T` will be deduced to `int` and `Ints...`
50 // // will be deduced to `0, 1, 2, 3, 4`.
51 // user_function(make_integer_sequence<int, 5>());
52 // }
53 template <typename T, T... Ints>
55  using value_type = T;
56  static constexpr size_t size() noexcept { return sizeof...(Ints); }
57 };
58 
59 // index_sequence
60 //
61 // A helper template for an `integer_sequence` of `size_t`,
62 // `absl::OTABSL_OPTION_NAMESPACE_NAME::index_sequence` is designed to be a drop-in replacement for C++14's
63 // `std::index_sequence`.
64 template <size_t... Ints>
65 using index_sequence = integer_sequence<size_t, Ints...>;
66 
67 namespace utility_internal {
68 
69 template <typename Seq, size_t SeqSize, size_t Rem>
70 struct Extend;
71 
72 // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
73 template <typename T, T... Ints, size_t SeqSize>
74 struct Extend<integer_sequence<T, Ints...>, SeqSize, 0> {
75  using type = integer_sequence<T, Ints..., (Ints + SeqSize)...>;
76 };
77 
78 template <typename T, T... Ints, size_t SeqSize>
79 struct Extend<integer_sequence<T, Ints...>, SeqSize, 1> {
80  using type = integer_sequence<T, Ints..., (Ints + SeqSize)..., 2 * SeqSize>;
81 };
82 
83 // Recursion helper for 'make_integer_sequence<T, N>'.
84 // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
85 template <typename T, size_t N>
86 struct Gen {
87  using type =
88  typename Extend<typename Gen<T, N / 2>::type, N / 2, N % 2>::type;
89 };
90 
91 template <typename T>
92 struct Gen<T, 0> {
94 };
95 
96 template <typename T>
98  explicit InPlaceTypeTag() = delete;
99  InPlaceTypeTag(const InPlaceTypeTag&) = delete;
100  InPlaceTypeTag& operator=(const InPlaceTypeTag&) = delete;
101 };
102 
103 template <size_t I>
105  explicit InPlaceIndexTag() = delete;
106  InPlaceIndexTag(const InPlaceIndexTag&) = delete;
107  InPlaceIndexTag& operator=(const InPlaceIndexTag&) = delete;
108 };
109 
110 } // namespace utility_internal
111 
112 // Compile-time sequences of integers
113 
114 // make_integer_sequence
115 //
116 // This template alias is equivalent to
117 // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
118 // replacement for C++14's `std::make_integer_sequence`.
119 template <typename T, T N>
121 
122 // make_index_sequence
123 //
124 // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
125 // and is designed to be a drop-in replacement for C++14's
126 // `std::make_index_sequence`.
127 template <size_t N>
129 
130 // index_sequence_for
131 //
132 // Converts a typename pack into an index sequence of the same length, and
133 // is designed to be a drop-in replacement for C++14's
134 // `std::index_sequence_for()`
135 template <typename... Ts>
137 
138 // Tag types
139 
140 #ifdef OTABSL_USES_STD_OPTIONAL
141 
142 using std::in_place_t;
143 using std::in_place;
144 
145 #else // OTABSL_USES_STD_OPTIONAL
146 
147 // in_place_t
148 //
149 // Tag type used to specify in-place construction, such as with
150 // `absl::OTABSL_OPTION_NAMESPACE_NAME::optional`, designed to be a drop-in replacement for C++17's
151 // `std::in_place_t`.
152 struct in_place_t {};
153 
155 
156 #endif // OTABSL_USES_STD_OPTIONAL
157 
158 #if defined(OTABSL_USES_STD_ANY) || defined(OTABSL_USES_STD_VARIANT)
159 using std::in_place_type;
161 #else
162 
163 // in_place_type_t
164 //
165 // Tag type used for in-place construction when the type to construct needs to
166 // be specified, such as with `absl::OTABSL_OPTION_NAMESPACE_NAME::any`, designed to be a drop-in replacement
167 // for C++17's `std::in_place_type_t`.
168 template <typename T>
170 
171 template <typename T>
173 #endif // OTABSL_USES_STD_ANY || OTABSL_USES_STD_VARIANT
174 
175 #ifdef OTABSL_USES_STD_VARIANT
176 using std::in_place_index;
178 #else
179 
180 // in_place_index_t
181 //
182 // Tag type used for in-place construction when the type to construct needs to
183 // be specified, such as with `absl::OTABSL_OPTION_NAMESPACE_NAME::any`, designed to be a drop-in replacement
184 // for C++17's `std::in_place_index_t`.
185 template <size_t I>
187 
188 template <size_t I>
190 #endif // OTABSL_USES_STD_VARIANT
191 
192 // Constexpr move and forward
193 
194 // move()
195 //
196 // A constexpr version of `std::move()`, designed to be a drop-in replacement
197 // for C++14's `std::move()`.
198 template <typename T>
199 constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t<T>&& move(T&& t) noexcept {
200  return static_cast<absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t<T>&&>(t);
201 }
202 
203 // forward()
204 //
205 // A constexpr version of `std::forward()`, designed to be a drop-in replacement
206 // for C++14's `std::forward()`.
207 template <typename T>
208 constexpr T&& forward(
209  absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t<T>& t) noexcept { // NOLINT(runtime/references)
210  return static_cast<T&&>(t);
211 }
212 
213 namespace utility_internal {
214 // Helper method for expanding tuple into a called method.
215 template <typename Functor, typename Tuple, std::size_t... Indexes>
216 auto apply_helper(Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
218  absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Functor>(functor),
219  std::get<Indexes>(absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Tuple>(t))...)) {
221  absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Functor>(functor),
222  std::get<Indexes>(absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Tuple>(t))...);
223 }
224 
225 } // namespace utility_internal
226 
227 // apply
228 //
229 // Invokes a Callable using elements of a tuple as its arguments.
230 // Each element of the tuple corresponds to an argument of the call (in order).
231 // Both the Callable argument and the tuple argument are perfect-forwarded.
232 // For member-function Callables, the first tuple element acts as the `this`
233 // pointer. `absl::OTABSL_OPTION_NAMESPACE_NAME::apply` is designed to be a drop-in replacement for C++17's
234 // `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
235 //
236 // Example:
237 //
238 // class Foo {
239 // public:
240 // void Bar(int);
241 // };
242 // void user_function1(int, std::string);
243 // void user_function2(std::unique_ptr<Foo>);
244 // auto user_lambda = [](int, int) {};
245 //
246 // int main()
247 // {
248 // std::tuple<int, std::string> tuple1(42, "bar");
249 // // Invokes the first user function on int, std::string.
250 // absl::OTABSL_OPTION_NAMESPACE_NAME::apply(&user_function1, tuple1);
251 //
252 // std::tuple<std::unique_ptr<Foo>> tuple2(absl::OTABSL_OPTION_NAMESPACE_NAME::make_unique<Foo>());
253 // // Invokes the user function that takes ownership of the unique
254 // // pointer.
255 // absl::OTABSL_OPTION_NAMESPACE_NAME::apply(&user_function2, std::move(tuple2));
256 //
257 // auto foo = absl::OTABSL_OPTION_NAMESPACE_NAME::make_unique<Foo>();
258 // std::tuple<Foo*, int> tuple3(foo.get(), 42);
259 // // Invokes the method Bar on foo with one argument, 42.
260 // absl::OTABSL_OPTION_NAMESPACE_NAME::apply(&Foo::Bar, tuple3);
261 //
262 // std::tuple<int, int> tuple4(8, 9);
263 // // Invokes a lambda.
264 // absl::OTABSL_OPTION_NAMESPACE_NAME::apply(user_lambda, tuple4);
265 // }
266 template <typename Functor, typename Tuple>
267 auto apply(Functor&& functor, Tuple&& t)
268  -> decltype(utility_internal::apply_helper(
269  absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Functor>(functor), absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Tuple>(t),
271  typename std::remove_reference<Tuple>::type>::value>{})) {
273  absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Functor>(functor), absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Tuple>(t),
275  typename std::remove_reference<Tuple>::type>::value>{});
276 }
277 
278 // exchange
279 //
280 // Replaces the value of `obj` with `new_value` and returns the old value of
281 // `obj`. `absl::OTABSL_OPTION_NAMESPACE_NAME::exchange` is designed to be a drop-in replacement for C++14's
282 // `std::exchange`.
283 //
284 // Example:
285 //
286 // Foo& operator=(Foo&& other) {
287 // ptr1_ = absl::OTABSL_OPTION_NAMESPACE_NAME::exchange(other.ptr1_, nullptr);
288 // int1_ = absl::OTABSL_OPTION_NAMESPACE_NAME::exchange(other.int1_, -1);
289 // return *this;
290 // }
291 template <typename T, typename U = T>
292 T exchange(T& obj, U&& new_value) {
293  T old_value = absl::OTABSL_OPTION_NAMESPACE_NAME::move(obj);
294  obj = absl::OTABSL_OPTION_NAMESPACE_NAME::forward<U>(new_value);
295  return old_value;
296 }
297 
298 namespace utility_internal {
299 template <typename T, typename Tuple, size_t... I>
300 T make_from_tuple_impl(Tuple&& tup, absl::OTABSL_OPTION_NAMESPACE_NAME::index_sequence<I...>) {
301  return T(std::get<I>(std::forward<Tuple>(tup))...);
302 }
303 } // namespace utility_internal
304 
305 // make_from_tuple
306 //
307 // Given the template parameter type `T` and a tuple of arguments
308 // `std::tuple(arg0, arg1, ..., argN)` constructs an object of type `T` as if by
309 // calling `T(arg0, arg1, ..., argN)`.
310 //
311 // Example:
312 //
313 // std::tuple<const char*, size_t> args("hello world", 5);
314 // auto s = absl::OTABSL_OPTION_NAMESPACE_NAME::make_from_tuple<std::string>(args);
315 // assert(s == "hello");
316 //
317 template <typename T, typename Tuple>
318 constexpr T make_from_tuple(Tuple&& tup) {
319  return utility_internal::make_from_tuple_impl<T>(
320  std::forward<Tuple>(tup),
322  std::tuple_size<absl::OTABSL_OPTION_NAMESPACE_NAME::decay_t<Tuple>>::value>{});
323 }
324 
326 } // namespace absl
327 
328 #endif // OTABSL_UTILITY_UTILITY_H_
absl::utility_internal::apply_helper
auto apply_helper(Functor &&functor, Tuple &&t, index_sequence< Indexes... >) -> decltype(absl::OTABSL_OPTION_NAMESPACE_NAME::base_internal::Invoke(absl::OTABSL_OPTION_NAMESPACE_NAME::forward< Functor >(functor), std::get< Indexes >(absl::OTABSL_OPTION_NAMESPACE_NAME::forward< Tuple >(t))...))
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:216
absl::in_place_index_t
void(*)(utility_internal::InPlaceIndexTag< I >) in_place_index_t
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:186
absl::utility_internal::InPlaceIndexTag
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:104
absl::utility_internal::InPlaceTypeTag
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:97
absl::in_place_type_t
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:169
absl::utility_internal::Extend
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:70
absl::utility_internal::InPlaceIndexTag::InPlaceIndexTag
InPlaceIndexTag()=delete
absl::integer_sequence::value_type
T value_type
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:55
absl::make_integer_sequence
typename utility_internal::Gen< T, N >::type make_integer_sequence
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:120
absl::base_internal::Invoke
InvokeT< F, Args... > Invoke(F &&f, Args &&... args)
Definition: includes/opentelemetry/nostd/internal/absl/base/internal/invoke.h:162
absl::make_from_tuple
constexpr T make_from_tuple(Tuple &&tup)
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:318
absl::utility_internal::InPlaceIndexTag::operator=
InPlaceIndexTag & operator=(const InPlaceIndexTag &)=delete
absl::make_index_sequence
make_integer_sequence< size_t, N > make_index_sequence
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:128
absl::integer_sequence::size
static constexpr size_t size() noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:56
absl::integer_sequence
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:54
absl::OTABSL_INTERNAL_INLINE_CONSTEXPR
OTABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1)
OTABSL_NAMESPACE_BEGIN
#define OTABSL_NAMESPACE_BEGIN
Definition: includes/opentelemetry/nostd/internal/absl/base/config.h:91
absl::utility_internal::Gen::type
typename Extend< typename Gen< T, N/2 >::type, N/2, N % 2 >::type type
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:88
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
absl::utility_internal::make_from_tuple_impl
T make_from_tuple_impl(Tuple &&tup, absl::OTABSL_OPTION_NAMESPACE_NAME::index_sequence< I... >)
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:300
absl::index_sequence_for
make_index_sequence< sizeof...(Ts)> index_sequence_for
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:136
absl::utility_internal::Gen
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:86
absl::in_place_type
void in_place_type(utility_internal::InPlaceTypeTag< T >)
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:172
absl::apply
auto apply(Functor &&functor, Tuple &&t) -> decltype(utility_internal::apply_helper(absl::OTABSL_OPTION_NAMESPACE_NAME::forward< Functor >(functor), absl::OTABSL_OPTION_NAMESPACE_NAME::forward< Tuple >(t), absl::OTABSL_OPTION_NAMESPACE_NAME::make_index_sequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:267
absl::utility_internal::InPlaceTypeTag::operator=
InPlaceTypeTag & operator=(const InPlaceTypeTag &)=delete
absl::utility_internal::InPlaceTypeTag::InPlaceTypeTag
InPlaceTypeTag()=delete
absl::forward
constexpr T && forward(absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > &t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:208
absl::in_place_index
void in_place_index(utility_internal::InPlaceIndexTag< I >)
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:189
absl
Definition: includes/opentelemetry/nostd/variant.h:54
absl::in_place_t
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:152
OTABSL_NAMESPACE_END
#define OTABSL_NAMESPACE_END
Definition: includes/opentelemetry/nostd/internal/absl/base/config.h:92
absl::exchange
T exchange(T &obj, U &&new_value)
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:292