Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/node/v8-memory-span.h
$ cat -n /usr/include/node/v8-memory-span.h 1 // Copyright 2021 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 INCLUDE_V8_MEMORY_SPAN_H_ 6 #define INCLUDE_V8_MEMORY_SPAN_H_ 7 8 #include
9 10 #include
11 #include
12 #include
13 14 #include "v8config.h" // NOLINT(build/include_directory) 15 16 namespace v8 { 17 18 /** 19 * Points to an unowned contiguous buffer holding a known number of elements. 20 * 21 * This is similar to std::span (under consideration for C++20), but does not 22 * require advanced C++ support. In the (far) future, this may be replaced with 23 * or aliased to std::span. 24 * 25 * To facilitate future migration, this class exposes a subset of the interface 26 * implemented by std::span. 27 */ 28 template
29 class V8_EXPORT MemorySpan { 30 private: 31 /** Some C++ machinery, brought from the future. */ 32 template
33 using is_array_convertible = std::is_convertible
; 34 template
35 static constexpr bool is_array_convertible_v = 36 is_array_convertible
::value; 37 38 template
39 using iter_reference_t = decltype(*std::declval
()); 40 41 template
42 struct is_compatible_iterator : std::false_type {}; 43 template
44 struct is_compatible_iterator< 45 It, 46 std::void_t< 47 std::is_base_of
::iterator_category>, 49 is_array_convertible
>, 50 T>>> : std::true_type {}; 51 template
52 static constexpr bool is_compatible_iterator_v = 53 is_compatible_iterator
::value; 54 55 template
56 static constexpr U* to_address(U* p) noexcept { 57 return p; 58 } 59 60 template
().operator->())>> 62 static constexpr auto to_address(It it) noexcept { 63 return it.operator->(); 64 } 65 66 public: 67 /** The default constructor creates an empty span. */ 68 constexpr MemorySpan() = default; 69 70 /** Constructor from nullptr and count, for backwards compatibility. 71 * This is not compatible with C++20 std::span. 72 */ 73 constexpr MemorySpan(std::nullptr_t, size_t) {} 74 75 /** Constructor from "iterator" and count. */ 76 template
, bool> = true> 78 constexpr MemorySpan(Iterator first, 79 size_t count) // NOLINT(runtime/explicit) 80 : data_(to_address(first)), size_(count) {} 81 82 /** Constructor from two "iterators". */ 83 template
&& 85 !std::is_convertible_v
, 86 bool> = true> 87 constexpr MemorySpan(Iterator first, 88 Iterator last) // NOLINT(runtime/explicit) 89 : data_(to_address(first)), size_(last - first) {} 90 91 /** Implicit conversion from C-style array. */ 92 template
93 constexpr MemorySpan(T (&a)[N]) noexcept // NOLINT(runtime/explicit) 94 : data_(a), size_(N) {} 95 96 /** Implicit conversion from std::array. */ 97 template
, bool> = true> 99 constexpr MemorySpan( 100 std::array
& a) noexcept // NOLINT(runtime/explicit) 101 : data_(a.data()), size_{N} {} 102 103 /** Implicit conversion from const std::array. */ 104 template
, bool> = true> 106 constexpr MemorySpan( 107 const std::array
& a) noexcept // NOLINT(runtime/explicit) 108 : data_(a.data()), size_{N} {} 109 110 /** Returns a pointer to the beginning of the buffer. */ 111 constexpr T* data() const { return data_; } 112 /** Returns the number of elements that the buffer holds. */ 113 constexpr size_t size() const { return size_; } 114 115 constexpr T& operator[](size_t i) const { return data_[i]; } 116 117 /** Returns true if the buffer is empty. */ 118 constexpr bool empty() const { return size() == 0; } 119 120 class Iterator { 121 public: 122 using iterator_category = std::forward_iterator_tag; 123 using value_type = T; 124 using difference_type = std::ptrdiff_t; 125 using pointer = value_type*; 126 using reference = value_type&; 127 128 T& operator*() const { return *ptr_; } 129 T* operator->() const { return ptr_; } 130 131 bool operator==(Iterator other) const { return ptr_ == other.ptr_; } 132 bool operator!=(Iterator other) const { return !(*this == other); } 133 134 Iterator& operator++() { 135 ++ptr_; 136 return *this; 137 } 138 139 Iterator operator++(int) { 140 Iterator temp(*this); 141 ++(*this); 142 return temp; 143 } 144 145 private: 146 friend class MemorySpan
; 147 148 explicit Iterator(T* ptr) : ptr_(ptr) {} 149 150 T* ptr_ = nullptr; 151 }; 152 153 Iterator begin() const { return Iterator(data_); } 154 Iterator end() const { return Iterator(data_ + size_); } 155 156 private: 157 T* data_ = nullptr; 158 size_t size_ = 0; 159 }; 160 161 /** 162 * Helper function template to create an array of fixed length, initialized by 163 * the provided initializer list, without explicitly specifying the array size, 164 * e.g. 165 * 166 * auto arr = v8::to_array
>({v8_str("one"), v8_str("two")}); 167 * 168 * In the future, this may be replaced with or aliased to std::to_array (under 169 * consideration for C++20). 170 */ 171 172 namespace detail { 173 template
174 constexpr std::array
, N> to_array_lvalue_impl( 175 T (&a)[N], std::index_sequence
) { 176 return {{a[I]...}}; 177 } 178 179 template
180 constexpr std::array
, N> to_array_rvalue_impl( 181 T (&&a)[N], std::index_sequence
) { 182 return {{std::move(a[I])...}}; 183 } 184 } // namespace detail 185 186 template
187 constexpr std::array
, N> to_array(T (&a)[N]) { 188 return detail::to_array_lvalue_impl(a, std::make_index_sequence
{}); 189 } 190 191 template
192 constexpr std::array
, N> to_array(T (&&a)[N]) { 193 return detail::to_array_rvalue_impl(std::move(a), 194 std::make_index_sequence
{}); 195 } 196 197 } // namespace v8 198 #endif // INCLUDE_V8_MEMORY_SPAN_H_
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2025 MyWebUniversity.com ™