• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(T * & value,bool isArray)37     static void DeletePointer(T*& value, bool isArray)
38     {
39         DeletePointerInner(value, isArray);
40         value = nullptr;
41     }
42 
43     template<typename T>
DeletePointer(const T * value,bool isArray)44     static void DeletePointer(const T* value, bool isArray)
45     {
46         DeletePointerInner(value, isArray);
47     }
48 
49     template<typename T>
DeletePointerInner(const T * value,bool isArray)50     static void DeletePointerInner(const T* value, bool isArray)
51     {
52         if (value == nullptr) {
53             return;
54         }
55         if (isArray) {
56             delete[] value;
57             value = nullptr;
58         } else {
59             delete value;
60             value = nullptr;
61         }
62     }
63 };
64 
65 template<typename T>
66 class ObjectScope {
67 public:
ObjectScope(const T * data,bool isArray)68     ObjectScope(const T* data, bool isArray) : data_(data), isArray_(isArray) {}
~ObjectScope()69     ~ObjectScope()
70     {
71         if (data_ == nullptr) {
72             return;
73         }
74         if (isArray_) {
75             delete[] data_;
76             data_ = nullptr;
77         } else {
78             delete data_;
79             data_ = nullptr;
80         }
81     }
82 
83 private:
84     const T* data_;
85     bool isArray_;
86 };
87 
88 class HandleScope {
89 public:
HandleScope(napi_env env,napi_status & status)90     HandleScope(napi_env env, napi_status& status) : env_(env)
91     {
92         status = napi_open_handle_scope(env, &scope_);
93     }
~HandleScope()94     ~HandleScope()
95     {
96         if (env_ != nullptr && scope_ != nullptr) {
97             napi_close_handle_scope(env_, scope_);
98         }
99     }
100 
101 private:
102     napi_handle_scope scope_ = nullptr;
103     napi_env env_ = nullptr;
104 };
105 } // namespace Commonlibrary::Concurrent::Common::Helper
106 #endif // JS_CONCURRENT_MODULE_COMMON_HELPER_OBJECT_HELPER_H
107