1 /** 2 * Copyright (c) 2021-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 #ifndef RUNTIME_MEM_OBJECT_VMHANDLE_H 16 #define RUNTIME_MEM_OBJECT_VMHANDLE_H 17 18 #include "libpandabase/macros.h" 19 #include "runtime/handle_base.h" 20 #include "runtime/include/managed_thread.h" 21 #include "runtime/handle_scope.h" 22 23 namespace panda { 24 25 using TaggedType = coretypes::TaggedType; 26 using TaggedValue = coretypes::TaggedValue; 27 28 // VMHandle should be used in language-agnostic part of runtime 29 template <typename T> 30 class VMHandle : public HandleBase { 31 public: VMHandle()32 inline explicit VMHandle() : HandleBase(reinterpret_cast<uintptr_t>(nullptr)) {} 33 VMHandle(ManagedThread * thread,ObjectHeader * object)34 explicit VMHandle(ManagedThread *thread, ObjectHeader *object) 35 { 36 if (object != nullptr) { 37 address_ = thread->GetTopScope<ObjectHeader *>()->NewHandle(object); 38 } else { 39 address_ = reinterpret_cast<uintptr_t>(nullptr); 40 } 41 } 42 43 ~VMHandle() = default; 44 45 NO_COPY_SEMANTIC(VMHandle); 46 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(VMHandle); 47 GetPtr()48 T *GetPtr() const 49 { 50 if (address_ == reinterpret_cast<uintptr_t>(nullptr)) { 51 return nullptr; 52 } 53 return *(reinterpret_cast<T **>(GetAddress())); 54 } 55 56 explicit operator T *() 57 { 58 return GetPtr(); 59 } 60 61 T *operator->() 62 { 63 return GetPtr(); 64 } 65 }; 66 67 } // namespace panda 68 69 #endif // RUNTIME_MEM_OBJECT_VMHANDLE_H 70