|
1 |
| -# Reference |
2 |
| - |
3 |
| -You are reading a draft of the next documentation and it's in continuous update so |
| 1 | +You are reading a draft of the next documentation and it's in continuos update so |
4 | 2 | if you don't find what you need please refer to:
|
5 | 3 | [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/)
|
| 4 | + |
| 5 | +# Reference (template) |
| 6 | + |
| 7 | +Holds a counted reference to a [Value](value.md) object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount. |
| 8 | + |
| 9 | +The referenced Value is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the Value. |
| 10 | + |
| 11 | +Reference objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. |
| 12 | + |
| 13 | +The following classes inherit, either directly or indirectly, from Reference: |
| 14 | + |
| 15 | +* [ObjectWrap](object_wrap.md) |
| 16 | +* [ObjectReference](object_reference.md) |
| 17 | +* [FunctionReference](function_reference.md) |
| 18 | + |
| 19 | +## Methods |
| 20 | + |
| 21 | +### Factory Method |
| 22 | + |
| 23 | +```cpp |
| 24 | +static Reference<T> New(const T& value, uint32_t initialRefcount = 0); |
| 25 | +``` |
| 26 | +
|
| 27 | +* `[in] value`: The value which is to be referenced. |
| 28 | +
|
| 29 | +* `[in] initialRefcount`: The initial reference count. |
| 30 | +
|
| 31 | +### Empty Constructor |
| 32 | +
|
| 33 | +```cpp |
| 34 | +Reference(); |
| 35 | +``` |
| 36 | + |
| 37 | +Creates a new _empty_ Reference instance. |
| 38 | + |
| 39 | +### Constructor |
| 40 | + |
| 41 | +```cpp |
| 42 | +Reference(napi_env env, napi_value value); |
| 43 | +``` |
| 44 | +
|
| 45 | +* `[in] env`: The `napi_env` environment in which to construct the Reference object. |
| 46 | +
|
| 47 | +* `[in] value`: The N-API primitive value to be held by the Reference. |
| 48 | +
|
| 49 | +### Env |
| 50 | +
|
| 51 | +```cpp |
| 52 | +Napi::Env Env() const; |
| 53 | +``` |
| 54 | + |
| 55 | +Returns the `Env` value in which the Reference was instantiated. |
| 56 | + |
| 57 | +### IsEmpty |
| 58 | + |
| 59 | +```cpp |
| 60 | +bool IsEmpty() const; |
| 61 | +``` |
| 62 | + |
| 63 | +Determines whether the value held by the Reference is empty. |
| 64 | + |
| 65 | +### Value |
| 66 | + |
| 67 | +```cpp |
| 68 | +T Value() const; |
| 69 | +``` |
| 70 | + |
| 71 | +Returns the value held by the Reference. |
| 72 | + |
| 73 | +### Ref |
| 74 | + |
| 75 | +```cpp |
| 76 | +uint32_t Ref(); |
| 77 | +``` |
| 78 | + |
| 79 | +Increments the reference count for the Reference and returns the resulting reference count. Throws an error if the increment fails. |
| 80 | + |
| 81 | +### Unref |
| 82 | + |
| 83 | +```cpp |
| 84 | +uint32_t Unref(); |
| 85 | +``` |
| 86 | + |
| 87 | +Decrements the reference count for the Reference and returns the resulting reference count. Throws an error if the decrement fails. |
| 88 | + |
| 89 | +### Reset (Empty) |
| 90 | + |
| 91 | +```cpp |
| 92 | +void Reset(); |
| 93 | +``` |
| 94 | + |
| 95 | +Sets the value held by the Reference to be empty. |
| 96 | + |
| 97 | +### Reset |
| 98 | + |
| 99 | +```cpp |
| 100 | +void Reset(const T& value, uint32_t refcount = 0); |
| 101 | +``` |
| 102 | +
|
| 103 | +* `[in] value`: The value which is to be referenced. |
| 104 | +
|
| 105 | +* `[in] initialRefcount`: The initial reference count. |
| 106 | +
|
| 107 | +Sets the value held by the Reference. |
| 108 | +
|
| 109 | +### SuppressDestruct |
| 110 | +
|
| 111 | +```cpp |
| 112 | +void SuppressDestruct(); |
| 113 | +``` |
| 114 | + |
| 115 | +Call this method on a Reference that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. |
0 commit comments