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