xref: /aosp_15_r20/system/libbase/include/android-base/collections.h (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <utility>
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker namespace android {
22*8f0ba417SAndroid Build Coastguard Worker namespace base {
23*8f0ba417SAndroid Build Coastguard Worker 
24*8f0ba417SAndroid Build Coastguard Worker // Helpers for converting a variadic template parameter pack to a homogeneous collection.
25*8f0ba417SAndroid Build Coastguard Worker // Parameters must be implictly convertible to the contained type (including via move/copy ctors).
26*8f0ba417SAndroid Build Coastguard Worker //
27*8f0ba417SAndroid Build Coastguard Worker // Use as follows:
28*8f0ba417SAndroid Build Coastguard Worker //
29*8f0ba417SAndroid Build Coastguard Worker //   template <typename... Args>
30*8f0ba417SAndroid Build Coastguard Worker //   std::vector<int> CreateVector(Args&&... args) {
31*8f0ba417SAndroid Build Coastguard Worker //     std::vector<int> result;
32*8f0ba417SAndroid Build Coastguard Worker //     Append(result, std::forward<Args>(args)...);
33*8f0ba417SAndroid Build Coastguard Worker //     return result;
34*8f0ba417SAndroid Build Coastguard Worker //   }
35*8f0ba417SAndroid Build Coastguard Worker template <typename CollectionType, typename T>
Append(CollectionType & collection,T && arg)36*8f0ba417SAndroid Build Coastguard Worker void Append(CollectionType& collection, T&& arg) {
37*8f0ba417SAndroid Build Coastguard Worker   collection.push_back(std::forward<T>(arg));
38*8f0ba417SAndroid Build Coastguard Worker }
39*8f0ba417SAndroid Build Coastguard Worker 
40*8f0ba417SAndroid Build Coastguard Worker template <typename CollectionType, typename T, typename... Args>
Append(CollectionType & collection,T && arg,Args &&...args)41*8f0ba417SAndroid Build Coastguard Worker void Append(CollectionType& collection, T&& arg, Args&&... args) {
42*8f0ba417SAndroid Build Coastguard Worker   collection.push_back(std::forward<T>(arg));
43*8f0ba417SAndroid Build Coastguard Worker   return Append(collection, std::forward<Args>(args)...);
44*8f0ba417SAndroid Build Coastguard Worker }
45*8f0ba417SAndroid Build Coastguard Worker 
46*8f0ba417SAndroid Build Coastguard Worker // Assert that all of the arguments in a variadic template parameter pack are of a given type
47*8f0ba417SAndroid Build Coastguard Worker // after std::decay.
48*8f0ba417SAndroid Build Coastguard Worker template <typename T, typename Arg, typename... Args>
AssertType(Arg &&)49*8f0ba417SAndroid Build Coastguard Worker void AssertType(Arg&&) {
50*8f0ba417SAndroid Build Coastguard Worker   static_assert(std::is_same<T, typename std::decay<Arg>::type>::value);
51*8f0ba417SAndroid Build Coastguard Worker }
52*8f0ba417SAndroid Build Coastguard Worker 
53*8f0ba417SAndroid Build Coastguard Worker template <typename T, typename Arg, typename... Args>
AssertType(Arg &&,Args &&...args)54*8f0ba417SAndroid Build Coastguard Worker void AssertType(Arg&&, Args&&... args) {
55*8f0ba417SAndroid Build Coastguard Worker   static_assert(std::is_same<T, typename std::decay<Arg>::type>::value);
56*8f0ba417SAndroid Build Coastguard Worker   AssertType<T>(std::forward<Args>(args)...);
57*8f0ba417SAndroid Build Coastguard Worker }
58*8f0ba417SAndroid Build Coastguard Worker 
59*8f0ba417SAndroid Build Coastguard Worker }  // namespace base
60*8f0ba417SAndroid Build Coastguard Worker }  // namespace android
61