LCOV - code coverage report
Current view: top level - src/base - template-utils.h (source / functions) Hit Total Coverage
Test: app.info Lines: 7 7 100.0 %
Date: 2019-04-19 Functions: 121 126 96.0 %

          Line data    Source code
       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             : 
      11             : namespace v8 {
      12             : namespace base {
      13             : 
      14             : namespace 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.
      20             : template <class Function, std::size_t... Indexes>
      21             : struct make_array_helper;
      22             : 
      23             : template <class Function, std::size_t... Indexes>
      24             : struct 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        3083 :     return {{f(0), f(Indexes)...}};
      29             :   }
      30             : };
      31             : 
      32             : template <class Function, std::size_t FirstIndex, std::size_t... Indexes>
      33             : struct 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.
      44             : template <std::size_t Size, class Function>
      45             : constexpr std::array<typename std::result_of<Function(size_t)>::type, Size>
      46             : make_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.
      54             : template <typename T, typename... Args>
      55    10057396 : std::unique_ptr<T> make_unique(Args&&... args) {
      56    63385237 :   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.
      64             : template <typename T, bool remove_array_extend = true>
      65             : struct 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.
      76             : template <typename T, typename = void>
      77             : struct has_output_operator : std::false_type {};
      78             : template <typename T>
      79             : struct has_output_operator<T, decltype(void(std::declval<std::ostream&>()
      80             :                                             << std::declval<T>()))>
      81             :     : std::true_type {};
      82             : 
      83             : namespace detail {
      84             : 
      85             : template <typename Func, typename T, typename... Ts>
      86             : struct 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             : 
      94             : template <typename Func, typename T1, typename T2, typename... Ts>
      95             : struct 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           6 :   static constexpr result_t fold(Func func, T1&& first, T2&& second,
     100             :                                  Ts&&... more) {
     101           6 :     return next_fold_helper::fold(
     102             :         func, func(std::forward<T1>(first), std::forward<T2>(second)),
     103          15 :         std::forward<Ts>(more)...);
     104             :   }
     105             : };
     106             : 
     107             : }  // namespace detail
     108             : 
     109             : // Fold all arguments from left to right with a given function.
     110             : template <typename Func, typename... Ts>
     111             : constexpr 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           2 :                                                 std::forward<Ts>(more)...);
     115             : }
     116             : 
     117             : // {is_same<Ts...>::value} is true if all Ts are the same, false otherwise.
     118             : template <typename... Ts>
     119             : struct is_same : public std::false_type {};
     120             : template <>
     121             : struct is_same<> : public std::true_type {};
     122             : template <typename T>
     123             : struct is_same<T> : public std::true_type {};
     124             : template <typename T, typename... Ts>
     125             : struct 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_

Generated by: LCOV version 1.10