src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / util.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #ifndef SRC_UTIL_H_
23 #define SRC_UTIL_H_
24
25 #include "v8.h"
26 #include <assert.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29
30 namespace node {
31
32 #define OFFSET_OF(TypeName, Field)                                            \
33   (reinterpret_cast<uintptr_t>(&(reinterpret_cast<TypeName*>(8)->Field)) - 8)
34
35 #define CONTAINER_OF(Pointer, TypeName, Field)                                \
36   reinterpret_cast<TypeName*>(                                                \
37       reinterpret_cast<uintptr_t>(Pointer) - OFFSET_OF(TypeName, Field))
38
39 #define FIXED_ONE_BYTE_STRING(isolate, string)                                \
40   (node::OneByteString((isolate), (string), sizeof(string) - 1))
41
42 #define DISALLOW_COPY_AND_ASSIGN(TypeName)                                    \
43   void operator=(const TypeName&);                                            \
44   TypeName(const TypeName&)
45
46 #if defined(NDEBUG)
47 # define ASSERT(expression)
48 # define CHECK(expression)                                                    \
49   do {                                                                        \
50     if (!(expression)) abort();                                               \
51   } while (0)
52 #else
53 # define ASSERT(expression)  assert(expression)
54 # define CHECK(expression)   assert(expression)
55 #endif
56
57 #define CHECK_EQ(a, b) CHECK((a) == (b))
58 #define CHECK_NE(a, b) CHECK((a) != (b))
59
60 #define UNREACHABLE() abort()
61
62 // If persistent.IsWeak() == false, then do not call persistent.Reset()
63 // while the returned Local<T> is still in scope, it will destroy the
64 // reference to the object.
65 template <class TypeName>
66 inline v8::Local<TypeName> PersistentToLocal(
67     v8::Isolate* isolate,
68     const v8::Persistent<TypeName>& persistent);
69
70 // Unchecked conversion from a non-weak Persistent<T> to Local<TLocal<T>,
71 // use with care!
72 //
73 // Do not call persistent.Reset() while the returned Local<T> is still in
74 // scope, it will destroy the reference to the object.
75 template <class TypeName>
76 inline v8::Local<TypeName> StrongPersistentToLocal(
77     const v8::Persistent<TypeName>& persistent);
78
79 template <class TypeName>
80 inline v8::Local<TypeName> WeakPersistentToLocal(
81     v8::Isolate* isolate,
82     const v8::Persistent<TypeName>& persistent);
83
84 // Convenience wrapper around v8::String::NewFromOneByte().
85 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
86                                            const char* data,
87                                            int length = -1);
88
89 // For the people that compile with -funsigned-char.
90 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
91                                            const signed char* data,
92                                            int length = -1);
93
94 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
95                                            const unsigned char* data,
96                                            int length = -1);
97
98 inline void Wrap(v8::Local<v8::Object> object, void* pointer);
99
100 template <typename TypeName>
101 inline TypeName* Unwrap(v8::Local<v8::Object> object);
102
103 }  // namespace node
104
105 #endif  // SRC_UTIL_H_