|
1 | 1 | # Buffer
|
2 | 2 |
|
3 |
| -You are reading a draft of the next documentation and it's in continuous update so |
4 |
| -if you don't find what you need please refer to: |
5 |
| -[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) |
| 3 | +The `Buffer` class creates a projection of raw data that can be consumed by |
| 4 | +script. |
| 5 | + |
| 6 | +## Methods |
| 7 | + |
| 8 | +### New |
| 9 | + |
| 10 | +Allocates a new `Buffer` object with a given length. |
| 11 | + |
| 12 | +```cpp |
| 13 | +static Buffer<T> New(napi_env env, size_t length); |
| 14 | +``` |
| 15 | +
|
| 16 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 17 | +- `[in] length`: The number of `T` elements to allocate. |
| 18 | +
|
| 19 | +Returns a new `Buffer` object. |
| 20 | +
|
| 21 | +### New |
| 22 | +
|
| 23 | +Wraps the provided external data into a new `Buffer` object. |
| 24 | +
|
| 25 | +The `Buffer` object does not assume ownership for the data and expects it to be |
| 26 | +valid for the lifetime of the object. Since the `Buffer` is subject to garbage |
| 27 | +collection this overload is only suitable for data which is static and never |
| 28 | +needs to be freed. |
| 29 | +
|
| 30 | +```cpp |
| 31 | +static Buffer<T> New(napi_env env, T* data, size_t length); |
| 32 | +``` |
| 33 | + |
| 34 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 35 | +- `[in] data`: The pointer to the external data to expose. |
| 36 | +- `[in] length`: The number of `T` elements in the external data. |
| 37 | + |
| 38 | +Returns a new `Buffer` object. |
| 39 | + |
| 40 | +### New |
| 41 | + |
| 42 | +Wraps the provided external data into a new `Buffer` object. |
| 43 | + |
| 44 | +The `Buffer` object does not assume ownership for the data and expects it |
| 45 | +to be valid for the lifetime of the object. The data can only be freed once the |
| 46 | +`finalizeCallback` is invoked to indicate that the `Buffer` has been released. |
| 47 | + |
| 48 | +```cpp |
| 49 | +template <typename Finalizer> |
| 50 | +static Buffer<T> New(napi_env env, |
| 51 | + T* data, |
| 52 | + size_t length, |
| 53 | + Finalizer finalizeCallback); |
| 54 | +``` |
| 55 | +
|
| 56 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 57 | +- `[in] data`: The pointer to the external data to expose. |
| 58 | +- `[in] length`: The number of `T` elements in the external data. |
| 59 | +- `[in] finalizeCallback`: The function to be called when the `Buffer` is |
| 60 | + destroyed. It must implement `operator()`, accept a `T*` (which is the |
| 61 | + external data pointer), and return `void`. |
| 62 | +
|
| 63 | +Returns a new `Buffer` object. |
| 64 | +
|
| 65 | +### New |
| 66 | +
|
| 67 | +Wraps the provided external data into a new `Buffer` object. |
| 68 | +
|
| 69 | +The `Buffer` object does not assume ownership for the data and expects it to be |
| 70 | +valid for the lifetime of the object. The data can only be freed once the |
| 71 | +`finalizeCallback` is invoked to indicate that the `Buffer` has been released. |
| 72 | +
|
| 73 | +```cpp |
| 74 | +template <typename Finalizer, typename Hint> |
| 75 | +static Buffer<T> New(napi_env env, |
| 76 | + T* data, |
| 77 | + size_t length, |
| 78 | + Finalizer finalizeCallback, |
| 79 | + Hint* finalizeHint); |
| 80 | +``` |
| 81 | + |
| 82 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 83 | +- `[in] data`: The pointer to the external data to expose. |
| 84 | +- `[in] length`: The number of `T` elements in the external data. |
| 85 | +- `[in] finalizeCallback`: The function to be called when the `Buffer` is |
| 86 | + destroyed. It must implement `operator()`, accept a `T*` (which is the |
| 87 | + external data pointer) and `Hint*`, and return `void`. |
| 88 | +- `[in] finalizeHint`: The hint to be passed as the second parameter of the |
| 89 | + finalize callback. |
| 90 | + |
| 91 | +Returns a new `Buffer` object. |
| 92 | + |
| 93 | +### Copy |
| 94 | + |
| 95 | +Allocates a new `Buffer` object and copies the provided external data into it. |
| 96 | + |
| 97 | +```cpp |
| 98 | +static Buffer<T> Copy(napi_env env, const T* data, size_t length); |
| 99 | +``` |
| 100 | +
|
| 101 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 102 | +- `[in] data`: The pointer to the external data to copy. |
| 103 | +- `[in] length`: The number of `T` elements in the external data. |
| 104 | +
|
| 105 | +Returns a new `Buffer` object containing a copy of the data. |
| 106 | +
|
| 107 | +### Constructor |
| 108 | +
|
| 109 | +Initializes an empty instance of the `Buffer` class. |
| 110 | +
|
| 111 | +```cpp |
| 112 | +Buffer(); |
| 113 | +``` |
| 114 | + |
| 115 | +### Constructor |
| 116 | + |
| 117 | +Initializes the `Buffer` object using an existing Uint8Array. |
| 118 | + |
| 119 | +```cpp |
| 120 | +Buffer(napi_env env, napi_value value); |
| 121 | +``` |
| 122 | +
|
| 123 | +- `[in] env`: The environment in which to create the `Buffer` object. |
| 124 | +- `[in] value`: The Uint8Array reference to wrap. |
| 125 | +
|
| 126 | +### Data |
| 127 | +
|
| 128 | +```cpp |
| 129 | +T* Data() const; |
| 130 | +``` |
| 131 | + |
| 132 | +Returns a pointer the external data. |
| 133 | + |
| 134 | +### Length |
| 135 | + |
| 136 | +```cpp |
| 137 | +size_t Length() const; |
| 138 | +``` |
| 139 | + |
| 140 | +Returns the number of `T` elements in the external data. |
0 commit comments