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 CORE__ECS_HELPER__PROPERTY_TOOLS__PROPERTY_DATA_H 17 #define CORE__ECS_HELPER__PROPERTY_TOOLS__PROPERTY_DATA_H 18 19 #include <PropertyTools/property_value.h> 20 #include <cstddef> 21 #include <cstdint> 22 23 #include <base/containers/array_view.h> 24 #include <base/containers/string.h> 25 #include <base/containers/string_view.h> 26 #include <base/namespace.h> 27 #include <core/namespace.h> 28 #include <core/property/intf_property_handle.h> 29 30 CORE_BEGIN_NAMESPACE() 31 struct Property; 32 class IPropertyApi; 33 class PropertyData : public IPropertyHandle { 34 public: 35 PropertyData(); 36 37 PropertyData(const PropertyData& other) = delete; 38 PropertyData(PropertyData&& other) = delete; 39 PropertyData& operator=(const PropertyData& other) = delete; 40 PropertyData& operator=(PropertyData&& other) = delete; 41 42 struct PropertyOffset { 43 constexpr explicit operator bool() const noexcept 44 { 45 return property != nullptr; 46 } 47 const Property* property { nullptr }; 48 uintptr_t offset { 0U }; 49 size_t index { 0 }; 50 BASE_NS::string propertyPath; 51 }; 52 53 bool WLock(IPropertyHandle& handle); // no-copy direct-access (Locks the datahandle); 54 bool WUnlock(const IPropertyHandle& handle); // (releases the datahandle lock, and removes ref) 55 bool RLock(const IPropertyHandle& handle); // no-copy direct-access (Locks the datahandle); 56 bool RUnlock(const IPropertyHandle& handle); // (releases the datahandle lock, and removes ref) 57 58 /** no-copy direct write access 59 * Searches for property and locks the handle 60 * if property was found, returns valid PropertyOffset and keeps it locked 61 * if not found, releases the lock immediately and returns null PropertyOffset 62 */ 63 PropertyOffset WLock(IPropertyHandle& handle, BASE_NS::string_view propertyPath); 64 65 /** no-copy direct read access 66 * Searches for property and locks the handle 67 * if property was found, returns valid PropertyOffset and keeps it locked 68 * if not found, releases the lock immediately and returns null PropertyOffset 69 */ 70 PropertyOffset RLock(const IPropertyHandle& handle, BASE_NS::string_view propertyPath); 71 72 /** Find offset and property type for a property. 73 * @param properties Property metadata to search from. 74 * @param propertyPath property path to search for. 75 * @param baseOffset offset to (already locked) property data. 76 * @return PropertyOffset-structure. If property was not found, returns null PropertyOffset. 77 */ 78 static PropertyOffset FindProperty( 79 BASE_NS::array_view<const Property> properties, BASE_NS::string_view propertyPath, uintptr_t baseOffset); 80 81 /** Find offset and property type for a property. 82 * @param properties Property metadata to search from. 83 * @param propertyPath Property path to search for. 84 * @return PropertyOffset-structure. If property was not found, returns null PropertyOffset. 85 */ 86 static PropertyOffset FindProperty( 87 BASE_NS::array_view<const Property> properties, BASE_NS::string_view propertyPath); 88 89 ~PropertyData() override; 90 size_t PropertyCount() const; 91 92 BASE_NS::array_view<const Property> MetaData() const; 93 const Property* MetaData(size_t index) const; 94 95 // deprecated accessors.. (PropertyValue is deprecated) 96 PropertyValue Get(size_t index); 97 PropertyValue Get(size_t index) const; 98 99 PropertyValue Get(BASE_NS::string_view name); 100 PropertyValue Get(BASE_NS::string_view name) const; 101 102 PropertyValue operator[](size_t index); 103 PropertyValue operator[](size_t index) const; 104 105 PropertyValue operator[](const BASE_NS::string_view& name); 106 PropertyValue operator[](const BASE_NS::string_view& name) const; 107 108 // Implement the IPropertyHandle api. 109 const IPropertyApi* Owner() const override; 110 size_t Size() const override; 111 const void* RLock() const override; 112 void RUnlock() const override; 113 void* WLock() override; 114 void WUnlock() override; 115 116 protected: 117 void Reset(); 118 119 private: 120 const IPropertyApi* owner_ = nullptr; 121 size_t size_ = 0; 122 const void* data_ = nullptr; 123 uint8_t* dataW_ = nullptr; 124 const IPropertyHandle* dataHandle_ = nullptr; 125 IPropertyHandle* dataHandleW_ = nullptr; 126 }; 127 CORE_END_NAMESPACE() 128 129 #endif // CORE__ECS_HELPER__PROPERTY_TOOLS__PROPERTY_DATA_H 130