11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_BASE_TEMPLATE_UTILS_H_ 61cb0ef41Sopenharmony_ci#define V8_BASE_TEMPLATE_UTILS_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <array> 91cb0ef41Sopenharmony_ci#include <functional> 101cb0ef41Sopenharmony_ci#include <iosfwd> 111cb0ef41Sopenharmony_ci#include <type_traits> 121cb0ef41Sopenharmony_ci#include <utility> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace v8 { 151cb0ef41Sopenharmony_cinamespace base { 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cinamespace detail { 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_citemplate <typename Function, std::size_t... Indexes> 201cb0ef41Sopenharmony_ciconstexpr inline auto make_array_helper(Function f, 211cb0ef41Sopenharmony_ci std::index_sequence<Indexes...>) 221cb0ef41Sopenharmony_ci -> std::array<decltype(f(0)), sizeof...(Indexes)> { 231cb0ef41Sopenharmony_ci return {{f(Indexes)...}}; 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci} // namespace detail 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci// base::make_array: Create an array of fixed length, initialized by a function. 291cb0ef41Sopenharmony_ci// The content of the array is created by calling the function with 0 .. Size-1. 301cb0ef41Sopenharmony_ci// Example usage to create the array {0, 2, 4}: 311cb0ef41Sopenharmony_ci// std::array<int, 3> arr = base::make_array<3>( 321cb0ef41Sopenharmony_ci// [](std::size_t i) { return static_cast<int>(2 * i); }); 331cb0ef41Sopenharmony_ci// The resulting array will be constexpr if the passed function is constexpr. 341cb0ef41Sopenharmony_citemplate <std::size_t Size, class Function> 351cb0ef41Sopenharmony_ciconstexpr auto make_array(Function f) { 361cb0ef41Sopenharmony_ci return detail::make_array_helper(f, std::make_index_sequence<Size>{}); 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci// Helper to determine how to pass values: Pass scalars and arrays by value, 401cb0ef41Sopenharmony_ci// others by const reference (even if it was a non-const ref before; this is 411cb0ef41Sopenharmony_ci// disallowed by the style guide anyway). 421cb0ef41Sopenharmony_ci// The default is to also remove array extends (int[5] -> int*), but this can be 431cb0ef41Sopenharmony_ci// disabled by setting {remove_array_extend} to false. 441cb0ef41Sopenharmony_citemplate <typename T, bool remove_array_extend = true> 451cb0ef41Sopenharmony_cistruct pass_value_or_ref { 461cb0ef41Sopenharmony_ci using noref_t = typename std::remove_reference<T>::type; 471cb0ef41Sopenharmony_ci using decay_t = typename std::conditional< 481cb0ef41Sopenharmony_ci std::is_array<noref_t>::value && !remove_array_extend, noref_t, 491cb0ef41Sopenharmony_ci typename std::decay<noref_t>::type>::type; 501cb0ef41Sopenharmony_ci using type = typename std::conditional<std::is_scalar<decay_t>::value || 511cb0ef41Sopenharmony_ci std::is_array<decay_t>::value, 521cb0ef41Sopenharmony_ci decay_t, const decay_t&>::type; 531cb0ef41Sopenharmony_ci}; 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci// Uses expression SFINAE to detect whether using operator<< would work. 561cb0ef41Sopenharmony_citemplate <typename T, typename TStream = std::ostream, typename = void> 571cb0ef41Sopenharmony_cistruct has_output_operator : std::false_type {}; 581cb0ef41Sopenharmony_citemplate <typename T, typename TStream> 591cb0ef41Sopenharmony_cistruct has_output_operator< 601cb0ef41Sopenharmony_ci T, TStream, decltype(void(std::declval<TStream&>() << std::declval<T>()))> 611cb0ef41Sopenharmony_ci : std::true_type {}; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci} // namespace base 641cb0ef41Sopenharmony_ci} // namespace v8 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci#endif // V8_BASE_TEMPLATE_UTILS_H_ 67