• 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 name) override
136     {
137         className = BASE_NS::move(name);
138     }
139     void SetObjectName(BASE_NS::string name) override
140     {
141         className = BASE_NS::move(name);
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, const Version& ver, const Version& serVer)
174         : object(BASE_NS::move(obj)), version_(ver), serializerVersion_(serVer)
175     {}
176 
177     Version GetSerializerVersion() const override
178     {
179         return serializerVersion_;
180     }
181     Version GetVersion() const override
182     {
183         return version_;
184     }
185     ISerNode::Ptr GetObject() const override
186     {
187         return object;
188     }
189 
190     void Apply(ISerNodeVisitor& v) override
191     {
192         v.Visit(*this);
193     }
194 
195 public:
196     ISerNode::Ptr object;
197     Version version_ {};
198     Version serializerVersion_ {};
199 };
200 
201 template<typename Type, const META_NS::ClassInfo& ClassInfo>
202 class BuiltinValueNode : public IntroduceInterfaces<BaseObject, IBuiltinValueNode<Type>> {
203     using MyBase = IntroduceInterfaces<BaseObject, IBuiltinValueNode<Type>>;
204     META_OBJECT(BuiltinValueNode, ClassInfo, MyBase)
205 public:
206     using InterfaceType = IBuiltinValueNode<Type>;
207 
208     BuiltinValueNode() = default;
209     BuiltinValueNode(const Type& v) : value(v) {}
210 
211     Type GetValue() const override
212     {
213         return value;
214     }
215 
216     void SetValue(const Type& v) override
217     {
218         value = v;
219     }
220 
221     void Apply(ISerNodeVisitor& v) override
222     {
223         v.Visit(*this);
224     }
225 
226 public:
227     Type value {};
228 };
229 
230 using BoolNode = BuiltinValueNode<bool, ClassId::BoolNode>;
231 using IntNode = BuiltinValueNode<int64_t, ClassId::IntNode>;
232 using UIntNode = BuiltinValueNode<uint64_t, ClassId::UIntNode>;
233 using DoubleNode = BuiltinValueNode<double, ClassId::DoubleNode>;
234 using StringNode = BuiltinValueNode<BASE_NS::string, ClassId::StringNode>;
235 using RefNode = BuiltinValueNode<RefUri, ClassId::RefNode>;
236 
237 template<typename Type, typename Node>
238 struct SupportedType {
239     using NodeType = Node;
240     constexpr const static TypeId ID = UidFromType<Type>();
241 
242     static ISerNode::Ptr CreateNode(const IAny& any)
243     {
244         return ISerNode::Ptr(new NodeType(GetValue<Type>(any)));
245     }
246 
247     static AnyReturnValue ExtractValue(const ISerNode::ConstPtr& n, IAny& any)
248     {
249         Type v {};
250         if (auto node = interface_cast<typename NodeType::InterfaceType>(n)) {
251             v = static_cast<Type>(node->GetValue());
252         } else {
253             if constexpr (BASE_NS::is_same_v<NodeType, IntNode> || BASE_NS::is_same_v<NodeType, DoubleNode>) {
254                 if (auto node = interface_cast<UIntNode::InterfaceType>(n)) {
255                     v = static_cast<Type>(node->GetValue());
256                 } else if (auto node = interface_cast<IntNode::InterfaceType>(n)) {
257                     v = static_cast<Type>(node->GetValue());
258                 }
259             }
260         }
261         return any.SetValue(v);
262     }
263 };
264 
265 // clang-format off
266 using SupportedBuiltins = TypeList<
267     SupportedType<bool, BoolNode>,
268     SupportedType<double, DoubleNode>,
269     SupportedType<uint8_t, UIntNode>,
270     SupportedType<uint16_t, UIntNode>,
271     SupportedType<uint32_t, UIntNode>,
272     SupportedType<uint64_t, UIntNode>,
273     SupportedType<int8_t, IntNode>,
274     SupportedType<int16_t, IntNode>,
275     SupportedType<int32_t, IntNode>,
276     SupportedType<int64_t, IntNode>,
277     SupportedType<BASE_NS::string, StringNode>,
278     SupportedType<RefUri, RefNode>
279     >;
280 // clang-format on
281 
282 } // namespace Serialization
283 META_END_NAMESPACE()
284 
285 #endif
286