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 };
39 
40 class IExportContext : public IExportFunctions {
41     META_INTERFACE(IExportFunctions, IExportContext, "a669af8b-58af-4468-ab64-4a38fcc80bf1")
42 public:
43     virtual ReturnError Export(BASE_NS::string_view name, const IAny& entity) = 0;
44     virtual ReturnError ExportAny(BASE_NS::string_view name, const IAny::Ptr& any) = 0;
45     virtual ReturnError ExportWeakPtr(BASE_NS::string_view name, const IObject::ConstWeakPtr& ptr) = 0;
46     // Export itself as known interfaces, like IMetadata, IContainer, IAttach
47     virtual ReturnError AutoExport() = 0;
48 
49     template<typename Type>
ExportValue(BASE_NS::string_view name,const Type & value)50     ReturnError ExportValue(BASE_NS::string_view name, const Type& value)
51     {
52         return Export(name, static_cast<const IAny&>(Any<Type>(value)));
53     }
54 
55     template<typename Type>
ExportValue(BASE_NS::string_view name,const BASE_NS::vector<Type> & value)56     ReturnError ExportValue(BASE_NS::string_view name, const BASE_NS::vector<Type>& value)
57     {
58         return Export(name, static_cast<const IAny&>(ArrayAny<Type>(value)));
59     }
60 };
61 
62 META_INTERFACE_TYPE(IExportContext);
63 
64 META_END_NAMESPACE()
65 
66 #endif
67