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_INTERFACE_SERIALIZATION_IEXPORT_CONTEXT_H
17 #define META_INTERFACE_SERIALIZATION_IEXPORT_CONTEXT_H
18
19 #include <core/plugin/intf_interface.h>
20
21 #include <meta/base/interface_macros.h>
22 #include <meta/base/namespace.h>
23 #include <meta/interface/detail/any.h>
24 #include <meta/interface/intf_any.h>
25 #include <meta/interface/serialization/intf_ser_node.h>
26
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28
29 /// Defines functions that can be used when exporting an object
30 class IExportFunctions : public CORE_NS::IInterface {
31 META_INTERFACE(CORE_NS::IInterface, IExportFunctions, "d7d2d7fe-649a-4b39-bc33-995097a396be")
32 public:
33 /// Export given entity to serialization node
34 virtual ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr& out) = 0;
35 ISerNode::Ptr ExportToNode(const IAny& entity)
36 {
37 ISerNode::Ptr node;
38 return ExportToNode(entity, node) ? node : nullptr;
39 }
40 /// Export given value to serialisation node
41 template<typename Type>
42 ReturnError ExportValueToNode(const Type& value, ISerNode::Ptr& out)
43 {
44 return ExportToNode(static_cast<const IAny&>(Any<BASE_NS::remove_const_t<Type>>(value)), out);
45 }
46 template<typename Type>
47 ISerNode::Ptr ExportValueToNode(const Type& value)
48 {
49 ISerNode::Ptr node;
50 return ExportValueToNode(value, node) ? node : nullptr;
51 }
52 };
53
54 /// Interface that used to export objects inside object's export function
55 class IExportContext : public IExportFunctions {
56 META_INTERFACE(IExportFunctions, IExportContext, "a669af8b-58af-4468-ab64-4a38fcc80bf1")
57 public:
58 /// Export given entity with name (exports the value inside the any)
59 virtual ReturnError Export(BASE_NS::string_view name, const IAny& entity) = 0;
60 /// Export given any with name (exports the actual any)
61 virtual ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) = 0;
62 /// Export weak pointer to object with name
63 virtual ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) = 0;
64 /// Export itself as known interfaces, like IMetadata, IContainer, IAttach
65 virtual ReturnError AutoExport() = 0;
66
67 /// Export given value with name
68 template<typename Type>
ExportValue(BASE_NS::string_view name,const Type & value)69 ReturnError ExportValue(BASE_NS::string_view name, const Type& value)
70 {
71 return Export(name, static_cast<const IAny&>(Any<BASE_NS::remove_const_t<Type>>(value)));
72 }
73 /// Export given array of values with name
74 template<typename Type>
ExportValue(BASE_NS::string_view name,const BASE_NS::vector<Type> & value)75 ReturnError ExportValue(BASE_NS::string_view name, const BASE_NS::vector<Type>& value)
76 {
77 return Export(name, static_cast<const IAny&>(ArrayAny<Type>(value)));
78 }
79
80 /// Get the context in which we are exporting, this is the IExporter typically
81 virtual CORE_NS::IInterface* Context() const = 0;
82 /// Get user set context object
83 virtual META_NS::IObject::Ptr UserContext() const = 0;
84 /// Substitute serialisation of the current object with given node (all exports for this object are discarded)
85 virtual ReturnError SubstituteThis(ISerNode::Ptr) = 0;
86 /// Get Metadata
87 virtual SerMetadata GetMetadata() const = 0;
88 };
89
90 META_INTERFACE_TYPE(META_NS::IExportContext)
91
92 META_END_NAMESPACE()
93
94 #endif
95