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_EXPORTER_H
17 #define META_SRC_SERIALIZATION_EXPORTER_H
18
19 #include <base/containers/unordered_map.h>
20
21 #include <meta/ext/minimal_object.h>
22 #include <meta/interface/intf_container.h>
23 #include <meta/interface/intf_metadata.h>
24 #include <meta/interface/intf_object_registry.h>
25 #include <meta/interface/serialization/intf_export_context.h>
26 #include <meta/interface/serialization/intf_exporter.h>
27 #include <meta/interface/serialization/intf_global_serialization_data.h>
28
29 #include "../base_object.h"
30 #include "ser_nodes.h"
31
META_BEGIN_NAMESPACE()32 META_BEGIN_NAMESPACE()
33 namespace Serialization {
34
35 constexpr Version VERSION { 2, 0 };
36 constexpr Version EXPORTER_VERSION { 1, 0 };
37
38 class Exporter : public IntroduceInterfaces<BaseObject, IExporter, IExportFunctions> {
39 META_OBJECT(Exporter, ClassId::Exporter, IntroduceInterfaces)
40 public:
41 Exporter() : registry_(GetObjectRegistry()), globalData_(registry_.GetGlobalSerializationData()) {}
42 explicit Exporter(IObjectRegistry& reg, IGlobalSerializationData& data) : registry_(reg), globalData_(data) {}
43
44 ISerNode::Ptr Export(const IObject::ConstPtr& object) override;
45
46 ReturnError ExportValue(const IAny& entity, ISerNode::Ptr&);
47 ReturnError ExportAny(const IAny::ConstPtr& any, ISerNode::Ptr&);
48 ReturnError ExportWeakPtr(const IObject::ConstWeakPtr& ptr, ISerNode::Ptr&);
49
50 ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override;
51 ReturnError AutoExportObjectMembers(const IObject::ConstPtr& object, BASE_NS::vector<NamedNode>& members);
52
53 private:
54 struct DeferredWeakPtrResolve {
55 IRefUriNode::Ptr node;
56 IObject::ConstWeakPtr ptr;
57 };
58
59 bool ShouldSerialize(const IObject::ConstPtr& object) const;
60 bool ShouldSerialize(const IAny& any) const;
61 InstanceId ConvertInstanceId(const InstanceId& id) const;
62 bool MarkExported(const IObject::ConstPtr& object);
63 bool HasBeenExported(const InstanceId& id) const;
64
65 ReturnError ExportObject(const IObject::ConstPtr& object, ISerNode::Ptr&);
66 ReturnError ExportPointer(const IAny& entity, ISerNode::Ptr&);
67 ISerNode::Ptr ExportBuiltinValue(const IAny& value);
68 ISerNode::Ptr ExportArray(const IArrayAny& array);
69
70 ISerNode::Ptr CreateObjectNode(const IObject::ConstPtr& object, BASE_NS::shared_ptr<MapNode> node);
71 ISerNode::Ptr CreateObjectRefNode(const RefUri& ref);
72 ISerNode::Ptr CreateObjectRefNode(const IObject::ConstPtr& object);
73 ISerNode::Ptr AutoExportObject(const IObject::ConstPtr& object);
74 IObject::Ptr ResolveUriSegment(const IObject::ConstPtr& ptr, RefUri& uri) const;
75 ISerNode::Ptr ExportIContainer(const IContainer& cont);
76 ReturnError ResolveDeferredWeakPtr(const DeferredWeakPtrResolve& d);
77
78 private:
79 IObjectRegistry& registry_;
80 IGlobalSerializationData& globalData_;
81 BASE_NS::unordered_map<InstanceId, IObject::ConstWeakPtr> exported_;
82 BASE_NS::unordered_map<InstanceId, InstanceId> mapInstanceIds_;
83 BASE_NS::vector<DeferredWeakPtrResolve> deferred_;
84 };
85
86 class ExportContext : public IntroduceInterfaces<IExportContext> {
87 public:
88 ExportContext(Exporter& exp, const IObject::ConstPtr& p) : exporter_(exp), object_(p) {}
89
90 BASE_NS::shared_ptr<MapNode> ExtractNode();
91
92 ReturnError Export(BASE_NS::string_view name, const IAny& entity) override;
93 ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) override;
94 ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) override;
95 ReturnError AutoExport() override;
96
97 ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override;
98
99 private:
100 Exporter& exporter_;
101 IObject::ConstPtr object_;
102 BASE_NS::vector<NamedNode> elements_;
103 };
104
105 } // namespace Serialization
106 META_END_NAMESPACE()
107
108 #endif
109