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_UTILS_H 17 #define CPP_ABCKIT_UTILS_H 18 19 #include "../../c/statuses.h" 20 21 #include <string> 22 23 #ifdef ABCKIT_USE_EXCEPTIONS 24 #include <stdexcept> 25 #endif 26 27 namespace abckit { 28 29 class ApiConfig; 30 31 // CC-OFFNXT(G.FUD.06) perf critical 32 // NOLINTBEGIN(performance-unnecessary-value-param) StatusToString(AbckitStatus status)33inline std::string StatusToString(AbckitStatus status) 34 { 35 switch (status) { 36 case ABCKIT_STATUS_NO_ERROR: 37 return "No error"; 38 case ABCKIT_STATUS_BAD_ARGUMENT: 39 return "Bad argument"; 40 case ABCKIT_STATUS_MEMORY_ALLOCATION: 41 return "Memory allocation error"; 42 case ABCKIT_STATUS_WRONG_MODE: 43 return "Wrong mode"; 44 case ABCKIT_STATUS_WRONG_TARGET: 45 return "Wrong target"; 46 case ABCKIT_STATUS_WRONG_LITERAL_TYPE: 47 return "Wrong literal type"; 48 case ABCKIT_STATUS_UNSUPPORTED: 49 return "Unsupported feature"; 50 case ABCKIT_STATUS_WRONG_CTX: 51 return "Wrong context"; 52 case ABCKIT_STATUS_INTERNAL_ERROR: 53 return "Internal error"; 54 default: 55 break; 56 } 57 return "Unsupported status type"; 58 } 59 // NOLINTEND(performance-unnecessary-value-param) 60 61 #ifdef ABCKIT_USE_EXCEPTIONS 62 /** 63 * @brief Exception 64 */ 65 class Exception : public std::runtime_error { 66 public: 67 /** 68 * @brief Constructor 69 * @param e - status 70 */ Exception(AbckitStatus e)71 explicit Exception(AbckitStatus e) : std::runtime_error(StatusToString(e)) {} 72 73 /** 74 * @brief what 75 * @return what 76 */ 77 // NOLINTNEXTLINE(readability-identifier-naming) what()78 virtual const char *what() const noexcept override 79 { 80 return std::runtime_error::what(); 81 } 82 }; 83 #else 84 85 /** 86 * @brief Exception 87 */ 88 class Exception { 89 public: 90 /** 91 * @brief Constructor 92 * @param e - status 93 */ Exception(AbckitStatus e)94 explicit Exception(AbckitStatus e) : whatMessage_(StatusToString(e)) {} 95 96 /** 97 * @brief what 98 * @return string 99 */ 100 // CC-OFFNXT(G.NAM.03) made to be compatible with std::runtime_error::what method 101 // NOLINTNEXTLINE(readability-identifier-naming) what()102 const char *what() const noexcept 103 { 104 // CC-OFFNXT(G.STD.04) made to be compatible with std::runtime_error::what method 105 return whatMessage_.c_str(); 106 } 107 108 private: 109 std::string whatMessage_; 110 }; 111 #endif 112 113 /** 114 * @brief IErrorHandler 115 */ 116 class IErrorHandler { 117 public: 118 /** 119 * Сonstructor 120 */ 121 IErrorHandler() = default; 122 123 /** 124 * @brief Constructor 125 * @param other 126 */ 127 IErrorHandler(const IErrorHandler &other) = default; 128 129 /** 130 * @brief Constructor 131 * @param other 132 * @return IErrorHandler 133 */ 134 IErrorHandler &operator=(const IErrorHandler &other) = default; 135 136 /** 137 * @brief Constructor 138 * @param other 139 */ 140 IErrorHandler(IErrorHandler &&other) = default; 141 142 /** 143 * @brief Constructor 144 * @param other 145 * @return IErrorHandler 146 */ 147 IErrorHandler &operator=(IErrorHandler &&other) = default; 148 149 /** 150 * @brief Destructor 151 */ 152 virtual ~IErrorHandler() = default; 153 154 //! @cond Doxygen_Suppress 155 virtual void HandleError(Exception &&e) = 0; 156 //! @endcond 157 }; 158 159 /** 160 * @brief DefaultErrorHandler 161 */ 162 class DefaultErrorHandler final : public IErrorHandler { 163 public: 164 /** 165 * Сonstructor 166 */ 167 DefaultErrorHandler() = default; 168 169 /** 170 * @brief Constructor 171 * @param other 172 */ 173 DefaultErrorHandler(const DefaultErrorHandler &other) = default; 174 175 /** 176 * @brief Constructor 177 * @param other 178 * @return DefaultErrorHandler 179 */ 180 DefaultErrorHandler &operator=(const DefaultErrorHandler &other) = default; 181 182 /** 183 * @brief Constructor 184 * @param other 185 */ 186 DefaultErrorHandler(DefaultErrorHandler &&other) = default; 187 188 /** 189 * @brief Constructor 190 * @param other 191 * @return DefaultErrorHandler 192 */ 193 DefaultErrorHandler &operator=(DefaultErrorHandler &&other) = default; 194 195 /** 196 * @brief Destructor 197 */ 198 ~DefaultErrorHandler() override = default; 199 200 /** 201 * Handle error 202 * @param e - exception 203 */ HandleError(Exception && e)204 void HandleError([[maybe_unused]] Exception &&e) override 205 { 206 // Default behaviour - do nothing. 207 // If at compile time exceptions are enabled - re-throw. 208 #ifdef ABCKIT_USE_EXCEPTIONS 209 throw e; 210 #endif 211 } 212 }; 213 214 /** 215 * @brief IResourceDeleter 216 */ 217 class IResourceDeleter { 218 public: 219 /** 220 * Сonstructor 221 */ 222 IResourceDeleter() = default; 223 224 /** 225 * @brief Constructor 226 * @param other 227 */ 228 IResourceDeleter(const IResourceDeleter &other) = default; 229 230 /** 231 * @brief Constructor 232 * @param other 233 * @return IResourceDeleter 234 */ 235 IResourceDeleter &operator=(const IResourceDeleter &other) = default; 236 237 /** 238 * @brief Constructor 239 * @param other 240 */ 241 IResourceDeleter(IResourceDeleter &&other) = default; 242 243 /** 244 * @brief Constructor 245 * @param other 246 * @return IResourceDeleter 247 */ 248 IResourceDeleter &operator=(IResourceDeleter &&other) = default; 249 250 /** 251 * @brief Destructor 252 */ 253 virtual ~IResourceDeleter() = default; 254 255 //! @cond Doxygen_Suppress 256 virtual void DeleteResource() = 0; 257 //! @endcond 258 }; 259 260 /** 261 * @brief DefaultResourceDeleter 262 */ 263 class DefaultResourceDeleter final : public IResourceDeleter { 264 public: 265 /** 266 * Сonstructor 267 */ 268 DefaultResourceDeleter() = default; 269 270 /** 271 * @brief Constructor 272 * @param other 273 */ 274 DefaultResourceDeleter(const DefaultResourceDeleter &other) = default; 275 276 /** 277 * @brief Constructor 278 * @param other 279 * @return `DefaultResourceDeleter` 280 */ 281 DefaultResourceDeleter &operator=(const DefaultResourceDeleter &other) = default; 282 283 /** 284 * @brief Constructor 285 * @param other 286 */ 287 DefaultResourceDeleter(DefaultResourceDeleter &&other) = default; 288 289 /** 290 * @brief Constructor 291 * @param other 292 * @return `DefaultResourceDeleter` 293 */ 294 DefaultResourceDeleter &operator=(DefaultResourceDeleter &&other) = default; 295 296 /** 297 * Destructor 298 */ 299 ~DefaultResourceDeleter() override = default; 300 301 /** 302 * @brief Delete resource 303 */ DeleteResource()304 void DeleteResource() override 305 { /* Do nothing by default. Debug log here, probably? */ 306 } 307 }; 308 309 } // namespace abckit 310 311 #endif // CPP_ABCKIT_UTILS_H 312