• 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_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/resource/intf_resource_consumer.h>
26 #include <meta/interface/serialization/intf_export_context.h>
27 #include <meta/interface/serialization/intf_exporter.h>
28 #include <meta/interface/serialization/intf_global_serialization_data.h>
29 
30 #include "base_object.h"
31 #include "ser_nodes.h"
32 
META_BEGIN_NAMESPACE()33 META_BEGIN_NAMESPACE()
34 namespace Serialization {
35 
36 constexpr Version VERSION { 2, 0 };
37 constexpr Version EXPORTER_VERSION { 1, 0 };
38 
39 class Exporter : public IntroduceInterfaces<BaseObject, IExporter, IExportFunctions, IResourceConsumer> {
40     META_OBJECT(Exporter, ClassId::Exporter, IntroduceInterfaces)
41 public:
42     Exporter() : registry_(GetObjectRegistry()), globalData_(registry_.GetGlobalSerializationData()) {}
43     explicit Exporter(IObjectRegistry& reg, IGlobalSerializationData& data) : registry_(reg), globalData_(data) {}
44 
45     ISerNode::Ptr Export(const IObject::ConstPtr& object) override;
46     void SetInstanceIdMapping(BASE_NS::unordered_map<InstanceId, InstanceId>) override;
47     void SetResourceManager(CORE_NS::IResourceManager::Ptr p) override;
48     void SetUserContext(IObject::Ptr) override;
49     void SetMetadata(SerMetadata m) override;
50 
51     ReturnError ExportValue(const IAny& entity, ISerNode::Ptr&);
52     ReturnError ExportAny(const IAny::ConstPtr& any, ISerNode::Ptr&);
53     ReturnError ExportWeakPtr(const IObject::ConstWeakPtr& ptr, ISerNode::Ptr&);
54 
55     ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override;
56     ReturnError AutoExportObjectMembers(const IObject::ConstPtr& object, BASE_NS::vector<NamedNode>& members);
57 
58     InstanceId ConvertInstanceId(const InstanceId& id) const;
59 
60     CORE_NS::IResourceManager::Ptr GetResourceManager() const override
61     {
62         return resources_;
63     }
64     META_NS::IObject::Ptr GetUserContext() const
65     {
66         return userContext_;
67     }
68     const SerMetadata& GetMetadata() const
69     {
70         return metadata_;
71     }
72 
73 private:
74     struct DeferredWeakPtrResolve {
75         IRefUriNode::Ptr node;
76         IObject::ConstWeakPtr ptr;
77     };
78 
79     bool ShouldSerialize(const IObject::ConstPtr& object) const;
80     bool ShouldSerialize(const IAny& any) const;
81     bool MarkExported(const IObject::ConstPtr& object);
82     bool HasBeenExported(const InstanceId& id) const;
83 
84     ReturnError ExportObject(const IObject::ConstPtr& object, ISerNode::Ptr&);
85     ReturnError ExportPointer(const IAny& entity, ISerNode::Ptr&);
86     ISerNode::Ptr ExportBuiltinValue(const IAny& value);
87     ISerNode::Ptr ExportArray(const IArrayAny& array);
88 
89     ISerNode::Ptr CreateObjectNode(const IObject::ConstPtr& object, BASE_NS::shared_ptr<MapNode> node);
90     ISerNode::Ptr CreateObjectRefNode(const RefUri& ref);
91     ISerNode::Ptr CreateObjectRefNode(const IObject::ConstPtr& object);
92     ISerNode::Ptr AutoExportObject(const IObject::ConstPtr& object);
93     IObject::Ptr ResolveUriSegment(const IObject::ConstPtr& ptr, RefUri& uri) const;
94     ISerNode::Ptr ExportIContainer(const IContainer& cont);
95     ReturnError ResolveDeferredWeakPtr(const DeferredWeakPtrResolve& d);
96 
97 private:
98     IObjectRegistry& registry_;
99     IGlobalSerializationData& globalData_;
100     BASE_NS::unordered_map<InstanceId, IObject::ConstWeakPtr> exported_;
101     BASE_NS::unordered_map<InstanceId, InstanceId> mapInstanceIds_;
102     BASE_NS::vector<DeferredWeakPtrResolve> deferred_;
103     CORE_NS::IResourceManager::Ptr resources_;
104     IObject::Ptr userContext_;
105     SerMetadata metadata_;
106 };
107 
108 class ExportContext : public IntroduceInterfaces<IExportContext> {
109 public:
110     ExportContext(Exporter& exp, IObject::ConstPtr p) : exporter_(exp), object_(BASE_NS::move(p)) {}
111 
112     BASE_NS::shared_ptr<MapNode> ExtractNode();
113 
114     ReturnError Export(BASE_NS::string_view name, const IAny& entity) override;
115     ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) override;
116     ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) override;
117     ReturnError AutoExport() override;
118 
119     ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override;
120 
121     CORE_NS::IInterface* Context() const override;
122     META_NS::IObject::Ptr UserContext() const override;
123     ReturnError SubstituteThis(ISerNode::Ptr) override;
124     SerMetadata GetMetadata() const override;
125 
126     ISerNode::Ptr GetSubstitution() const
127     {
128         return substNode_;
129     }
130 
131 private:
132     Exporter& exporter_;
133     IObject::ConstPtr object_;
134     BASE_NS::vector<NamedNode> elements_;
135     ISerNode::Ptr substNode_;
136 };
137 
138 } // namespace Serialization
139 META_END_NAMESPACE()
140 
141 #endif
142