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