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 class IExportFunctions : public CORE_NS::IInterface {
30 META_INTERFACE(CORE_NS::IInterface, IExportFunctions, "d7d2d7fe-649a-4b39-bc33-995097a396be")
31 public:
32 virtual ReturnError ExportToNode(const IAny& entity, ISerNode::Ptr& out) = 0;
33 ISerNode::Ptr ExportToNode(const IAny& entity)
34 {
35 ISerNode::Ptr node;
36 return ExportToNode(entity, node) ? node : nullptr;
37 }
38 template<typename Type>
39 ReturnError ExportValueToNode(const Type& value, ISerNode::Ptr& out)
40 {
41 return ExportToNode(static_cast<const IAny&>(Any<BASE_NS::remove_const_t<Type>>(value)), out);
42 }
43 };
44
45 class IExportContext : public IExportFunctions {
46 META_INTERFACE(IExportFunctions, IExportContext, "a669af8b-58af-4468-ab64-4a38fcc80bf1")
47 public:
48 virtual ReturnError Export(BASE_NS::string_view name, const IAny& entity) = 0;
49 virtual ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) = 0;
50 virtual ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) = 0;
51 // Export itself as known interfaces, like IMetadata, IContainer, IAttach
52 virtual ReturnError AutoExport() = 0;
53
54 template<typename Type>
ExportValue(BASE_NS::string_view name,const Type & value)55 ReturnError ExportValue(BASE_NS::string_view name, const Type& value)
56 {
57 return Export(name, static_cast<const IAny&>(Any<BASE_NS::remove_const_t<Type>>(value)));
58 }
59
60 template<typename Type>
ExportValue(BASE_NS::string_view name,const BASE_NS::vector<Type> & value)61 ReturnError ExportValue(BASE_NS::string_view name, const BASE_NS::vector<Type>& value)
62 {
63 return Export(name, static_cast<const IAny&>(ArrayAny<Type>(value)));
64 }
65 };
66
67 META_INTERFACE_TYPE(META_NS::IExportContext)
68
69 META_END_NAMESPACE()
70
71 #endif
72