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 32 META_BEGIN_NAMESPACE() 33 namespace Serialization { 34 35 constexpr Version EXPORTER_VERSION { 1, 0 }; 36 37 class Exporter : public Internal::BaseObjectFwd<Exporter, ClassId::Exporter, IExporter, IExportFunctions> { 38 public: Exporter()39 Exporter() : registry_(GetObjectRegistry()), globalData_(registry_.GetGlobalSerializationData()) {} Exporter(IObjectRegistry& reg, IGlobalSerializationData& data)40 explicit Exporter(IObjectRegistry& reg, IGlobalSerializationData& data) : registry_(reg), globalData_(data) {} 41 42 ISerNode::Ptr Export(const IObject::ConstPtr& object) override; 43 44 ReturnError ExportValue(const IAny& entity, ISerNode::Ptr&); 45 ReturnError ExportAny(const IAny::ConstPtr& any, ISerNode::Ptr&); 46 ReturnError ExportWeakPtr(const IObject::ConstWeakPtr& ptr, ISerNode::Ptr&); 47 48 ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override; 49 ReturnError AutoExportObjectMembers(const IObject::ConstPtr& object, BASE_NS::vector<NamedNode>& members); 50 51 private: 52 bool ShouldSerialize(const IObject::ConstPtr& object) const; 53 bool ShouldSerialize(const IAny& any) const; 54 InstanceId ConvertInstanceId(const InstanceId& id) const; 55 bool MarkExported(const IObject::ConstPtr& object); 56 bool HasBeenExported(const InstanceId& id) const; 57 58 ReturnError ExportObject(const IObject::ConstPtr& object, ISerNode::Ptr&); 59 ReturnError ExportPointer(const IAny& entity, ISerNode::Ptr&); 60 ISerNode::Ptr ExportBuiltinValue(const IAny& value); 61 ISerNode::Ptr ExportArray(const IArrayAny& array); 62 63 ISerNode::Ptr CreateObjectNode(const IObject::ConstPtr& object, BASE_NS::shared_ptr<MapNode> node); 64 ISerNode::Ptr CreateObjectRefNode(const RefUri& ref); 65 ISerNode::Ptr CreateObjectRefNode(const IObject::ConstPtr& object); 66 ISerNode::Ptr AutoExportObject(const IObject::ConstPtr& object); 67 IObject::Ptr ResolveUriSegment(const IObject::ConstPtr& ptr, RefUri& uri) const; 68 69 BASE_NS::vector<NamedNode> ExportIMetadata(const IMetadata& data); 70 ISerNode::Ptr ExportIContainer(const IContainer& cont); 71 72 private: 73 IObjectRegistry& registry_; 74 IGlobalSerializationData& globalData_; 75 BASE_NS::unordered_map<InstanceId, IObject::ConstWeakPtr> exported_; 76 BASE_NS::unordered_map<InstanceId, InstanceId> mapInstanceIds_; 77 }; 78 79 class ExportContext : public IntroduceInterfaces<IExportContext> { 80 public: ExportContext(Exporter& exp, const IObject::ConstPtr& p)81 ExportContext(Exporter& exp, const IObject::ConstPtr& p) : exporter_(exp), object_(p) {} 82 83 BASE_NS::shared_ptr<MapNode> ExtractNode(); 84 85 ReturnError Export(BASE_NS::string_view name, const IAny& entity) override; 86 ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) override; 87 ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) override; 88 ReturnError AutoExport() override; 89 90 ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr&) override; 91 92 private: 93 Exporter& exporter_; 94 IObject::ConstPtr object_; 95 BASE_NS::vector<NamedNode> elements_; 96 }; 97 98 } // namespace Serialization 99 META_END_NAMESPACE() 100 101 #endif 102