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 API_ECS_SERIALIZER_PROPERTYUTIL_H
17 #define API_ECS_SERIALIZER_PROPERTYUTIL_H
18
19 #include <base/containers/string.h>
20 #include <core/property/intf_property_api.h>
21 #include <core/property/property_types.h>
22 #include <core/property/scoped_handle.h>
23 #include <core/property_tools/property_data.h>
24 #include <ecs_serializer/namespace.h>
25
ECS_SERIALIZER_BEGIN_NAMESPACE()26 ECS_SERIALIZER_BEGIN_NAMESPACE()
27
28 inline bool IsPropertyContainer(const CORE_NS::Property& property)
29 {
30 return property.type == PROPERTYTYPE(CORE_NS::IPropertyHandle*);
31 }
32
ResolveContainerProperty(const CORE_NS::IPropertyHandle & handle,const BASE_NS::string & propertyPath,BASE_NS::string & path,BASE_NS::string & name)33 inline CORE_NS::IPropertyHandle* ResolveContainerProperty(const CORE_NS::IPropertyHandle& handle,
34 const BASE_NS::string& propertyPath, BASE_NS::string& path, BASE_NS::string& name)
35 {
36 // Extract property path.
37 auto separatorPosition = propertyPath.find_first_of('.');
38 if (separatorPosition == BASE_NS::string::npos) {
39 return nullptr;
40 }
41
42 path = propertyPath.substr(0, separatorPosition);
43 name = propertyPath.substr(separatorPosition + 1);
44
45 CORE_NS::IPropertyHandle* result = nullptr;
46
47 uintptr_t offset = uintptr_t(handle.RLock());
48
49 // Get potential container.
50 auto propertyData = CORE_NS::PropertyData::FindProperty(handle.Owner()->MetaData(), path, offset);
51 if (propertyData) {
52 // Ensure it is a container.
53 if (IsPropertyContainer(*propertyData.property)) {
54 // Try to flush value to container.
55 result = *(CORE_NS::IPropertyHandle**)(propertyData.offset);
56 }
57 }
58
59 handle.RUnlock();
60
61 return result;
62 }
63
64 ECS_SERIALIZER_END_NAMESPACE()
65
66 #endif // API_ECS_SERIALIZER_PROPERTYUTIL_H
67