1 /* 2 * Copyright (c) 2021-2022 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 p ermissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ROSEN_CLASS_VAR_DEFINITION_H 17 #define OHOS_ROSEN_CLASS_VAR_DEFINITION_H 18 19 namespace OHOS::Rosen { 20 #define DEFINE_VAR(type, memberName) \ 21 private: \ 22 type memberName##_; 23 24 #define DEFINE_VAR_WITH_LOCK(type, memberName) \ 25 private: \ 26 std::atomic<bool> memberName##atomicBool_ { false }; \ 27 type memberName##_; 28 29 #define DEFINE_VAR_DEFAULT(type, memberName, defaultValue) \ 30 private: \ 31 type memberName##_ {defaultValue}; 32 33 #define DEFINE_FUNC_GET(type, funcName, memberName) \ 34 public: \ 35 type Get##funcName() const \ 36 { \ 37 return memberName##_; \ 38 } 39 40 #define DEFINE_FUNC_GET_WITH_LOCK(type, funcName, memberName) \ 41 public: \ 42 type Get##funcName() \ 43 { \ 44 bool expect = false; \ 45 type res; \ 46 while (!memberName##atomicBool_.compare_exchange_weak(expect, true, \ 47 std::memory_order_relaxed)) { \ 48 expect = false; \ 49 } \ 50 res = memberName##_; \ 51 memberName##atomicBool_.store(false); \ 52 return res; \ 53 } 54 55 #define DEFINE_FUNC_SET(type, funcName, memberName) \ 56 public: \ 57 void Set##funcName(type value) \ 58 { \ 59 memberName##_ = value; \ 60 } 61 62 #define DEFINE_FUNC_SET_WITH_LOCK(type, funcName, memberName) \ 63 public: \ 64 void Set##funcName(type value) \ 65 { \ 66 bool expect = false; \ 67 while (!memberName##atomicBool_.compare_exchange_weak(expect, true, \ 68 std::memory_order_relaxed)) { \ 69 expect = false; \ 70 } \ 71 \ 72 memberName##_ = value; \ 73 memberName##atomicBool_.store(false); \ 74 } 75 76 #define DEFINE_VAR_FUNC_GET(type, funcName, memberName) \ 77 DEFINE_VAR(type, memberName) \ 78 DEFINE_FUNC_GET(type, funcName, memberName) 79 80 #define DEFINE_VAR_DEFAULT_FUNC_GET(type, funcName, memberName, defaultValue) \ 81 DEFINE_VAR_DEFAULT(type, memberName, defaultValue) \ 82 DEFINE_FUNC_GET(type, funcName, memberName) 83 84 #define DEFINE_VAR_DEFAULT_FUNC_SET(type, funcName, memberName, defaultValue) \ 85 DEFINE_VAR_DEFAULT(type, memberName, defaultValue) \ 86 DEFINE_FUNC_SET(type, funcName, memberName) 87 88 #define DEFINE_VAR_FUNC_GET_SET(type, funcName, memberName) \ 89 DEFINE_VAR(type, memberName) \ 90 DEFINE_FUNC_GET(type, funcName, memberName) \ 91 DEFINE_FUNC_SET(type, funcName, memberName) 92 93 #define DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(type, funcName, memberName) \ 94 DEFINE_VAR_WITH_LOCK(type, memberName) \ 95 DEFINE_FUNC_GET_WITH_LOCK(type, funcName, memberName) \ 96 DEFINE_FUNC_SET_WITH_LOCK(type, funcName, memberName) 97 98 #define DEFINE_VAR_DEFAULT_FUNC_GET_SET(type, funcName, memberName, defaultValue) \ 99 DEFINE_VAR_DEFAULT(type, memberName, defaultValue) \ 100 DEFINE_FUNC_GET(type, funcName, memberName) \ 101 DEFINE_FUNC_SET(type, funcName, memberName) 102 } // namespace OHOS::Rosen 103 #endif // OHOS_ROSEN_CLASS_VAR_DEFINITION_H 104