1 /* 2 * Copyright (c) 2025 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 COMMON_INTERFACES_OBJECTS_UTILS_FIELD_MACRO_H 17 #define COMMON_INTERFACES_OBJECTS_UTILS_FIELD_MACRO_H 18 19 #include <type_traits> 20 #include <utility> 21 22 #include "common_interfaces/objects/utils/objects_traits.h" 23 #include "common_interfaces/base_runtime.h" 24 25 // CC-OFFNXT(C_RULE_ID_DEFINE_LENGTH_LIMIT) solid logic 26 // CC-OFFNXT(G.PRE.02) code readability 27 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 28 #define PRIMITIVE_FIELD(name, type, offset, endOffset) \ 29 static constexpr size_t endOffset = (offset) + sizeof(type); \ 30 inline void Set##name(type value) \ 31 { \ 32 /* CC-OFFNXT(G.PRE.02) code readability */ \ 33 auto *addr = reinterpret_cast<type *>(ToUintPtr(this) + offset); \ 34 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 35 *addr = value; \ 36 } \ 37 inline type Get##name() const \ 38 { \ 39 /* CC-OFFNXT(G.PRE.02) code readability */ \ 40 auto *addr = reinterpret_cast<type *>(ToUintPtr(this) + offset); \ 41 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 42 return *addr; \ 43 } 44 45 // CC-OFFNXT(C_RULE_ID_DEFINE_LENGTH_LIMIT) solid logic 46 // CC-OFFNXT(G.PRE.02) code readability 47 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 48 #define POINTER_FIELD(name, offset, endOffset) \ 49 static constexpr size_t endOffset = (offset) + sizeof(uint64_t); \ 50 template <typename PointerType, typename WriteBarrier, \ 51 objects_traits::enable_if_is_write_barrier<std::decay_t<WriteBarrier>> = 0> \ 52 inline void Set##name(WriteBarrier &&writeBarrier, PointerType value) \ 53 { \ 54 /* CC-OFFNXT(G.PRE.02) code readability */ \ 55 void *obj = static_cast<void *>(this); \ 56 std::invoke(writeBarrier, obj, offset, value); \ 57 } \ 58 \ 59 template <typename PointerType, typename ReadBarrier, \ 60 objects_traits::enable_if_is_read_barrier<std::decay_t<ReadBarrier>, PointerType> = 0> \ 61 inline PointerType Get##name(ReadBarrier &&readBarrier) const \ 62 { \ 63 /* CC-OFFNXT(G.PRE.02) code readability */ \ 64 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 65 return std::invoke(readBarrier, const_cast<void *>(static_cast<const void *>(this)), offset); \ 66 } 67 68 #if !defined(NDEBUG) 69 // CC-OFFNXT(C_RULE_ID_DEFINE_LENGTH_LIMIT) solid logic 70 // CC-OFFNXT(G.PRE.02) code readability 71 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 72 #define BASE_CAST_CHECK(CAST_TYPE, CHECK_METHOD) \ 73 static inline CAST_TYPE *Cast(BaseObject *object) \ 74 { \ 75 /* CC-OFFNXT(G.PRE.02) code readability */ \ 76 if (!object->GetBaseClass()->CHECK_METHOD()) { \ 77 std::abort(); \ 78 } \ 79 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 80 /* CC-OFFNXT(G.PRE.02) code readability */ \ 81 return static_cast<CAST_TYPE *>(object); \ 82 } \ 83 static inline const CAST_TYPE *ConstCast(const BaseObject *object) \ 84 { \ 85 /* CC-OFFNXT(G.PRE.02) code readability */ \ 86 if (!object->GetBaseClass()->CHECK_METHOD()) { \ 87 std::abort(); \ 88 } \ 89 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 90 /* CC-OFFNXT(G.PRE.02) code readability */ \ 91 return static_cast<const CAST_TYPE *>(object); \ 92 } 93 #else 94 // CC-OFFNXT(C_RULE_ID_DEFINE_LENGTH_LIMIT) solid logic 95 // CC-OFFNXT(G.PRE.02) code readability 96 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 97 #define BASE_CAST_CHECK(CAST_TYPE, CHECK_METHOD) \ 98 static inline CAST_TYPE *Cast(BaseObject *object) \ 99 { \ 100 /* CC-OFFNXT(G.PRE.02) code readability */ \ 101 DCHECK_CC(object->CHECK_METHOD()); \ 102 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 103 /* CC-OFFNXT(G.PRE.02) code readability */ \ 104 return static_cast<CAST_TYPE *>(object); \ 105 } \ 106 static const inline CAST_TYPE *ConstCast(const BaseObject *object) \ 107 { \ 108 /* CC-OFFNXT(G.PRE.02) code readability */ \ 109 DCHECK_CC(object->CHECK_METHOD()); \ 110 /* CC-OFFNXT(G.PRE.05) C_RULE_ID_KEYWORD_IN_DEFINE */ \ 111 /* CC-OFFNXT(G.PRE.02) code readability */ \ 112 return static_cast<const CAST_TYPE *>(object); \ 113 } 114 #endif 115 #endif //COMMON_INTERFACES_OBJECTS_UTILS_FIELD_MACRO_H