v8  10.1.124 (node 18.2.0)
V8 is Google's open source JavaScript engine
v8-memory-span.h
Go to the documentation of this file.
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 <stddef.h>
9 
10 #include "v8config.h" // NOLINT(build/include_directory)
11 
12 namespace v8 {
13 
14 /**
15  * Points to an unowned continous buffer holding a known number of elements.
16  *
17  * This is similar to std::span (under consideration for C++20), but does not
18  * require advanced C++ support. In the (far) future, this may be replaced with
19  * or aliased to std::span.
20  *
21  * To facilitate future migration, this class exposes a subset of the interface
22  * implemented by std::span.
23  */
24 template <typename T>
26  public:
27  /** The default constructor creates an empty span. */
28  constexpr MemorySpan() = default;
29 
30  constexpr MemorySpan(T* data, size_t size) : data_(data), size_(size) {}
31 
32  /** Returns a pointer to the beginning of the buffer. */
33  constexpr T* data() const { return data_; }
34  /** Returns the number of elements that the buffer holds. */
35  constexpr size_t size() const { return size_; }
36 
37  private:
38  T* data_ = nullptr;
39  size_t size_ = 0;
40 };
41 
42 } // namespace v8
43 #endif // INCLUDE_V8_MEMORY_SPAN_H_