Skip to content

Commit 79deefb

Browse files
legendecasGabriel Schulhof
authored andcommitted
src: explicitly disallow assign and copy
PR-URL: #590 Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
1 parent af50ac2 commit 79deefb

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

napi.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
9191

9292
#endif // NAPI_CPP_EXCEPTIONS
9393

94+
# define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
95+
# define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
96+
97+
#define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \
98+
NAPI_DISALLOW_ASSIGN(CLASS) \
99+
NAPI_DISALLOW_COPY(CLASS)
100+
94101
#define NAPI_FATAL_IF_FAILED(status, location, message) \
95102
do { \
96103
if ((status) != napi_ok) { \
@@ -1129,7 +1136,7 @@ namespace Napi {
11291136
// A reference can be moved but cannot be copied.
11301137
Reference(Reference<T>&& other);
11311138
Reference<T>& operator =(Reference<T>&& other);
1132-
Reference<T>& operator =(const Reference<T>&) = delete;
1139+
NAPI_DISALLOW_ASSIGN(Reference<T>)
11331140

11341141
operator napi_ref() const;
11351142
bool operator ==(const Reference<T> &other) const;
@@ -1174,7 +1181,7 @@ namespace Napi {
11741181
ObjectReference& operator =(Reference<Object>&& other);
11751182
ObjectReference(ObjectReference&& other);
11761183
ObjectReference& operator =(ObjectReference&& other);
1177-
ObjectReference& operator =(const ObjectReference&) = delete;
1184+
NAPI_DISALLOW_ASSIGN(ObjectReference)
11781185

11791186
Napi::Value Get(const char* utf8name) const;
11801187
Napi::Value Get(const std::string& utf8name) const;
@@ -1211,8 +1218,7 @@ namespace Napi {
12111218
FunctionReference& operator =(Reference<Function>&& other);
12121219
FunctionReference(FunctionReference&& other);
12131220
FunctionReference& operator =(FunctionReference&& other);
1214-
FunctionReference(const FunctionReference&) = delete;
1215-
FunctionReference& operator =(const FunctionReference&) = delete;
1221+
NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
12161222

12171223
Napi::Value operator ()(const std::initializer_list<napi_value>& args) const;
12181224

@@ -1402,8 +1408,7 @@ namespace Napi {
14021408
~CallbackInfo();
14031409

14041410
// Disallow copying to prevent multiple free of _dynamicArgs
1405-
CallbackInfo(CallbackInfo const &) = delete;
1406-
void operator=(CallbackInfo const &) = delete;
1411+
NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
14071412

14081413
Napi::Env Env() const;
14091414
Value NewTarget() const;
@@ -1891,8 +1896,7 @@ namespace Napi {
18911896
~HandleScope();
18921897

18931898
// Disallow copying to prevent double close of napi_handle_scope
1894-
HandleScope(HandleScope const &) = delete;
1895-
void operator=(HandleScope const &) = delete;
1899+
NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
18961900

18971901
operator napi_handle_scope() const;
18981902

@@ -1910,8 +1914,7 @@ namespace Napi {
19101914
~EscapableHandleScope();
19111915

19121916
// Disallow copying to prevent double close of napi_escapable_handle_scope
1913-
EscapableHandleScope(EscapableHandleScope const &) = delete;
1914-
void operator=(EscapableHandleScope const &) = delete;
1917+
NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
19151918

19161919
operator napi_escapable_handle_scope() const;
19171920

@@ -1931,8 +1934,7 @@ namespace Napi {
19311934
virtual ~CallbackScope();
19321935

19331936
// Disallow copying to prevent double close of napi_callback_scope
1934-
CallbackScope(CallbackScope const &) = delete;
1935-
void operator=(CallbackScope const &) = delete;
1937+
NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
19361938

19371939
operator napi_callback_scope() const;
19381940

@@ -1952,8 +1954,7 @@ namespace Napi {
19521954

19531955
AsyncContext(AsyncContext&& other);
19541956
AsyncContext& operator =(AsyncContext&& other);
1955-
AsyncContext(const AsyncContext&) = delete;
1956-
AsyncContext& operator =(const AsyncContext&) = delete;
1957+
NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
19571958

19581959
operator napi_async_context() const;
19591960

@@ -1971,8 +1972,7 @@ namespace Napi {
19711972
// An async worker can be moved but cannot be copied.
19721973
AsyncWorker(AsyncWorker&& other);
19731974
AsyncWorker& operator =(AsyncWorker&& other);
1974-
AsyncWorker(const AsyncWorker&) = delete;
1975-
AsyncWorker& operator =(const AsyncWorker&) = delete;
1975+
NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
19761976

19771977
operator napi_async_work() const;
19781978

0 commit comments

Comments
 (0)