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 /// Export enum using its underlying type
24 template<typename Value>
25 ISerNode::Ptr EnumExport(IExportFunctions& f, const Value& v)
26 {
27 using Type = BASE_NS::underlying_type_t<BASE_NS::remove_const_t<BASE_NS::remove_reference_t<decltype(v)>>>;
28 return f.ExportToNode(Any<Type>(static_cast<Type>(v)));
29 }
30
31 /// Import enum using its underlying type
32 template<typename Value>
EnumImport(IImportFunctions & f,const ISerNode::ConstPtr & node,Value & out)33 bool EnumImport(IImportFunctions& f, const ISerNode::ConstPtr& node, Value& out)
34 {
35 using Plain = BASE_NS::remove_const_t<BASE_NS::remove_reference_t<decltype(out)>>;
36 using Type = BASE_NS::underlying_type_t<Plain>;
37 Any<Type> any;
38 bool res = f.ImportFromNode(node, any);
39 if (res) {
40 out = static_cast<Plain>(any.InternalGetValue());
41 }
42 return res;
43 }
44
45 /// Get value from integer or unsigned integer nodes as given type
46 template<typename Type>
ExtractInteger(const ISerNode::ConstPtr & node,Type & out)47 bool ExtractInteger(const ISerNode::ConstPtr& node, Type& out)
48 {
49 if (auto n = interface_cast<IIntNode>(node)) {
50 out = static_cast<Type>(n->GetValue());
51 return true;
52 }
53 if (auto n = interface_cast<IUIntNode>(node)) {
54 out = static_cast<Type>(n->GetValue());
55 return true;
56 }
57 return false;
58 }
59
60 /// Get value from integers, floating point or boolean nodes as given type
61 template<typename Type>
ExtractNumber(const ISerNode::ConstPtr & node,Type & out)62 bool ExtractNumber(const ISerNode::ConstPtr& node, Type& out)
63 {
64 if (ExtractInteger(node, out)) {
65 return true;
66 }
67 if (auto n = interface_cast<IDoubleNode>(node)) {
68 out = static_cast<Type>(n->GetValue());
69 return true;
70 }
71 if (auto n = interface_cast<IBoolNode>(node)) {
72 out = static_cast<Type>(n->GetValue());
73 return true;
74 }
75 return false;
76 }
77
78 /// Create empty object node with object id and class name
CreateObjectNode(ObjectId oid,BASE_NS::string className)79 inline IObjectNode::Ptr CreateObjectNode(ObjectId oid, BASE_NS::string className)
80 {
81 auto obj = GetObjectRegistry().Create<IObjectNode>(META_NS::ClassId::ObjectNode);
82 if (obj) {
83 obj->SetObjectId(oid);
84 obj->SetObjectClassName(BASE_NS::move(className));
85 }
86 return obj;
87 }
88
89 META_END_NAMESPACE()
90
91 #endif
92