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 META_EXT_SERIALIZATION_COMMON_VALUE_SERIALIZERS_H
17 #define META_EXT_SERIALIZATION_COMMON_VALUE_SERIALIZERS_H
18
19 #include <meta/ext/serialization/value_serializer.h>
20
META_BEGIN_NAMESPACE()21 META_BEGIN_NAMESPACE()
22
23 template<typename Value>
24 ISerNode::Ptr EnumExport(IExportFunctions& f, const Value& v)
25 {
26 using Type = BASE_NS::underlying_type_t<BASE_NS::remove_const_t<BASE_NS::remove_reference_t<decltype(v)>>>;
27 return f.ExportToNode(Any<Type>(static_cast<Type>(v)));
28 }
29
30 template<typename Value>
EnumImport(IImportFunctions & f,const ISerNode::ConstPtr & node,Value & out)31 bool EnumImport(IImportFunctions& f, const ISerNode::ConstPtr& node, Value& out)
32 {
33 using Plain = BASE_NS::remove_const_t<BASE_NS::remove_reference_t<decltype(out)>>;
34 using Type = BASE_NS::underlying_type_t<Plain>;
35 Any<Type> any;
36 bool res = f.ImportFromNode(node, any);
37 if (res) {
38 out = static_cast<Plain>(any.InternalGetValue());
39 }
40 return res;
41 }
42
43 template<typename Type>
ExtractInteger(const ISerNode::ConstPtr & node,Type & out)44 bool ExtractInteger(const ISerNode::ConstPtr& node, Type& out)
45 {
46 if (auto n = interface_cast<IIntNode>(node)) {
47 out = static_cast<Type>(n->GetValue());
48 return true;
49 }
50 if (auto n = interface_cast<IUIntNode>(node)) {
51 out = static_cast<Type>(n->GetValue());
52 return true;
53 }
54 return false;
55 }
56
57 template<typename Type>
ExtractNumber(const ISerNode::ConstPtr & node,Type & out)58 bool ExtractNumber(const ISerNode::ConstPtr& node, Type& out)
59 {
60 if (ExtractInteger(node, out)) {
61 return true;
62 }
63 if (auto n = interface_cast<IDoubleNode>(node)) {
64 out = static_cast<Type>(n->GetValue());
65 return true;
66 }
67 if (auto n = interface_cast<IBoolNode>(node)) {
68 out = static_cast<Type>(n->GetValue());
69 return true;
70 }
71 return false;
72 }
73
CreateObjectNode(ObjectId oid,BASE_NS::string className)74 inline IObjectNode::Ptr CreateObjectNode(ObjectId oid, BASE_NS::string className)
75 {
76 auto obj = GetObjectRegistry().Create<IObjectNode>(META_NS::ClassId::ObjectNode);
77 if (obj) {
78 obj->SetObjectId(oid);
79 obj->SetObjectClassName(BASE_NS::move(className));
80 }
81 return obj;
82 }
83
84 template<typename Type>
ImportFromNode(IImportFunctions & f,const ISerNode::ConstPtr & node,Type & value)85 ReturnError ImportFromNode(IImportFunctions& f, const ISerNode::ConstPtr& node, Type& value)
86 {
87 Any<Type> any;
88 auto res = f.ImportFromNode(node, any);
89 if (res) {
90 value = any.InternalGetValue();
91 }
92 return res;
93 }
94
95 META_END_NAMESPACE()
96
97 #endif
98