Skip to content

Commit

Permalink
doc: add instance data APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Schulhof <[email protected]>
Re: #567 (comment)
PR-URL: #708
Reviewed-By: Nicola Del Gobbo <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
Gabriel Schulhof committed May 4, 2020
1 parent 081cdc2 commit 381c0da
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions doc/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,58 @@ The `script` can be any of the following types:
- [`Napi::String`](string.md)
- `const char *`
- `const std::string &`
### GetInstanceData
```cpp
template <typename T> T* GetInstanceData();
```

Returns the instance data that was previously associated with the environment,
or `nullptr` if none was associated.

### SetInstanceData

```cpp
template <typename T> using Finalizer = void (*)(Env, T*);
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
void SetInstanceData(T* data);
```
- `[template] fini`: A function to call when the instance data is to be deleted.
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
not given, the default finalizer will be used, which simply uses the `delete`
operator to destroy `T*` when the addon instance is unloaded.
- `[in] data`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle.
Associates a data item stored at `T* data` with the current instance of the
addon. The item will be passed to the function `fini` which gets called when an
instance of the addon is unloaded.
### SetInstanceData
```cpp
template <typename DataType, typename HintType>
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
template <typename DataType,
typename HintType,
FinalizerWithHint<DataType, HintType> fini =
Env::DefaultFiniWithHint<DataType, HintType>>
void SetInstanceData(DataType* data, HintType* hint);
```

- `[template] fini`: A function to call when the instance data is to be deleted.
Accepts a function of the form
`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
the default finalizer will be used, which simply uses the `delete` operator to
destroy `T*` when the addon instance is unloaded.
- `[in] data`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle.
- `[in] hint`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle and will be passed as a hint to
`fini` when the addon instance is unloaded.

Associates a data item stored at `T* data` with the current instance of the
addon. The item will be passed to the function `fini` which gets called when an
instance of the addon is unloaded. This overload accepts an additional hint to
be passed to `fini`.

0 comments on commit 381c0da

Please sign in to comment.