Skip to content

Commit 968a5f2

Browse files
Anisha Rohramhdawson
authored andcommitted
doc: Add documentation for ObjectReference.md
PR-URL: #307 Reviewed-By: Michael Dawson <[email protected]>
1 parent 908cdc3 commit 968a5f2

File tree

1 file changed

+116
-4
lines changed

1 file changed

+116
-4
lines changed

doc/object_reference.md

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,117 @@
1-
# Object reference
1+
# Object Reference
2+
3+
ObjectReference is a subclass of [Reference](reference.md), and is equivalent to an instance of `Reference<Object>`. This means that an ObjectReference holds an [Object](object.md), and a count of the number of references to that Object. When the count is greater than 0, an ObjectReference is not eligible for garbage collection. This ensures that the Object being held as a value of the ObjectReference will remain accessible, even if the original Object no longer is. However, ObjectReference is unique from a Reference since properties can be set and get to the Object itself that can be accessed through the ObjectReference.
4+
5+
For more general information on references, please consult [Reference](referenc.md).
6+
7+
## Example
8+
```cpp
9+
#include <napi.h>
10+
11+
using namescape Napi;
12+
13+
void Init(Env env) {
14+
15+
// Create an empty ObjectReference that has an initial reference count of 2.
16+
ObjectReference obj_ref = Reference<Object>::New(Object::New(env), 2);
17+
18+
// Set a couple of different properties on the reference.
19+
obj_ref.Set("hello", String::New(env, "world"));
20+
obj_ref.Set(42, "The Answer to Life, the Universe, and Everything");
21+
22+
// Get the properties using the keys.
23+
Value val1 = obj_ref.Get("hello");
24+
Value val2 = obj_ref.Get(42);
25+
}
26+
```
27+
28+
## Methods
29+
30+
### Initialization
31+
32+
```cpp
33+
static ObjectReference New(const Object& value, uint32_t initialRefcount = 0);
34+
```
35+
36+
* `[in] value`: The Object which is to be referenced.
37+
38+
* `[in] initialRefcount`: The initial reference count.
39+
40+
Returns the newly created reference.
41+
42+
```cpp
43+
static ObjectReference Weak(const Object& value);
44+
```
45+
46+
Creates a "weak" reference to the value, in that the initial count of number of references is set to 0.
47+
48+
* `[in] value`: The value which is to be referenced.
49+
50+
Returns the newly created reference.
51+
52+
```cpp
53+
static ObjectReference Persistent(const Object& value);
54+
```
55+
56+
Creates a "persistent" reference to the value, in that the initial count of number of references is set to 1.
57+
58+
* `[in] value`: The value which is to be referenced.
59+
60+
Returns the newly created reference.
61+
62+
### Empty Constructor
63+
64+
```cpp
65+
ObjectReference();
66+
```
67+
68+
Returns a new _empty_ ObjectReference instance.
69+
70+
### Constructor
71+
72+
```cpp
73+
ObjectReference(napi_env env, napi_value value);
74+
```
75+
76+
* `[in] env`: The `napi_env` environment in which to construct the ObjectReference object.
77+
78+
* `[in] value`: The N-API primitive value to be held by the ObjectReference.
79+
80+
Returns the newly created reference.
81+
82+
### Set
83+
```cpp
84+
void Set(___ key, ___ value);
85+
```
86+
87+
* `[in] key`: The name for the property being assigned.
88+
89+
* `[in] value`: The value being assigned to the property.
90+
91+
The `key` can be any of the following types:
92+
- `const char*`
93+
- `const std::string`
94+
- `uint32_t`
95+
96+
The `value` can be any of the following types:
97+
- `napi_value`
98+
- `Napi::Value`
99+
- `const char*`
100+
- `bool`
101+
- `double`
102+
103+
### Get
104+
105+
```cpp
106+
Value Get(___ key);
107+
```
108+
109+
* `[in] key`: The name of the property to return the value for.
110+
111+
Returns the [Value](value.md) associated with the key property. Returns NULL if no such key exists.
112+
113+
The `key` can be any of the following types:
114+
- `const char*`
115+
- `const std::string`
116+
- `uint32_t`
2117
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/)

0 commit comments

Comments
 (0)