• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2025 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 PANDA_RUNTIME_ETS_FFI_CLASSES_ETS_BOX_CLASSES_H_
17 #define PANDA_RUNTIME_ETS_FFI_CLASSES_ETS_BOX_CLASSES_H_
18 
19 #include "macros.h"
20 #include "plugins/ets/runtime/types/ets_object.h"
21 
22 namespace ark::ets {
23 
24 template <typename T>
25 class EtsBoxPrimitive : public EtsObject {
26 public:
27     EtsBoxPrimitive() = delete;
28     ~EtsBoxPrimitive() = delete;
29 
30     static EtsBoxPrimitive *Create(EtsCoroutine *coro, T value);
31 
32     static Class *GetBoxClass(EtsCoroutine *coro);
33 
34     static EtsClass *GetEtsBoxClass(EtsCoroutine *coro);
35 
Unbox(EtsObject * obj)36     static T Unbox(EtsObject *obj)
37     {
38         auto val = EtsBoxPrimitive<T>::FromCoreType(obj);
39         ASSERT(val != nullptr);
40         return val->GetValue();
41     }
42 
FromCoreType(EtsObject * obj)43     static constexpr EtsBoxPrimitive *FromCoreType(EtsObject *obj)
44     {
45         ASSERT(obj->GetClass()->IsBoxed());
46         return static_cast<EtsBoxPrimitive<T> *>(obj);
47     }
48 
GetValue()49     T GetValue() const
50     {
51         return value_;
52     }
53 
SetValue(T value)54     void SetValue(T value)
55     {
56         value_ = value;
57     }
58 
59     NO_COPY_SEMANTIC(EtsBoxPrimitive);
60     NO_MOVE_SEMANTIC(EtsBoxPrimitive);
61 
GetValueOffset()62     static constexpr size_t GetValueOffset()
63     {
64         return MEMBER_OFFSET(EtsBoxPrimitive, value_);
65     }
66 
67 private:
68     T value_;
69 };
70 }  // namespace ark::ets
71 
72 #endif  // PANDA_RUNTIME_ETS_FFI_CLASSES_ETS_BOX_CLASSES_H_
73