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 SCENE_SRC_SERIALIZATION_UTIL_H
17 #define SCENE_SRC_SERIALIZATION_UTIL_H
18
19 #include <meta/api/metadata_util.h>
20
SCENE_BEGIN_NAMESPACE()21 SCENE_BEGIN_NAMESPACE()
22 // workaround for now to find forwarded properties for scene objects
23 // notice that this will instantiate all properties
24 inline BASE_NS::vector<META_NS::IProperty::ConstPtr> GetAllProperties(const META_NS::IMetadata& m)
25 {
26 BASE_NS::vector<META_NS::IProperty::ConstPtr> res;
27 for (auto&& d : m.GetAllMetadatas(META_NS::MetadataType::PROPERTY)) {
28 if (auto p = m.GetProperty(d.name)) {
29 res.push_back(p);
30 }
31 }
32 return res;
33 }
34
35 void AddObjectProperties(const META_NS::IObject& obj, META_NS::IMetadata& out);
36 void AddFlatProperties(const META_NS::IMetadata& in, META_NS::IObject& parent);
37
SerCloneAllToDefaultIfSet(const META_NS::IMetadata & in,META_NS::IMetadata & out)38 inline void SerCloneAllToDefaultIfSet(const META_NS::IMetadata& in, META_NS::IMetadata& out)
39 {
40 if (auto m = interface_cast<META_NS::IObject>(&in)) {
41 AddObjectProperties(*m, out);
42 }
43 }
44
SerCopy(const META_NS::IMetadata & in,META_NS::IMetadata & out)45 inline void SerCopy(const META_NS::IMetadata& in, META_NS::IMetadata& out)
46 {
47 if (auto m = interface_cast<META_NS::IObject>(&out)) {
48 AddFlatProperties(in, *m);
49 }
50 }
51
52 constexpr const BASE_NS::string_view ESCAPED_SER_CHARS = "./![]";
53 constexpr const char ESCAPE_SER_CHAR = '\\';
54
EscapeSerName(BASE_NS::string_view str)55 inline BASE_NS::string EscapeSerName(BASE_NS::string_view str)
56 {
57 BASE_NS::string res { str };
58 for (size_t i = 0; i != res.size(); ++i) {
59 if (ESCAPED_SER_CHARS.find(res[i]) != BASE_NS::string_view::npos) {
60 res.insert(i, &ESCAPE_SER_CHAR, 1);
61 ++i;
62 }
63 }
64 return res;
65 }
66
UnescapeSerName(BASE_NS::string_view str)67 inline BASE_NS::string UnescapeSerName(BASE_NS::string_view str)
68 {
69 BASE_NS::string res { str };
70 for (size_t i = 0; i < res.size(); ++i) {
71 if (res[i] == ESCAPE_SER_CHAR) {
72 res.erase(i, 1);
73 }
74 }
75 return res;
76 }
77
78 SCENE_END_NAMESPACE()
79
80 #endif