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_ARKTS_MODULE_H 17 #define CPP_ABCKIT_ARKTS_MODULE_H 18 19 #include "../core/module.h" 20 #include "../base_concepts.h" 21 #include "./import_descriptor.h" 22 #include "./export_descriptor.h" 23 #include "./annotation_interface.h" 24 25 #include <string_view> 26 27 namespace abckit::arkts { 28 29 /** 30 * @brief Module 31 */ 32 class Module final : public core::Module { 33 // To access private constructor. 34 // We restrict constructors in order to prevent C/C++ API mix-up by user. 35 /// @brief to access private constructor 36 friend class abckit::File; 37 /// @brief abckit::DefaultHash<Module> 38 friend class abckit::DefaultHash<Module>; 39 /// @brief to access private TargetCast 40 friend class abckit::traits::TargetCheckCast<Module>; 41 42 public: 43 /** 44 * @brief Constructor Arkts API Module from the Core API with compatibility check 45 * @param coreOther - Core API Module 46 */ 47 explicit Module(const core::Module &coreOther); 48 49 /** 50 * @brief Construct a new Module object 51 * @param other 52 */ 53 Module(const Module &other) = default; 54 55 /** 56 * @brief Constructor 57 * @param other 58 * @return Module& 59 */ 60 Module &operator=(const Module &other) = default; 61 62 /** 63 * @brief Construct a new Module object 64 * @param other 65 */ 66 Module(Module &&other) = default; 67 68 /** 69 * @brief Constructor 70 * @param other 71 * @return Module& 72 */ 73 Module &operator=(Module &&other) = default; 74 75 /** 76 * @brief Destroy the Module object 77 */ 78 ~Module() override = default; 79 80 /** 81 * @brief Adds import from one ArktsV1 module to another ArktsV1 module. 82 * @return Pointer to the newly created import descriptor. 83 * @param [ in ] imported - The module the `importing` module imports from. 84 * @param [ in ] name - Import name. For namespace imports equals to "*". For default imports equals to "default". 85 * For regular imports is the same as in user code. 86 * @param [ in ] alias - Alias name for the import. For namespace imports is the same as in user code. For default 87 * import is the same as the default import name in user code. For regular imports is the same as in user code. 88 * @note Allocates 89 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `imported` is false. 90 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 91 */ 92 ImportDescriptor AddImportFromArktsV1ToArktsV1(Module imported, std::string_view name, 93 std::string_view alias) const; 94 95 /** 96 * @brief Removes import `id` from Module. 97 * @param [ in ] desc - Import to remove from the Module. 98 * @return New state of Module. 99 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 100 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if Module does not have the import descriptor `id`. 101 * @note Set `ABCKIT_STATUS_UNSUPPORTED` error if Module doesn't have `ABCKIT_TARGET_ARK_TS_V1` target. 102 */ 103 Module RemoveImport(arkts::ImportDescriptor desc) const; 104 105 /** 106 * @brief Adds export from one Arkts module to another Arkts module. 107 * @return Newly created export descriptor. 108 * @param [ in ] exported - The module the entity is exported from. In case of local export is the same as 109 * `exporting`. 110 * @param [ in ] name - Import name. For namespace imports equals to "*". For default imports equals to "default". 111 * For regular imports is the same as in user code. 112 * @param [ in ] alias - Alias. For namespace imports equals to "*". For default imports equals to "default". 113 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 114 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `exported` is false. 115 */ 116 arkts::ExportDescriptor AddExportFromArktsV1ToArktsV1(arkts::Module exported, std::string_view name, 117 std::string_view alias) const; 118 119 /** 120 * @brief Removes export `ed` from Module. 121 * @param [ in ] desc - Export to remove from the Module. 122 * @return New state of Module. 123 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 124 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if Module does not have the export descriptor `ed`. 125 * @note Set `ABCKIT_STATUS_UNSUPPORTED` error if Module doesn't have `ABCKIT_TARGET_ARK_TS_V1` target. 126 */ 127 Module RemoveExport(arkts::ExportDescriptor desc) const; 128 129 /** 130 * @brief Adds new annotation interface to the Module. 131 * @return Newly constructed annotation interface. 132 * @param [ in ] name - Annotation interface name. 133 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 134 * @note Set `ABCKIT_STATUS_UNSUPPORTED` error if module Module doesn't have `ABCKIT_TARGET_ARK_TS_V1` target. 135 * @note Allocates 136 */ 137 arkts::AnnotationInterface AddAnnotationInterface(std::string_view name) const; 138 139 private: 140 /** 141 * @brief Converts underlying module from Core to Arkts target 142 * @return AbckitArktsModule* - converted module 143 * @note Set `ABCKIT_STATUS_WRONG_TARGET` error if `this` does not have `ABCKIT_TARGET_ARK_TS_V1` or 144 * `ABCKIT_TARGET_ARK_TS_V2` target. 145 */ 146 AbckitArktsModule *TargetCast() const; 147 148 ABCKIT_NO_UNIQUE_ADDRESS traits::TargetCheckCast<Module> targetChecker_; 149 }; 150 151 } // namespace abckit::arkts 152 153 #endif // CPP_ABCKIT_ARKTS_MODULE_H 154