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