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_H 17 #define CPP_ABCKIT_CORE_CLASS_H 18 19 #include "../base_classes.h" 20 #include "function.h" 21 22 #include <functional> 23 #include <vector> 24 25 namespace abckit::core { 26 27 /** 28 * @brief Class 29 */ 30 class Class : public ViewInResource<AbckitCoreClass *, const File *> { 31 // We restrict constructors in order to prevent C/C++ API mix-up by user. 32 /// @brief to access private constructor 33 friend Module; 34 /// @brief to access private constructor 35 friend Namespace; 36 /// @brief to access private constructor 37 friend class Function; 38 /// @brief to access private constructor 39 friend class abckit::Type; 40 /// @brief abckit::DefaultHash<Class> 41 friend abckit::DefaultHash<Class>; 42 /// @brief to access private constructor 43 friend File; 44 45 protected: 46 /// @brief Core API View type 47 using CoreViewT = Class; 48 49 public: 50 /** 51 * @brief Construct a new Class object 52 * @param other 53 */ 54 Class(const Class &other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 55 56 /** 57 * @brief Constructor 58 * @param other 59 * @return Class& 60 */ 61 Class &operator=(const Class &other) = default; 62 63 /** 64 * @brief Construct a new Class object 65 * @param other 66 */ 67 Class(Class &&other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 68 69 /** 70 * @brief Constructor 71 * @param other 72 * @return Class& 73 */ 74 Class &operator=(Class &&other) = default; 75 76 /** 77 * @brief Destroy the Class object 78 */ 79 ~Class() override = default; 80 81 /** 82 * @brief Returns binary file that the Class is a part of. 83 * @return Pointer to the `File`. 84 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 85 */ 86 const File *GetFile() const; 87 88 /** 89 * @brief Get Class name 90 * @return `std::string` 91 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 92 */ 93 std::string GetName() const; 94 95 /** 96 * @brief Returns module for this `Class`. 97 * @return Owning `core::Module`. 98 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `bool(*this)` results in `false`. 99 */ 100 core::Module GetModule() const; 101 102 /** 103 * @brief Get vector with all Methods 104 * @return std::vector<core::Function> 105 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 106 */ 107 std::vector<core::Function> GetAllMethods() const; 108 109 /** 110 * @brief Get vector with all Annotations 111 * @return std::vector<core::Annotation> 112 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 113 */ 114 std::vector<core::Annotation> GetAnnotations() const; 115 116 /** 117 * @brief Enumerates methods of Class, invoking callback `cb` for each method. 118 * @return `false` if was early exited. Otherwise - `true`. 119 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 120 * should continue. 121 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 122 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 123 */ 124 bool EnumerateMethods(const std::function<bool(core::Function)> &cb) const; 125 126 /** 127 * @brief Enumerates annotations of Class, invoking callback `cb` for each annotation. 128 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 129 * should continue. 130 * @return false` if was early exited. Otherwise - `true`. 131 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 132 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false. 133 */ 134 bool EnumerateAnnotations(const std::function<bool(core::Annotation)> &cb) const; 135 136 /** 137 * @brief Returns parent function for class. 138 * @return `core::Function`. 139 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 140 */ 141 Function GetParentFunction() const; 142 143 /** 144 * @brief Returns parent namespace for class. 145 * @return `core::Namespace`. 146 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 147 */ 148 Namespace GetParentNamespace() const; 149 150 private: 151 inline bool GetAllMethodsInner(std::vector<core::Function> &methods) const; 152 153 inline bool GetAllAnnotationsInner(std::vector<core::Annotation> &anns) const; 154 155 Class(AbckitCoreClass *klass, const ApiConfig *conf, const File *file); 156 157 const ApiConfig *conf_; 158 159 protected: GetApiConfig()160 const ApiConfig *GetApiConfig() const override 161 { 162 return conf_; 163 } 164 }; 165 166 } // namespace abckit::core 167 168 #endif // CPP_ABCKIT_CORE_CLASS_H 169