1/* 2 * Copyright (c) 2021 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 AIE_MACROS_H 17#define AIE_MACROS_H 18 19#include <cstdlib> 20#include <new> 21 22namespace OHOS { 23namespace AI { 24/** 25 * If x is true, return ret, pass otherwise 26 */ 27#define CHK_RET(x, ret) \ 28 do { \ 29 if (x) { \ 30 return ret; \ 31 } \ 32 } while (0) 33 34/** 35 * If x is true, return void, pass otherwise 36 */ 37#define CHK_RET_NONE(x) \ 38 do { \ 39 if (x) { \ 40 return; \ 41 } \ 42 } while (0) 43 44/** 45 * Try assign classname to var, assign nullptr if failed 46 */ 47#define AIE_NEW(var, classname) \ 48 do { \ 49 var = new(std::nothrow) classname; \ 50 } while (0) 51 52/** 53 * Try delete the pointer p, and set p to nullptr. 54 */ 55#define AIE_DELETE(p) \ 56 do { \ 57 if (p == nullptr) { \ 58 break; \ 59 } \ 60 delete p; \ 61 p = nullptr; \ 62 } while (0) 63 64/** 65 * Try delete the pointer p[], and set p to nullptr. 66 */ 67#define AIE_DELETE_ARRAY(p) \ 68 do { \ 69 if (p == nullptr) { \ 70 break; \ 71 } \ 72 delete[] p; \ 73 p = nullptr; \ 74 } while (0) 75 76#undef FORBID_COPY_AND_ASSIGN 77/** 78 * Forbid copy and assign classname, for singleton pattern. 79 */ 80#define FORBID_COPY_AND_ASSIGN(CLASSNAME) \ 81private: \ 82 CLASSNAME(const CLASSNAME&) = delete; \ 83 CLASSNAME& operator = (const CLASSNAME&) = delete; \ 84 CLASSNAME(CLASSNAME&&) = delete; \ 85 CLASSNAME& operator = (CLASSNAME&&) = delete 86 87#undef FORBID_CREATE_BY_SELF 88/** 89 * Forbid create classname, for singleton pattern. 90 */ 91#define FORBID_CREATE_BY_SELF(CLASSNAME) \ 92private: \ 93 CLASSNAME(); \ 94 ~CLASSNAME() 95} // namespace AI 96} // namespace OHOS 97 98#endif // AIE_MACROS_H 99