• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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_TEST_MIRROR_CLASSES_H
17 #define PANDA_RUNTIME_ETS_TEST_MIRROR_CLASSES_H
18 
19 #include <cstddef>
20 #include <gtest/gtest.h>
21 #include <string_view>
22 #include <libpandabase/macros.h>
23 #include "types/ets_class.h"
24 
25 namespace ark::ets::test {
26 
27 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
28 #define MIRROR_FIELD_INFO(KLASS, FIELD, ETS_FIELD) MirrorFieldInfo(ETS_FIELD, MEMBER_OFFSET(KLASS, FIELD))
29 
30 class MirrorFieldInfo {
31 public:
MirrorFieldInfo(const char * name,std::size_t offset)32     constexpr MirrorFieldInfo(const char *name, std::size_t offset) : name_(name), offset_(offset) {}
33     ~MirrorFieldInfo() = default;
34     DEFAULT_COPY_SEMANTIC(MirrorFieldInfo);
35     DEFAULT_MOVE_SEMANTIC(MirrorFieldInfo);
36 
Name()37     constexpr const char *Name() const
38     {
39         return name_.data();
40     }
41 
Offset()42     constexpr std::size_t Offset() const
43     {
44         return offset_;
45     }
46 
CompareMemberOffsets(EtsClass * klass,const std::vector<MirrorFieldInfo> & members)47     static void CompareMemberOffsets(EtsClass *klass, const std::vector<MirrorFieldInfo> &members)
48     {
49         ASSERT_NE(nullptr, klass);
50         ASSERT_EQ(members.size(), klass->GetInstanceFieldsNumber());
51 
52         for (const MirrorFieldInfo &memb : members) {
53             EtsField *field = klass->GetFieldIDByName(memb.Name());
54             ASSERT_NE(nullptr, field);
55             ASSERT_EQ(memb.Offset(), field->GetOffset())
56                 << "Offsets of the field '" << memb.Name() << "' are different";
57         }
58     }
59 
60 private:
61     std::string_view name_;
62     std::size_t offset_ = 0;
63 };
64 
65 }  // namespace ark::ets::test
66 
67 #endif  // PANDA_RUNTIME_ETS_TEST_MIRROR_CLASSES_H
68