src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / util-inl.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_INL_H_
23 #define SRC_UTIL_INL_H_
24
25 #include "util.h"
26
27 #include <assert.h>
28
29 namespace node {
30
31 template <class TypeName>
32 inline v8::Local<TypeName> PersistentToLocal(
33     v8::Isolate* isolate,
34     const v8::Persistent<TypeName>& persistent) {
35   if (persistent.IsWeak()) {
36     return WeakPersistentToLocal(isolate, persistent);
37   } else {
38     return StrongPersistentToLocal(persistent);
39   }
40 }
41
42 template <class TypeName>
43 inline v8::Local<TypeName> StrongPersistentToLocal(
44     const v8::Persistent<TypeName>& persistent) {
45   return *reinterpret_cast<v8::Local<TypeName>*>(
46       const_cast<v8::Persistent<TypeName>*>(&persistent));
47 }
48
49 template <class TypeName>
50 inline v8::Local<TypeName> WeakPersistentToLocal(
51     v8::Isolate* isolate,
52     const v8::Persistent<TypeName>& persistent) {
53   return v8::Local<TypeName>::New(isolate, persistent);
54 }
55
56 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
57                                            const char* data,
58                                            int length) {
59   return v8::String::NewFromOneByte(isolate,
60                                     reinterpret_cast<const uint8_t*>(data),
61                                     v8::String::kNormalString,
62                                     length);
63 }
64
65 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
66                                            const signed char* data,
67                                            int length) {
68   return v8::String::NewFromOneByte(isolate,
69                                     reinterpret_cast<const uint8_t*>(data),
70                                     v8::String::kNormalString,
71                                     length);
72 }
73
74 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
75                                            const unsigned char* data,
76                                            int length) {
77   return v8::String::NewFromOneByte(isolate,
78                                     reinterpret_cast<const uint8_t*>(data),
79                                     v8::String::kNormalString,
80                                     length);
81 }
82
83 template <typename TypeName>
84 void Wrap(v8::Local<v8::Object> object, TypeName* pointer) {
85   assert(!object.IsEmpty());
86   assert(object->InternalFieldCount() > 0);
87   object->SetAlignedPointerInInternalField(0, pointer);
88 }
89
90 template <typename TypeName>
91 TypeName* Unwrap(v8::Local<v8::Object> object) {
92   assert(!object.IsEmpty());
93   assert(object->InternalFieldCount() > 0);
94   void* pointer = object->GetAlignedPointerFromInternalField(0);
95   return static_cast<TypeName*>(pointer);
96 }
97
98 }  // namespace node
99
100 #endif  // SRC_UTIL_INL_H_