diff --git a/doc/function.md b/doc/function.md index ddc089cf7..2b1cbfda7 100644 --- a/doc/function.md +++ b/doc/function.md @@ -60,7 +60,7 @@ This is the type describing a callback returning `void` that will be invoked from JavaScript. ```cpp -typedef void (*VoidCallback)(const Napi::CallbackInfo& info); +using VoidCallback = void (*)(const Napi::CallbackInfo& info); ``` ### Napi::Function::Callback @@ -70,7 +70,7 @@ from JavaScript. ```cpp -typedef Value (*Callback)(const Napi::CallbackInfo& info); +using Callback = Value (*)(const Napi::CallbackInfo& info); ``` ## Methods diff --git a/doc/property_descriptor.md b/doc/property_descriptor.md index 526d61159..793e53626 100644 --- a/doc/property_descriptor.md +++ b/doc/property_descriptor.md @@ -50,7 +50,7 @@ Void Init(Env env) { ### PropertyDescriptor::GetterCallback ```cpp -typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info); +using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info); ``` This is the signature of a getter function to be passed as a template parameter @@ -59,7 +59,7 @@ to `PropertyDescriptor::Accessor`. ### PropertyDescriptor::SetterCallback ```cpp -typedef void (*SetterCallback)(const Napi::CallbackInfo& info); +using SetterCallback = void (*)(const Napi::CallbackInfo& info); ``` This is the signature of a setter function to be passed as a template parameter diff --git a/doc/typed_array_of.md b/doc/typed_array_of.md index f0abbd125..a982b80e5 100644 --- a/doc/typed_array_of.md +++ b/doc/typed_array_of.md @@ -11,14 +11,14 @@ classes. The common JavaScript `TypedArray` types are pre-defined for each of use: ```cpp -typedef Napi::TypedArrayOf Int8Array; -typedef Napi::TypedArrayOf Uint8Array; -typedef Napi::TypedArrayOf Int16Array; -typedef Napi::TypedArrayOf Uint16Array; -typedef Napi::TypedArrayOf Int32Array; -typedef Napi::TypedArrayOf Uint32Array; -typedef Napi::TypedArrayOf Float32Array; -typedef Napi::TypedArrayOf Float64Array; +using Int8Array = Napi::TypedArrayOf; +using Uint8Array = Napi::TypedArrayOf; +using Int16Array = Napi::TypedArrayOf; +using Uint16Array = Napi::TypedArrayOf; +using Int32Array = Napi::TypedArrayOf; +using Uint32Array = Napi::TypedArrayOf; +using Float32Array = Napi::TypedArrayOf; +using Float64Array = Napi::TypedArrayOf; ``` The one exception is the `Uint8ClampedArray` which requires explicit diff --git a/doc/version_management.md b/doc/version_management.md index c67ca450e..8b1e7040b 100644 --- a/doc/version_management.md +++ b/doc/version_management.md @@ -25,7 +25,8 @@ information is stored in the `napi_node_version` structure that is defined as shown below: ```cpp -typedef struct { +using napi_node_version = +struct { uint32_t major; uint32_t minor; uint32_t patch; diff --git a/napi-inl.deprecated.h b/napi-inl.deprecated.h index f19aca76b..51e954cea 100644 --- a/napi-inl.deprecated.h +++ b/napi-inl.deprecated.h @@ -11,7 +11,7 @@ PropertyDescriptor::Accessor(const char* utf8name, Getter getter, napi_property_attributes attributes, void* /*data*/) { - typedef details::CallbackData CbData; + using CbData = details::CallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ getter, nullptr }); @@ -40,7 +40,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name, Getter getter, napi_property_attributes attributes, void* /*data*/) { - typedef details::CallbackData CbData; + using CbData = details::CallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ getter, nullptr }); @@ -71,7 +71,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(const char* utf8name, Setter setter, napi_property_attributes attributes, void* /*data*/) { - typedef details::AccessorCallbackData CbData; + using CbData = details::AccessorCallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ getter, setter, nullptr }); @@ -102,7 +102,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name, Setter setter, napi_property_attributes attributes, void* /*data*/) { - typedef details::AccessorCallbackData CbData; + using CbData = details::AccessorCallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ getter, setter, nullptr }); @@ -133,8 +133,8 @@ inline PropertyDescriptor PropertyDescriptor::Function(const char* utf8name, Callable cb, napi_property_attributes attributes, void* /*data*/) { - typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType; - typedef details::CallbackData CbData; + using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr))); + using CbData = details::CallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ cb, nullptr }); @@ -163,8 +163,8 @@ inline PropertyDescriptor PropertyDescriptor::Function(napi_value name, Callable cb, napi_property_attributes attributes, void* /*data*/) { - typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType; - typedef details::CallbackData CbData; + using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr))); + using CbData = details::CallbackData; // TODO: Delete when the function is destroyed auto callbackData = new CbData({ cb, nullptr }); diff --git a/napi-inl.h b/napi-inl.h index b12ec4ede..a5bdff854 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1989,8 +1989,8 @@ inline Function Function::New(napi_env env, Callable cb, const char* utf8name, void* data) { - typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType; - typedef details::CallbackData CbData; + using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr))); + using CbData = details::CallbackData; auto callbackData = new CbData({ cb, data }); napi_value value; @@ -3043,7 +3043,7 @@ PropertyDescriptor::Accessor(Napi::Env env, Getter getter, napi_property_attributes attributes, void* data) { - typedef details::CallbackData CbData; + using CbData = details::CallbackData; auto callbackData = new CbData({ getter, data }); napi_status status = AttachData(env, object, callbackData); @@ -3081,7 +3081,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, Getter getter, napi_property_attributes attributes, void* data) { - typedef details::CallbackData CbData; + using CbData = details::CallbackData; auto callbackData = new CbData({ getter, data }); napi_status status = AttachData(env, object, callbackData); @@ -3110,7 +3110,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, Setter setter, napi_property_attributes attributes, void* data) { - typedef details::AccessorCallbackData CbData; + using CbData = details::AccessorCallbackData; auto callbackData = new CbData({ getter, setter, data }); napi_status status = AttachData(env, object, callbackData); @@ -3150,7 +3150,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, Setter setter, napi_property_attributes attributes, void* data) { - typedef details::AccessorCallbackData CbData; + using CbData = details::AccessorCallbackData; auto callbackData = new CbData({ getter, setter, data }); napi_status status = AttachData(env, object, callbackData); diff --git a/napi.h b/napi.h index 485176157..3db1b6da6 100644 --- a/napi.h +++ b/napi.h @@ -136,21 +136,31 @@ namespace Napi { class TypedArray; template class TypedArrayOf; - typedef TypedArrayOf Int8Array; ///< Typed-array of signed 8-bit integers - typedef TypedArrayOf Uint8Array; ///< Typed-array of unsigned 8-bit integers - typedef TypedArrayOf Int16Array; ///< Typed-array of signed 16-bit integers - typedef TypedArrayOf Uint16Array; ///< Typed-array of unsigned 16-bit integers - typedef TypedArrayOf Int32Array; ///< Typed-array of signed 32-bit integers - typedef TypedArrayOf Uint32Array; ///< Typed-array of unsigned 32-bit integers - typedef TypedArrayOf Float32Array; ///< Typed-array of 32-bit floating-point values - typedef TypedArrayOf Float64Array; ///< Typed-array of 64-bit floating-point values + using Int8Array = + TypedArrayOf; ///< Typed-array of signed 8-bit integers + using Uint8Array = + TypedArrayOf; ///< Typed-array of unsigned 8-bit integers + using Int16Array = + TypedArrayOf; ///< Typed-array of signed 16-bit integers + using Uint16Array = + TypedArrayOf; ///< Typed-array of unsigned 16-bit integers + using Int32Array = + TypedArrayOf; ///< Typed-array of signed 32-bit integers + using Uint32Array = + TypedArrayOf; ///< Typed-array of unsigned 32-bit integers + using Float32Array = + TypedArrayOf; ///< Typed-array of 32-bit floating-point values + using Float64Array = + TypedArrayOf; ///< Typed-array of 64-bit floating-point values #if NAPI_VERSION > 5 - typedef TypedArrayOf BigInt64Array; ///< Typed array of signed 64-bit integers - typedef TypedArrayOf BigUint64Array; ///< Typed array of unsigned 64-bit integers + using BigInt64Array = + TypedArrayOf; ///< Typed array of signed 64-bit integers + using BigUint64Array = + TypedArrayOf; ///< Typed array of unsigned 64-bit integers #endif // NAPI_VERSION > 5 /// Defines the signature of a N-API C++ module's registration callback (init) function. - typedef Object (*ModuleRegisterCallback)(Env env, Object exports); + using ModuleRegisterCallback = Object (*)(Env env, Object exports); class MemoryManagement; @@ -1008,70 +1018,71 @@ namespace Napi { class Function : public Object { public: - typedef void (*VoidCallback)(const CallbackInfo& info); - typedef Value (*Callback)(const CallbackInfo& info); - - template - static Function New(napi_env env, - const char* utf8name = nullptr, - void* data = nullptr); - - template - static Function New(napi_env env, - const char* utf8name = nullptr, - void* data = nullptr); - - template - static Function New(napi_env env, - const std::string& utf8name, - void* data = nullptr); - - template - static Function New(napi_env env, - const std::string& utf8name, - void* data = nullptr); - - /// Callable must implement operator() accepting a const CallbackInfo& - /// and return either void or Value. - template - static Function New(napi_env env, - Callable cb, - const char* utf8name = nullptr, - void* data = nullptr); - /// Callable must implement operator() accepting a const CallbackInfo& - /// and return either void or Value. - template - static Function New(napi_env env, - Callable cb, - const std::string& utf8name, - void* data = nullptr); - - Function(); - Function(napi_env env, napi_value value); - - Value operator ()(const std::initializer_list& args) const; - - Value Call(const std::initializer_list& args) const; - Value Call(const std::vector& args) const; - Value Call(size_t argc, const napi_value* args) const; - Value Call(napi_value recv, const std::initializer_list& args) const; - Value Call(napi_value recv, const std::vector& args) const; - Value Call(napi_value recv, size_t argc, const napi_value* args) const; - - Value MakeCallback(napi_value recv, - const std::initializer_list& args, - napi_async_context context = nullptr) const; - Value MakeCallback(napi_value recv, - const std::vector& args, - napi_async_context context = nullptr) const; - Value MakeCallback(napi_value recv, - size_t argc, - const napi_value* args, - napi_async_context context = nullptr) const; - - Object New(const std::initializer_list& args) const; - Object New(const std::vector& args) const; - Object New(size_t argc, const napi_value* args) const; + using VoidCallback = void (*)(const CallbackInfo& info); + using Callback = Value (*)(const CallbackInfo& info); + + template + static Function New(napi_env env, + const char* utf8name = nullptr, + void* data = nullptr); + + template + static Function New(napi_env env, + const char* utf8name = nullptr, + void* data = nullptr); + + template + static Function New(napi_env env, + const std::string& utf8name, + void* data = nullptr); + + template + static Function New(napi_env env, + const std::string& utf8name, + void* data = nullptr); + + /// Callable must implement operator() accepting a const CallbackInfo& + /// and return either void or Value. + template + static Function New(napi_env env, + Callable cb, + const char* utf8name = nullptr, + void* data = nullptr); + /// Callable must implement operator() accepting a const CallbackInfo& + /// and return either void or Value. + template + static Function New(napi_env env, + Callable cb, + const std::string& utf8name, + void* data = nullptr); + + Function(); + Function(napi_env env, napi_value value); + + Value operator()(const std::initializer_list& args) const; + + Value Call(const std::initializer_list& args) const; + Value Call(const std::vector& args) const; + Value Call(size_t argc, const napi_value* args) const; + Value Call(napi_value recv, + const std::initializer_list& args) const; + Value Call(napi_value recv, const std::vector& args) const; + Value Call(napi_value recv, size_t argc, const napi_value* args) const; + + Value MakeCallback(napi_value recv, + const std::initializer_list& args, + napi_async_context context = nullptr) const; + Value MakeCallback(napi_value recv, + const std::vector& args, + napi_async_context context = nullptr) const; + Value MakeCallback(napi_value recv, + size_t argc, + const napi_value* args, + napi_async_context context = nullptr) const; + + Object New(const std::initializer_list& args) const; + Object New(const std::vector& args) const; + Object New(size_t argc, const napi_value* args) const; }; class Promise : public Object { @@ -1381,14 +1392,17 @@ namespace Napi { protected: /// !cond INTERNAL - typedef napi_status (*create_error_fn)(napi_env envb, napi_value code, napi_value msg, napi_value* result); - - template - static TError New(napi_env env, - const char* message, - size_t length, - create_error_fn create_error); - /// !endcond + using create_error_fn = napi_status (*)(napi_env envb, + napi_value code, + napi_value msg, + napi_value* result); + + template + static TError New(napi_env env, + const char* message, + size_t length, + create_error_fn create_error); + /// !endcond private: mutable std::string _message; @@ -1443,8 +1457,8 @@ namespace Napi { class PropertyDescriptor { public: - typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info); - typedef void (*SetterCallback)(const Napi::CallbackInfo& info); + using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info); + using SetterCallback = void (*)(const Napi::CallbackInfo& info); #ifndef NODE_ADDON_API_DISABLE_DEPRECATED template @@ -1664,13 +1678,13 @@ namespace Napi { template class InstanceWrap { public: + using InstanceVoidMethodCallback = void (T::*)(const CallbackInfo& info); + using InstanceMethodCallback = Napi::Value (T::*)(const CallbackInfo& info); + using InstanceGetterCallback = Napi::Value (T::*)(const CallbackInfo& info); + using InstanceSetterCallback = void (T::*)(const CallbackInfo& info, + const Napi::Value& value); - typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info); - typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info); - typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info); - typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value); - - typedef ClassPropertyDescriptor PropertyDescriptor; + using PropertyDescriptor = ClassPropertyDescriptor; static PropertyDescriptor InstanceMethod(const char* utf8name, InstanceVoidMethodCallback method, @@ -1735,11 +1749,12 @@ namespace Napi { private: using This = InstanceWrap; - typedef MethodCallbackData InstanceVoidMethodCallbackData; - typedef MethodCallbackData InstanceMethodCallbackData; - typedef AccessorCallbackData InstanceAccessorCallbackData; + using InstanceVoidMethodCallbackData = + MethodCallbackData; + using InstanceMethodCallbackData = + MethodCallbackData; + using InstanceAccessorCallbackData = + AccessorCallbackData; static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info); static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info); @@ -1795,12 +1810,13 @@ namespace Napi { static T* Unwrap(Object wrapper); // Methods exposed to JavaScript must conform to one of these callback signatures. - typedef void (*StaticVoidMethodCallback)(const CallbackInfo& info); - typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info); - typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info); - typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value); + using StaticVoidMethodCallback = void (*)(const CallbackInfo& info); + using StaticMethodCallback = Napi::Value (*)(const CallbackInfo& info); + using StaticGetterCallback = Napi::Value (*)(const CallbackInfo& info); + using StaticSetterCallback = void (*)(const CallbackInfo& info, + const Napi::Value& value); - typedef ClassPropertyDescriptor PropertyDescriptor; + using PropertyDescriptor = ClassPropertyDescriptor; static Function DefineClass(Napi::Env env, const char* utf8name, @@ -1883,12 +1899,13 @@ namespace Napi { const napi_property_descriptor* props, void* data = nullptr); - typedef MethodCallbackData StaticVoidMethodCallbackData; - typedef MethodCallbackData StaticMethodCallbackData; + using StaticVoidMethodCallbackData = + MethodCallbackData; + using StaticMethodCallbackData = + MethodCallbackData; - typedef AccessorCallbackData StaticAccessorCallbackData; + using StaticAccessorCallbackData = + AccessorCallbackData; template static napi_value WrappedMethod(napi_env env, @@ -2631,7 +2648,7 @@ namespace Napi { static T* Unwrap(Object wrapper); protected: - typedef ClassPropertyDescriptor AddonProp; + using AddonProp = ClassPropertyDescriptor; void DefineAddon(Object exports, const std::initializer_list& props); Napi::Object DefineProperties(Object object,