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_SRC_SERIALIZATION_SER_NODES_H
17 #define META_SRC_SERIALIZATION_SER_NODES_H
18 
19 #include <base/containers/unordered_map.h>
20 
21 #include <meta/base/ref_uri.h>
22 #include <meta/base/type_traits.h>
23 #include <meta/base/version.h>
24 #include <meta/ext/minimal_object.h>
25 #include <meta/ext/object_factory.h>
26 #include <meta/interface/intf_object_factory.h>
27 #include <meta/interface/serialization/intf_ser_node.h>
28 
29 #include "../base_object.h"
30 
META_BEGIN_NAMESPACE()31 META_BEGIN_NAMESPACE()
32 namespace Serialization {
33 
34 class NilNode : public Internal::BaseObjectFwd<NilNode, ClassId::NilNode, INilNode> {
35 public:
36     NilNode() = default;
37     void Apply(ISerNodeVisitor& v) override
38     {
39         v.Visit(*this);
40     }
41 };
42 
43 class MapNode : public Internal::BaseObjectFwd<MapNode, ClassId::MapNode, IMapNode> {
44 public:
45     MapNode() = default;
46     MapNode(BASE_NS::vector<NamedNode> elements) : elements(BASE_NS::move(elements)) {}
47 
48     BASE_NS::vector<NamedNode> GetMembers() const override
49     {
50         return elements;
51     }
52 
53     ISerNode::Ptr FindNode(BASE_NS::string_view name) const override
54     {
55         for (auto&& v : elements) {
56             if (name == v.name) {
57                 return v.node;
58             }
59         }
60         return nullptr;
61     }
62 
63     void AddNode(BASE_NS::string_view name, ISerNode::Ptr n) override
64     {
65         elements.push_back(NamedNode { BASE_NS::string(name), BASE_NS::move(n) });
66     }
67 
68     void Apply(ISerNodeVisitor& v) override
69     {
70         v.Visit(*this);
71     }
72 
73 public:
74     BASE_NS::vector<NamedNode> elements;
75 };
76 
77 class ArrayNode : public Internal::BaseObjectFwd<ArrayNode, ClassId::ArrayNode, IArrayNode> {
78 public:
79     ArrayNode() = default;
80     ArrayNode(BASE_NS::vector<ISerNode::Ptr> elements) : elements(BASE_NS::move(elements)) {}
81 
82     BASE_NS::vector<ISerNode::Ptr> GetMembers() const override
83     {
84         return elements;
85     }
86 
87     void AddNode(const ISerNode::Ptr& node) override
88     {
89         elements.push_back(node);
90     }
91 
92     void Apply(ISerNodeVisitor& v) override
93     {
94         v.Visit(*this);
95     }
96 
97 public:
98     BASE_NS::vector<ISerNode::Ptr> elements;
99 };
100 
101 class ObjectNode : public Internal::BaseObjectFwd<ObjectNode, ClassId::ObjectNode, IObjectNode> {
102 public:
103     ObjectNode() = default;
104     ObjectNode(BASE_NS::string className, BASE_NS::string name, const ObjectId& oid, const InstanceId& iid,
105         ISerNode::Ptr members)
106         : className(BASE_NS::move(className)), name(BASE_NS::move(name)), objectType(oid), instance(iid),
107           members(BASE_NS::move(members))
108     {}
109 
110     BASE_NS::string GetObjectClassName() const override
111     {
112         return className;
113     }
114     BASE_NS::string GetObjectName() const override
115     {
116         return name;
117     }
118     ObjectId GetObjectId() const override
119     {
120         return objectType;
121     }
122     InstanceId GetInstanceId() const override
123     {
124         return instance;
125     }
126     ISerNode::Ptr GetMembers() const override
127     {
128         return members;
129     }
130 
131     void SetObjectClassName(BASE_NS::string name) override
132     {
133         className = BASE_NS::move(name);
134     }
135     void SetObjectName(BASE_NS::string name) override
136     {
137         className = BASE_NS::move(name);
138     }
139     void SetObjectId(ObjectId id) override
140     {
141         objectType = id;
142     }
143     void SetInstanceId(InstanceId id) override
144     {
145         instance = id;
146     }
147     void SetMembers(ISerNode::Ptr n) override
148     {
149         members = BASE_NS::move(n);
150     }
151 
152     void Apply(ISerNodeVisitor& v) override
153     {
154         v.Visit(*this);
155     }
156 
157 public:
158     BASE_NS::string className;
159     BASE_NS::string name;
160     ObjectId objectType;
161     InstanceId instance;
162     ISerNode::Ptr members;
163 };
164 
165 class RootNode : public Internal::BaseObjectFwd<RootNode, ClassId::RootNode, IRootNode> {
166 public:
167     RootNode() = default;
168     RootNode(const Version& ver, ISerNode::Ptr obj) : serializerVersion_(ver), object(BASE_NS::move(obj)) {}
169 
170     Version GetSerializerVersion() const override
171     {
172         return serializerVersion_;
173     }
174     ISerNode::Ptr GetObject() const override
175     {
176         return object;
177     }
178 
179     void Apply(ISerNodeVisitor& v) override
180     {
181         v.Visit(*this);
182     }
183 
184 public:
185     Version serializerVersion_ { 1, 0 };
186     ISerNode::Ptr object;
187 };
188 
189 template<typename Type, const META_NS::ClassInfo& ClassInfo>
190 class BuiltinValueNode
191     : public Internal::BaseObjectFwd<BuiltinValueNode<Type, ClassInfo>, ClassInfo, IBuiltinValueNode<Type>> {
192 public:
193     using InterfaceType = IBuiltinValueNode<Type>;
194 
195     BuiltinValueNode() = default;
196     BuiltinValueNode(const Type& v) : value(v) {}
197 
198     Type GetValue() const override
199     {
200         return value;
201     }
202 
203     void SetValue(const Type& v) override
204     {
205         value = v;
206     }
207 
208     void Apply(ISerNodeVisitor& v) override
209     {
210         v.Visit(*this);
211     }
212 
213 public:
214     Type value {};
215 };
216 
217 using BoolNode = BuiltinValueNode<bool, ClassId::BoolNode>;
218 using IntNode = BuiltinValueNode<int64_t, ClassId::IntNode>;
219 using UIntNode = BuiltinValueNode<uint64_t, ClassId::UIntNode>;
220 using DoubleNode = BuiltinValueNode<double, ClassId::DoubleNode>;
221 using StringNode = BuiltinValueNode<BASE_NS::string, ClassId::StringNode>;
222 using RefNode = BuiltinValueNode<RefUri, ClassId::RefNode>;
223 
224 template<typename Type, typename Node>
225 struct SupportedType {
226     using NodeType = Node;
227     constexpr const static TypeId ID = UidFromType<Type>();
228 
229     static ISerNode::Ptr CreateNode(const IAny& any)
230     {
231         return ISerNode::Ptr(new NodeType(GetValue<Type>(any)));
232     }
233 
234     static AnyReturnValue ExtractValue(const ISerNode::ConstPtr& n, IAny& any)
235     {
236         Type v {};
237         if (auto node = interface_cast<typename NodeType::InterfaceType>(n)) {
238             v = static_cast<Type>(node->GetValue());
239         } else {
240             if constexpr (BASE_NS::is_same_v<NodeType, IntNode> || BASE_NS::is_same_v<NodeType, DoubleNode>) {
241                 if (auto node = interface_cast<UIntNode::InterfaceType>(n)) {
242                     v = static_cast<Type>(node->GetValue());
243                 } else if (auto node = interface_cast<IntNode::InterfaceType>(n)) {
244                     v = static_cast<Type>(node->GetValue());
245                 }
246             }
247         }
248         return any.SetValue(v);
249     }
250 };
251 
252 // clang-format off
253 using SupportedBuiltins = TypeList<
254     SupportedType<bool, BoolNode>,
255     SupportedType<double, DoubleNode>,
256     SupportedType<uint8_t, UIntNode>,
257     SupportedType<uint16_t, UIntNode>,
258     SupportedType<uint32_t, UIntNode>,
259     SupportedType<uint64_t, UIntNode>,
260     SupportedType<int8_t, IntNode>,
261     SupportedType<int16_t, IntNode>,
262     SupportedType<int32_t, IntNode>,
263     SupportedType<int64_t, IntNode>,
264     SupportedType<BASE_NS::string, StringNode>,
265     SupportedType<RefUri, RefNode>
266     >;
267 // clang-format on
268 
269 } // namespace Serialization
270 META_END_NAMESPACE()
271 
272 #endif
273