Skip to content

Commit

Permalink
rename Hint to Arg; better class privatization
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinEady committed Jun 26, 2021
1 parent 370e742 commit cca2da6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
53 changes: 28 additions & 25 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,20 @@ inline Value Env::RunScript(String script) {
return Value(_env, result);
}

template <typename Hook, typename Hint>
void CleanupHook<Hook, Hint>::Wrapper(void* data) NAPI_NOEXCEPT {
template <typename Hook, typename Arg>
void Env::CleanupHook<Hook, Arg>::Wrapper(void* data) NAPI_NOEXCEPT {
auto* cleanupData =
static_cast<typename Napi::CleanupHook<Hook, Hint>::CleanupData*>(data);
static_cast<typename Napi::Env::CleanupHook<Hook, Arg>::CleanupData*>(
data);
cleanupData->hook();
delete cleanupData;
}
template <typename Hook, typename Hint>
void CleanupHook<Hook, Hint>::WrapperWithHint(void* data) NAPI_NOEXCEPT {
template <typename Hook, typename Arg>
void Env::CleanupHook<Hook, Arg>::WrapperWithArg(void* data) NAPI_NOEXCEPT {
auto* cleanupData =
static_cast<typename Napi::CleanupHook<Hook, Hint>::CleanupData*>(data);
cleanupData->hook(static_cast<Hint*>(cleanupData->hint));
static_cast<typename Napi::Env::CleanupHook<Hook, Arg>::CleanupData*>(
data);
cleanupData->hook(static_cast<Arg*>(cleanupData->arg));
delete cleanupData;
}

Expand Down Expand Up @@ -5707,41 +5709,42 @@ Addon<T>::DefineProperties(Object object,
}
#endif // NAPI_VERSION > 5

template <typename Hook, typename Hint>
CleanupHook<Hook, Hint> Env::AddCleanupHook(Hook hook, Hint* arg) {
return CleanupHook<Hook, Hint>(*this, hook, arg);
template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg> Env::AddCleanupHook(Hook hook, Arg* arg) {
return CleanupHook<Hook, Arg>(*this, hook, arg);
}

template <typename Hook>
CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
Env::CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
return CleanupHook<Hook>(*this, hook);
}

template <class Hook, class Hint>
CleanupHook<Hook, Hint>::CleanupHook(Napi::Env env, Hook hook)
: wrapper(CleanupHook<Hook, Hint>::Wrapper) {
template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
data = new CleanupData{std::move(hook), nullptr};
napi_status status = napi_add_env_cleanup_hook(env, wrapper, data);
NAPI_FATAL_IF_FAILED(status,
"CleanupHook<Hook, Hint>::CleanupHook Wrapper",
"Env::CleanupHook<Hook, Arg>::CleanupHook Wrapper",
"napi_add_env_cleanup_hook");
}

template <class Hook, class Hint>
CleanupHook<Hook, Hint>::CleanupHook(Napi::Env env, Hook hook, Hint* hint)
: wrapper(CleanupHook<Hook, Hint>::WrapperWithHint) {
data = new CleanupData{std::move(hook), hint};
template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook, Arg* arg)
: wrapper(Env::CleanupHook<Hook, Arg>::WrapperWithArg) {
data = new CleanupData{std::move(hook), arg};
napi_status status = napi_add_env_cleanup_hook(env, wrapper, data);
NAPI_FATAL_IF_FAILED(status,
"CleanupHook<Hook, Hint>::CleanupHook WrapperWithHint",
"napi_add_env_cleanup_hook");
NAPI_FATAL_IF_FAILED(
status,
"Env::CleanupHook<Hook, Arg>::CleanupHook WrapperWithArg",
"napi_add_env_cleanup_hook");
}

template <class Hook, class Hint>
void CleanupHook<Hook, Hint>::Remove(Env env) {
template <class Hook, class Arg>
void Env::CleanupHook<Hook, Arg>::Remove(Env env) {
napi_status status = napi_remove_env_cleanup_hook(env, wrapper, data);
NAPI_FATAL_IF_FAILED(status,
"CleanupHook<Hook, Hint>::Remove",
"Env::CleanupHook<Hook, Arg>::Remove",
"napi_remove_env_cleanup_hook");
delete data;
}
Expand Down
42 changes: 21 additions & 21 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ namespace Napi {
class CallbackInfo;
class TypedArray;
template <typename T> class TypedArrayOf;
template <typename Hook, typename Hint = void>
class CleanupHook;

using Int8Array =
TypedArrayOf<int8_t>; ///< Typed-array of signed 8-bit integers
Expand Down Expand Up @@ -186,6 +184,8 @@ namespace Napi {
template <typename T> static void DefaultFini(Env, T* data);
template <typename DataType, typename HintType>
static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
template <typename Hook, typename Arg = void>
class CleanupHook;
#endif // NAPI_VERSION > 5
public:
Env(napi_env env);
Expand All @@ -206,8 +206,8 @@ namespace Napi {
template <typename Hook>
CleanupHook<Hook> AddCleanupHook(Hook hook);

template <typename Hook, typename Hint>
CleanupHook<Hook, Hint> AddCleanupHook(Hook hook, Hint* hint);
template <typename Hook, typename Arg>
CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);

#if NAPI_VERSION > 5
template <typename T> T* GetInstanceData();
Expand All @@ -227,24 +227,24 @@ namespace Napi {

private:
napi_env _env;
};

template <typename Hook, typename Hint>
class CleanupHook {
public:
CleanupHook(Env env, Hook hook, Hint* hint);
CleanupHook(Env env, Hook hook);
void Remove(Env env);

private:
static inline void Wrapper(void* data) NAPI_NOEXCEPT;
static inline void WrapperWithHint(void* data) NAPI_NOEXCEPT;

void (*wrapper)(void* arg);
struct CleanupData {
Hook hook;
Hint* hint;
} * data;
template <typename Hook, typename Arg>
class CleanupHook {
public:
CleanupHook(Env env, Hook hook, Arg* arg);
CleanupHook(Env env, Hook hook);
void Remove(Env env);

private:
static inline void Wrapper(void* data) NAPI_NOEXCEPT;
static inline void WrapperWithArg(void* data) NAPI_NOEXCEPT;

void (*wrapper)(void* arg);
struct CleanupData {
Hook hook;
Arg* arg;
} * data;
};
};

/// A JavaScript value of unknown type.
Expand Down

0 comments on commit cca2da6

Please sign in to comment.