• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_VALUE_H_
16 #define PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_VALUE_H_
17 
18 #include "runtime/include/runtime.h"
19 #include "ets_type.h"
20 #include "plugins/ets/runtime/ani/ani.h"
21 
22 namespace ark::ets {
23 
24 class EtsObject;
25 
26 template <typename T>
GetEtsType()27 constexpr EtsType GetEtsType()
28 {
29     if constexpr (std::is_same<T, ets_boolean>::value) {  // NOLINT
30         return EtsType::BOOLEAN;
31     }
32     if constexpr (std::is_same<T, ets_byte>::value) {  // NOLINT
33         return EtsType::BYTE;
34     }
35     if constexpr (std::is_same<T, ets_char>::value) {  // NOLINT
36         return EtsType::CHAR;
37     }
38     if constexpr (std::is_same<T, ets_short>::value) {  // NOLINT
39         return EtsType::SHORT;
40     }
41     if constexpr (std::is_same<T, ets_int>::value) {  // NOLINT
42         return EtsType::INT;
43     }
44     if constexpr (std::is_same<T, ets_long>::value) {  // NOLINT
45         return EtsType::LONG;
46     }
47     if constexpr (std::is_same<T, ets_float>::value) {  // NOLINT
48         return EtsType::FLOAT;
49     }
50     if constexpr (std::is_same<T, ets_double>::value) {  // NOLINT
51         return EtsType::DOUBLE;
52     }
53     if constexpr (std::is_same<T, EtsObject>::value) {  // NOLINT
54         return EtsType::OBJECT;
55     }
56     if constexpr (std::is_same<T, void>::value) {  // NOLINT
57         return EtsType::VOID;
58     }
59     return EtsType::UNKNOWN;
60 }
61 
62 class EtsValue {
63 public:
64     EtsValue() = default;
65 
66     template <typename T>
EtsValue(T value)67     explicit EtsValue(T value)
68     {
69         static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, ets_object> || std::is_same_v<T, ani_ref> ||
70                       std::is_same_v<T, std::nullptr_t>);
71         static_assert(sizeof(T) <= sizeof(holder_));
72 
73         memcpy_s(&holder_, sizeof(holder_), &value, sizeof(value));
74     }
75 
76     template <typename T>
GetAs()77     T GetAs()
78     {
79         static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, ets_object> || std::is_same_v<T, ani_ref>);
80         static_assert(sizeof(T) <= sizeof(holder_));
81 
82         T tmp;
83         memcpy_s(&tmp, sizeof(T), &holder_, sizeof(T));
84         return tmp;
85     }
86 
87 private:
88     int64_t holder_ {0};
89 };
90 
91 }  // namespace ark::ets
92 
93 #endif  // PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_VALUE_H_
94