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_IIMPORT_CONTEXT_H
17 #define META_INTERFACE_SERIALIZATION_IIMPORT_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
META_BEGIN_NAMESPACE()26 META_BEGIN_NAMESPACE()
27
28 class IImportFunctions : public CORE_NS::IInterface {
29 META_INTERFACE(CORE_NS::IInterface, IImportFunctions, "b5415715-8eb7-4106-ab1e-2dfad7134936")
30 public:
31 virtual IObject::Ptr ResolveRefUri(const RefUri& uri) = 0;
32 virtual ReturnError ImportFromNode(const ISerNode::ConstPtr&, IAny& entity) = 0;
33 };
34
35 class IImportContext : public IImportFunctions {
36 META_INTERFACE(IImportFunctions, IImportContext, "21dd3f27-d987-4ffd-93de-8d8c6d0585ef")
37 public:
38 virtual bool HasMember(BASE_NS::string_view name) const = 0;
39 virtual ReturnError Import(BASE_NS::string_view name, IAny& entity) = 0;
40 virtual ReturnError ImportAny(BASE_NS::string_view name, IAny::Ptr& any) = 0;
41 virtual ReturnError ImportWeakPtr(BASE_NS::string_view name, IObject::WeakPtr& ptr) = 0;
42 virtual ReturnError AutoImport() = 0;
43
44 template<typename Type>
ImportValue(BASE_NS::string_view name,Type & value)45 ReturnError ImportValue(BASE_NS::string_view name, Type& value)
46 {
47 Any<Type> v(value);
48 auto r = Import(name, static_cast<IAny&>(v));
49 if (r) {
50 value = v.InternalGetValue();
51 }
52 return r;
53 }
54
55 template<typename Type>
ImportValue(BASE_NS::string_view name,BASE_NS::vector<Type> & value)56 ReturnError ImportValue(BASE_NS::string_view name, BASE_NS::vector<Type>& value)
57 {
58 ArrayAny<Type> v(value);
59 auto r = Import(name, static_cast<IAny&>(v));
60 if (r) {
61 value = v.InternalGetValue();
62 }
63 return r;
64 }
65 };
66
67 META_END_NAMESPACE()
68
69 #endif
70