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 CPP_ABCKIT_CORE_CLASS_IMPL_H
17 #define CPP_ABCKIT_CORE_CLASS_IMPL_H
18
19 #include "class.h"
20 #include "annotation.h"
21 #include "function.h"
22 #include "module.h"
23
24 namespace abckit::core {
25
GetFile()26 inline const File *Class::GetFile() const
27 {
28 return GetResource();
29 }
30
GetName()31 inline std::string Class::GetName() const
32 {
33 const ApiConfig *conf = GetApiConfig();
34 AbckitString *cString = conf->cIapi_->classGetName(GetView());
35 CheckError(conf);
36 std::string str = conf->cIapi_->abckitStringToString(cString);
37 CheckError(conf);
38 return str;
39 }
40
41 // CC-OFFNXT(G.FUD.06) perf critical
GetAllMethods()42 inline std::vector<core::Function> Class::GetAllMethods() const
43 {
44 std::vector<core::Function> methods;
45 Payload<std::vector<core::Function> *> payload {&methods, GetApiConfig(), GetResource()};
46
47 GetApiConfig()->cIapi_->classEnumerateMethods(GetView(), &payload, [](AbckitCoreFunction *method, void *data) {
48 const auto &payload = *static_cast<Payload<std::vector<core::Function> *> *>(data);
49 payload.data->push_back(core::Function(method, payload.config, payload.resource));
50 return true;
51 });
52
53 CheckError(GetApiConfig());
54
55 return methods;
56 }
57
58 // CC-OFFNXT(G.FUD.06) perf critical
GetAnnotations()59 inline std::vector<core::Annotation> Class::GetAnnotations() const
60 {
61 std::vector<core::Annotation> anns;
62
63 Payload<std::vector<core::Annotation> *> payload {&anns, GetApiConfig(), GetResource()};
64
65 GetApiConfig()->cIapi_->classEnumerateAnnotations(
66 GetView(), &payload, [](AbckitCoreAnnotation *method, void *data) {
67 const auto &payload = *static_cast<Payload<std::vector<core::Annotation> *> *>(data);
68 payload.data->push_back(core::Annotation(method, payload.config, payload.resource));
69 return true;
70 });
71
72 CheckError(GetApiConfig());
73
74 return anns;
75 }
76
77 // CC-OFFNXT(G.FUD.06) perf critical
EnumerateMethods(const std::function<bool (core::Function)> & cb)78 inline bool Class::EnumerateMethods(const std::function<bool(core::Function)> &cb) const
79 {
80 Payload<const std::function<bool(core::Function)> &> payload {cb, GetApiConfig(), GetResource()};
81
82 bool isNormalExit = GetApiConfig()->cIapi_->classEnumerateMethods(
83 GetView(), &payload, [](AbckitCoreFunction *method, void *data) -> bool {
84 const auto &payload = *static_cast<Payload<const std::function<bool(core::Function)> &> *>(data);
85 return payload.data(core::Function(method, payload.config, payload.resource));
86 });
87 CheckError(GetApiConfig());
88 return isNormalExit;
89 }
90
GetAllMethodsInner(std::vector<core::Function> & methods)91 inline bool Class::GetAllMethodsInner(std::vector<core::Function> &methods) const
92 {
93 Payload<std::vector<core::Function> *> payload {&methods, GetApiConfig(), GetResource()};
94
95 auto isNormalExit =
96 GetApiConfig()->cIapi_->classEnumerateMethods(GetView(), &payload, [](AbckitCoreFunction *method, void *data) {
97 const auto &payload = *static_cast<Payload<std::vector<core::Function> *> *>(data);
98 payload.data->push_back(core::Function(method, payload.config, payload.resource));
99 return true;
100 });
101 return isNormalExit;
102 }
103
GetAllAnnotationsInner(std::vector<core::Annotation> & anns)104 inline bool Class::GetAllAnnotationsInner(std::vector<core::Annotation> &anns) const
105 {
106 Payload<std::vector<core::Annotation> *> payload {&anns, GetApiConfig(), GetResource()};
107
108 auto isNormalExit = GetApiConfig()->cIapi_->classEnumerateAnnotations(
109 GetView(), &payload, [](AbckitCoreAnnotation *method, void *data) {
110 const auto &payload = *static_cast<Payload<std::vector<core::Annotation> *> *>(data);
111 payload.data->push_back(core::Annotation(method, payload.config, payload.resource));
112 return true;
113 });
114 return isNormalExit;
115 }
116
Class(AbckitCoreClass * klass,const ApiConfig * conf,const File * file)117 inline Class::Class(AbckitCoreClass *klass, const ApiConfig *conf, const File *file)
118 : ViewInResource(klass), conf_(conf)
119 {
120 SetResource(file);
121 };
122
EnumerateAnnotations(const std::function<bool (core::Annotation)> & cb)123 inline bool Class::EnumerateAnnotations(const std::function<bool(core::Annotation)> &cb) const
124 {
125 Payload<const std::function<bool(core::Annotation)> &> payload {cb, GetApiConfig(), GetResource()};
126
127 bool isNormalExit = GetApiConfig()->cIapi_->classEnumerateAnnotations(
128 GetView(), &payload, [](AbckitCoreAnnotation *method, void *data) {
129 const auto &payload = *static_cast<Payload<const std::function<bool(core::Annotation)> &> *>(data);
130 return payload.data(core::Annotation(method, payload.config, payload.resource));
131 });
132
133 CheckError(GetApiConfig());
134 return isNormalExit;
135 }
136
GetModule()137 inline Module Class::GetModule() const
138 {
139 AbckitCoreModule *mod = GetApiConfig()->cIapi_->classGetModule(GetView());
140 CheckError(GetApiConfig());
141 return Module(mod, GetApiConfig(), GetResource());
142 }
143
GetParentFunction()144 inline Function Class::GetParentFunction() const
145 {
146 AbckitCoreFunction *func = GetApiConfig()->cIapi_->classGetParentFunction(GetView());
147 CheckError(GetApiConfig());
148 return Function(func, GetApiConfig(), GetResource());
149 }
150
GetParentNamespace()151 inline Namespace Class::GetParentNamespace() const
152 {
153 AbckitCoreNamespace *ns = GetApiConfig()->cIapi_->classGetParentNamespace(GetView());
154 CheckError(GetApiConfig());
155 return Namespace(ns, GetApiConfig(), GetResource());
156 }
157
158 } // namespace abckit::core
159
160 #endif // CPP_ABCKIT_CORE_CLASS_IMPL_H
161