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_INTERFACE_OBJECTS_BASE_CLASS_H 17 #define COMMON_INTERFACE_OBJECTS_BASE_CLASS_H 18 19 #include "base/bit_field.h" 20 21 namespace common { 22 class BaseObject; 23 24 static constexpr uint32_t BITS_PER_BYTE = 8; 25 26 enum class CommonType : uint8_t { 27 INVALID = 0, 28 FIRST_OBJECT_TYPE, 29 LINE_STRING = FIRST_OBJECT_TYPE, 30 31 SLICED_STRING, 32 TREE_STRING, 33 LAST_OBJECT_TYPE = TREE_STRING, 34 35 STRING_FIRST = LINE_STRING, 36 STRING_LAST = TREE_STRING, 37 }; 38 39 class BaseClass { 40 public: 41 BaseClass() = delete; 42 ~BaseClass() = delete; 43 NO_MOVE_SEMANTIC_CC(BaseClass); 44 NO_COPY_SEMANTIC_CC(BaseClass); 45 46 static constexpr size_t TYPE_BITFIELD_NUM = BITS_PER_BYTE * sizeof(CommonType); 47 48 using ObjectTypeBits = BitField<CommonType, 0, TYPE_BITFIELD_NUM>; // 8 49 GetObjectType()50 CommonType GetObjectType() const 51 { 52 return ObjectTypeBits::Decode(bitfield_); 53 } 54 SetObjectType(CommonType type)55 void SetObjectType(CommonType type) 56 { 57 bitfield_ = ObjectTypeBits::Update(bitfield_, type); 58 } 59 ClearBitField()60 void ClearBitField() 61 { 62 bitfield_ = 0; 63 } 64 IsString()65 bool IsString() const 66 { 67 return GetObjectType() >= CommonType::LINE_STRING && GetObjectType() <= CommonType::TREE_STRING; 68 } 69 IsLineString()70 bool IsLineString() const 71 { 72 return GetObjectType() == CommonType::LINE_STRING; 73 } 74 IsSlicedString()75 bool IsSlicedString() const 76 { 77 return GetObjectType() == CommonType::SLICED_STRING; 78 } 79 IsTreeString()80 bool IsTreeString() const 81 { 82 return GetObjectType() == CommonType::TREE_STRING; 83 } 84 85 private: 86 // header_ is a padding field in base class, it will be used to store the root class in ets_runtime. 87 [[maybe_unused]] uint64_t header_; 88 // bitfield will be initialized as the bitfield_ and bitfield1_ of js_hclass. 89 // Now only the low 8bits in bitfield are used as the common type encode. Other field has no specific means here 90 // but should follow the bitfield and bitfield1_ defines in js_hclass. 91 uint64_t bitfield_; 92 GetBitField()93 uint64_t GetBitField() const 94 { 95 return bitfield_; 96 } 97 }; 98 }; 99 #endif //COMMON_INTERFACE_OBJECTS_BASE_CLASS_H