From 35c91ec219b94b1d0d9269e6be6357f214f2e992 Mon Sep 17 00:00:00 2001 From: Cheng Date: Fri, 12 Apr 2024 10:52:12 +0900 Subject: [PATCH] Allow passing const pointers to js --- src/instance_data.h | 2 +- src/prototype.h | 17 +++++++++++++++-- src/prototype_internal.h | 12 +++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/instance_data.h b/src/instance_data.h index c82fbbc..e3a1ec7 100644 --- a/src/instance_data.h +++ b/src/instance_data.h @@ -16,7 +16,7 @@ namespace internal { // Get the base name of a type. template struct TopClass { - static constexpr const char* name = Type::name; + static constexpr const char* name = Type>::name; }; template diff --git a/src/prototype.h b/src/prototype.h index 6980590..9bbea58 100644 --- a/src/prototype.h +++ b/src/prototype.h @@ -27,8 +27,9 @@ struct Type> { // Default converter for pointers. template -struct Type::value && - std::is_class>::value>::type> { +struct Type && + std::is_class_v && + std::is_class_v>>> { static constexpr const char* name = Type::name; static inline napi_status ToNode(napi_env env, T* ptr, napi_value* result) { static_assert(internal::HasWrap::value && @@ -89,6 +90,18 @@ struct Type::value && } }; +// Default converter for const pointers. +template +struct Type && + std::is_class_v && + std::is_class_v>>> { + static constexpr const char* name = Type>::name; + static inline napi_status ToNode(napi_env env, T* ptr, napi_value* result) { + return ki::Type*>::ToNode( + env, const_cast*>(ptr), result); + } +}; + } // namespace ki #endif // SRC_PROTOTYPE_H_ diff --git a/src/prototype_internal.h b/src/prototype_internal.h index 491fb94..50b2954 100644 --- a/src/prototype_internal.h +++ b/src/prototype_internal.h @@ -38,27 +38,28 @@ template struct HasWrap : std::false_type {}; template -struct HasWrap::Wrap)>> : std::true_type {}; +struct HasWrap>::Wrap)>> + : std::true_type {}; template struct HasUnwrap : std::false_type {}; template -struct HasUnwrap::Unwrap)>> +struct HasUnwrap>::Unwrap)>> : std::true_type {}; template struct HasFinalize : std::false_type {}; template -struct HasFinalize::Finalize)>> +struct HasFinalize>::Finalize)>> : std::true_type {}; template struct HasDestructor : std::false_type {}; template -struct HasDestructor::Destructor)>> +struct HasDestructor>::Destructor)>> : std::true_type {}; // Whether the JS object can be cached using the pointer as key. @@ -70,7 +71,8 @@ template struct CanCachePointer : std::true_type {}; template -struct CanCachePointer::can_cache_pointer)>> { +struct CanCachePointer< + T, void_t>::can_cache_pointer)>> { static constexpr bool value = TypeBridge::can_cache_pointer; };