• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_INCLUDE_ITABLE_H_
17 #define PANDA_RUNTIME_INCLUDE_ITABLE_H_
18 
19 #include "libpandabase/utils/span.h"
20 #include "runtime/include/mem/allocator.h"
21 #include "runtime/include/method.h"
22 
23 namespace panda {
24 
25 class Class;
26 
27 class ITable {
28 public:
29     class Entry {
30     public:
SetInterface(Class * interface)31         void SetInterface(Class *interface)
32         {
33             interface_ = interface;
34         }
35 
GetInterface()36         Class *GetInterface() const
37         {
38             return interface_;
39         }
40 
SetMethods(Span<Method * > methods)41         void SetMethods(Span<Method *> methods)
42         {
43             methods_ = methods;
44         }
45 
GetMethods()46         Span<Method *> GetMethods() const
47         {
48             return methods_;
49         }
50 
Copy(mem::InternalAllocatorPtr allocator)51         Entry Copy(mem::InternalAllocatorPtr allocator) const
52         {
53             Entry entry;
54             entry.interface_ = interface_;
55             if (methods_.data() != nullptr) {
56                 entry.methods_ = {allocator->AllocArray<Method *>(methods_.size()), methods_.size()};
57                 for (size_t idx = 0; idx < methods_.size(); idx++) {
58                     entry.methods_[idx] = methods_[idx];
59                 }
60             }
61             return entry;
62         }
63 
64     private:
65         Class *interface_ {nullptr};
66         Span<Method *> methods_ {nullptr, nullptr};
67     };
68 
69     ITable() = default;
70 
ITable(Span<Entry> elements)71     explicit ITable(Span<Entry> elements) : elements_(elements) {}
72 
Get()73     Span<Entry> Get()
74     {
75         return elements_;
76     }
77 
Get()78     Span<const Entry> Get() const
79     {
80         return Span<const Entry>(elements_.data(), elements_.size());
81     }
82 
Size()83     size_t Size() const
84     {
85         return elements_.size();
86     }
87 
88     Entry &operator[](size_t i)
89     {
90         return elements_[i];
91     }
92 
93     const Entry &operator[](size_t i) const
94     {
95         return elements_[i];
96     }
97 
98     ~ITable() = default;
99 
100     DEFAULT_COPY_SEMANTIC(ITable);
101     DEFAULT_MOVE_SEMANTIC(ITable);
102 
103 private:
104     Span<Entry> elements_ {nullptr, nullptr};
105 };
106 
107 }  // namespace panda
108 
109 #endif  // PANDA_RUNTIME_INCLUDE_ITABLE_H_
110