1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_TEMPLATE_UTILS_H_
6#define V8_BASE_TEMPLATE_UTILS_H_
7
8#include <array>
9#include <memory>
10
11namespace v8 {
12namespace base {
13
14namespace detail {
15
16// make_array_helper statically iteratively creates the index list 0 .. Size-1.
17// A specialization for the base case (first index is 0) finally constructs the
18// array.
19// TODO(clemensh): Use std::index_sequence once we have C++14 support.
20template <class Function, std::size_t... Indexes>
21struct make_array_helper;
22
23template <class Function, std::size_t... Indexes>
24struct make_array_helper<Function, 0, Indexes...> {
25 constexpr static std::array<typename std::result_of<Function(size_t)>::type,
26 sizeof...(Indexes) + 1>
27 make_array(Function f) {
28 return {{f(0), f(Indexes)...}};
29 }
30};
31
32template <class Function, std::size_t FirstIndex, std::size_t... Indexes>
33struct make_array_helper<Function, FirstIndex, Indexes...>
34 : make_array_helper<Function, FirstIndex - 1, FirstIndex, Indexes...> {};
35
36} // namespace detail
37
38// base::make_array: Create an array of fixed length, initialized by a function.
39// The content of the array is created by calling the function with 0 .. Size-1.
40// Example usage to create the array {0, 2, 4}:
41// std::array<int, 3> arr = base::make_array<3>(
42// [](std::size_t i) { return static_cast<int>(2 * i); });
43// The resulting array will be constexpr if the passed function is constexpr.
44template <std::size_t Size, class Function>
45constexpr std::array<typename std::result_of<Function(size_t)>::type, Size>
46make_array(Function f) {
47 static_assert(Size > 0, "Can only create non-empty arrays");
48 return detail::make_array_helper<Function, Size - 1>::make_array(f);
49}
50
51// base::make_unique<T>: Construct an object of type T and wrap it in a
52// std::unique_ptr.
53// Replacement for C++14's std::make_unique.
54template <typename T, typename... Args>
55std::unique_ptr<T> make_unique(Args&&... args) {
56 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
57}
58
59// Helper to determine how to pass values: Pass scalars and arrays by value,
60// others by const reference (even if it was a non-const ref before; this is
61// disallowed by the style guide anyway).
62// The default is to also remove array extends (int[5] -> int*), but this can be
63// disabled by setting {remove_array_extend} to false.
64template <typename T, bool remove_array_extend = true>
65struct pass_value_or_ref {
66 using noref_t = typename std::remove_reference<T>::type;
67 using decay_t = typename std::conditional<
68 std::is_array<noref_t>::value && !remove_array_extend, noref_t,
69 typename std::decay<noref_t>::type>::type;
70 using type = typename std::conditional<std::is_scalar<decay_t>::value ||
71 std::is_array<decay_t>::value,
72 decay_t, const decay_t&>::type;
73};
74
75// Uses expression SFINAE to detect whether using operator<< would work.
76template <typename T, typename = void>
77struct has_output_operator : std::false_type {};
78template <typename T>
79struct has_output_operator<T, decltype(void(std::declval<std::ostream&>()
80 << std::declval<T>()))>
81 : std::true_type {};
82
83namespace detail {
84
85template <typename Func, typename T, typename... Ts>
86struct fold_helper {
87 static_assert(sizeof...(Ts) == 0, "this is the base case");
88 using result_t = typename std::remove_reference<T>::type;
89 static constexpr T&& fold(Func func, T&& first) {
90 return std::forward<T>(first);
91 }
92};
93
94template <typename Func, typename T1, typename T2, typename... Ts>
95struct fold_helper<Func, T1, T2, Ts...> {
96 using folded_t = typename std::result_of<Func(T1, T2)>::type;
97 using next_fold_helper = fold_helper<Func, folded_t&&, Ts...>;
98 using result_t = typename next_fold_helper::result_t;
99 static constexpr result_t fold(Func func, T1&& first, T2&& second,
100 Ts&&... more) {
101 return next_fold_helper::fold(
102 func, func(std::forward<T1>(first), std::forward<T2>(second)),
103 std::forward<Ts>(more)...);
104 }
105};
106
107} // namespace detail
108
109// Fold all arguments from left to right with a given function.
110template <typename Func, typename... Ts>
111constexpr auto fold(Func func, Ts&&... more) ->
112 typename detail::fold_helper<Func, Ts...>::result_t {
113 return detail::fold_helper<Func, Ts...>::fold(func,
114 std::forward<Ts>(more)...);
115}
116
117// {is_same<Ts...>::value} is true if all Ts are the same, false otherwise.
118template <typename... Ts>
119struct is_same : public std::false_type {};
120template <>
121struct is_same<> : public std::true_type {};
122template <typename T>
123struct is_same<T> : public std::true_type {};
124template <typename T, typename... Ts>
125struct is_same<T, T, Ts...> : public is_same<T, Ts...> {};
126
127} // namespace base
128} // namespace v8
129
130#endif // V8_BASE_TEMPLATE_UTILS_H_
131