// Copyright 2017 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_BASE_TEMPLATE_UTILS_H_ #define V8_BASE_TEMPLATE_UTILS_H_ #include #include namespace v8 { namespace base { namespace detail { // make_array_helper statically iteratively creates the index list 0 .. Size-1. // A specialization for the base case (first index is 0) finally constructs the // array. // TODO(clemensh): Use std::index_sequence once we have C++14 support. template struct make_array_helper; template struct make_array_helper { constexpr static std::array::type, sizeof...(Indexes) + 1> make_array(Function f) { return {{f(0), f(Indexes)...}}; } }; template struct make_array_helper : make_array_helper {}; } // namespace detail // base::make_array: Create an array of fixed length, initialized by a function. // The content of the array is created by calling the function with 0 .. Size-1. // Example usage to create the array {0, 2, 4}: // std::array arr = base::make_array<3>( // [](std::size_t i) { return static_cast(2 * i); }); // The resulting array will be constexpr if the passed function is constexpr. template constexpr std::array::type, Size> make_array(Function f) { static_assert(Size > 0, "Can only create non-empty arrays"); return detail::make_array_helper::make_array(f); } // base::make_unique: Construct an object of type T and wrap it in a // std::unique_ptr. // Replacement for C++14's std::make_unique. template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } // Helper to determine how to pass values: Pass scalars and arrays by value, // others by const reference (even if it was a non-const ref before; this is // disallowed by the style guide anyway). // The default is to also remove array extends (int[5] -> int*), but this can be // disabled by setting {remove_array_extend} to false. template struct pass_value_or_ref { using noref_t = typename std::remove_reference::type; using decay_t = typename std::conditional< std::is_array::value && !remove_array_extend, noref_t, typename std::decay::type>::type; using type = typename std::conditional::value || std::is_array::value, decay_t, const decay_t&>::type; }; // Uses expression SFINAE to detect whether using operator<< would work. template struct has_output_operator : std::false_type {}; template struct has_output_operator() << std::declval()))> : std::true_type {}; namespace detail { template struct fold_helper { static_assert(sizeof...(Ts) == 0, "this is the base case"); using result_t = typename std::remove_reference::type; static constexpr T&& fold(Func func, T&& first) { return std::forward(first); } }; template struct fold_helper { using folded_t = typename std::result_of::type; using next_fold_helper = fold_helper; using result_t = typename next_fold_helper::result_t; static constexpr result_t fold(Func func, T1&& first, T2&& second, Ts&&... more) { return next_fold_helper::fold( func, func(std::forward(first), std::forward(second)), std::forward(more)...); } }; } // namespace detail // Fold all arguments from left to right with a given function. template constexpr auto fold(Func func, Ts&&... more) -> typename detail::fold_helper::result_t { return detail::fold_helper::fold(func, std::forward(more)...); } // {is_same::value} is true if all Ts are the same, false otherwise. template struct is_same : public std::false_type {}; template <> struct is_same<> : public std::true_type {}; template struct is_same : public std::true_type {}; template struct is_same : public is_same {}; } // namespace base } // namespace v8 #endif // V8_BASE_TEMPLATE_UTILS_H_