1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef JS_CONCURRENT_MODULE_COMMON_HELPER_OBJECT_HELPER_H
17 #define JS_CONCURRENT_MODULE_COMMON_HELPER_OBJECT_HELPER_H
18 
19 namespace Commonlibrary::Concurrent::Common::Helper {
20 class DereferenceHelp {
21 public:
22     template<typename Inner, typename Outer>
DereferenceOf(const Inner Outer::* field,const Inner * pointer)23     static Outer* DereferenceOf(const Inner Outer::*field, const Inner* pointer)
24     {
25         if (field != nullptr && pointer != nullptr) {
26             auto fieldOffset = reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field));
27             auto outPointer = reinterpret_cast<Outer*>(reinterpret_cast<uintptr_t>(pointer) - fieldOffset);
28             return outPointer;
29         }
30         return nullptr;
31     }
32 };
33 
34 class CloseHelp {
35 public:
36     template<typename T>
DeletePointer(const T * value,bool isArray)37     static void DeletePointer(const T* value, bool isArray)
38     {
39         if (value == nullptr) {
40             return;
41         }
42         if (isArray) {
43             delete[] value;
44             value = nullptr;
45         } else {
46             delete value;
47             value = nullptr;
48         }
49     }
50 };
51 
52 template<typename T>
53 class ObjectScope {
54 public:
ObjectScope(const T * data,bool isArray)55     ObjectScope(const T* data, bool isArray) : data_(data), isArray_(isArray) {}
~ObjectScope()56     ~ObjectScope()
57     {
58         if (data_ == nullptr) {
59             return;
60         }
61         if (isArray_) {
62             delete[] data_;
63         } else {
64             delete data_;
65             data_ = nullptr;
66         }
67     }
68 
69 private:
70     const T* data_;
71     bool isArray_;
72 };
73 
74 class HandleScope {
75 public:
HandleScope(napi_env env,napi_status & status)76     HandleScope(napi_env env, napi_status& status) : env_(env)
77     {
78         status = napi_open_handle_scope(env, &scope_);
79     }
~HandleScope()80     ~HandleScope()
81     {
82         if (env_ != nullptr && scope_ != nullptr) {
83             napi_close_handle_scope(env_, scope_);
84         }
85     }
86 
87 private:
88     napi_handle_scope scope_ = nullptr;
89     napi_env env_ = nullptr;
90 };
91 } // namespace Commonlibrary::Concurrent::Common::Helper
92 #endif // JS_CONCURRENT_MODULE_COMMON_HELPER_OBJECT_HELPER_H
93