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_NAMESPACE_H 17 #define CPP_ABCKIT_CORE_NAMESPACE_H 18 19 #include "../base_classes.h" 20 21 #include <functional> 22 #include <string_view> 23 24 namespace abckit::core { 25 26 /** 27 * @brief Namespace 28 */ 29 class Namespace : public ViewInResource<AbckitCoreNamespace *, const File *> { 30 // We restrict constructors in order to prevent C/C++ API mix-up by user. 31 /// @brief to access private constructor 32 friend class abckit::File; 33 /// @brief to access private constructor 34 friend class core::Module; 35 /// @brief to access private constructor 36 friend class core::Class; 37 /// @brief to access private constructor 38 friend class core::Function; 39 /// @brief abckit::DefaultHash<Namespace> 40 friend class abckit::DefaultHash<Namespace>; 41 42 protected: 43 /// @brief Core API View type 44 using CoreViewT = Namespace; 45 46 public: 47 /** 48 * @brief Construct a new Namespace object 49 * @param other 50 */ 51 Namespace(const Namespace &other) = default; 52 53 /** 54 * @brief 55 * @param other 56 * @return Namespace& 57 */ 58 Namespace &operator=(const Namespace &other) = default; 59 60 /** 61 * @brief Construct a new Namespace object 62 * @param other 63 */ 64 Namespace(Namespace &&other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 65 66 /** 67 * @brief 68 * @param other 69 * @return Namespace& 70 */ 71 Namespace &operator=(Namespace &&other) = default; 72 73 /** 74 * @brief Destroy the Namespace object 75 */ 76 ~Namespace() override = default; 77 78 /** 79 * @brief Return namespace's name. 80 * @return `std::string` 81 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 82 */ GetName()83 std::string GetName() const 84 { 85 AbckitString *abcName = GetApiConfig()->cIapi_->namespaceGetName(GetView()); 86 CheckError(GetApiConfig()); 87 std::string name = GetApiConfig()->cIapi_->abckitStringToString(abcName); 88 CheckError(GetApiConfig()); 89 return name; 90 } 91 92 /** 93 * @brief Returns parent namespace. 94 * @return `core::Namespace` or NULL if namespace has no parent namespace. 95 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 96 */ GetParentNamespace()97 Namespace GetParentNamespace() const 98 { 99 AbckitCoreNamespace *parent = GetApiConfig()->cIapi_->namespaceGetParentNamespace(GetView()); 100 return Namespace(parent, GetApiConfig(), GetResource()); 101 } 102 103 /** 104 * @brief Enumerates namespaces defined inside of the Namespace, invoking callback `cb` for each inner 105 * namespace. 106 * @return `false` if was early exited. Otherwise - `true`. 107 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 108 * should continue. 109 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 110 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 111 */ 112 bool EnumerateNamespaces(const std::function<bool(core::Namespace)> &cb) const; 113 114 /** 115 * @brief Enumerates classes of the Namespace, invoking callback `cb` for each class. 116 * @return `false` if was early exited. Otherwise - `true`. 117 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 118 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 119 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 120 */ 121 bool EnumerateClasses(const std::function<bool(core::Class)> &cb) const; 122 123 /** 124 * @brief Enumerates top level functions of the Namespace, invoking callback `cb` for each top level function. 125 * @return `false` if was early exited. Otherwise - `true`. 126 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 127 * should continue. 128 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 129 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 130 */ 131 bool EnumerateTopLevelFunctions(const std::function<bool(core::Function)> &cb) const; 132 133 private: Namespace(AbckitCoreNamespace * ns,const ApiConfig * conf,const File * file)134 Namespace(AbckitCoreNamespace *ns, const ApiConfig *conf, const File *file) : ViewInResource(ns), conf_(conf) 135 { 136 SetResource(file); 137 }; 138 const ApiConfig *conf_; 139 140 protected: 141 /** 142 * @brief Returns Api Config. 143 * @return const ApiConfig* 144 */ GetApiConfig()145 const ApiConfig *GetApiConfig() const override 146 { 147 return conf_; 148 } 149 }; 150 151 } // namespace abckit::core 152 153 #endif // CPP_ABCKIT_CORE_NAMESPACE_H 154