• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RESOURCE_RESOURCE_PLACEHOLDER_H
17 #define META_SRC_RESOURCE_RESOURCE_PLACEHOLDER_H
18 
19 #include <meta/ext/implementation_macros.h>
20 #include <meta/ext/minimal_object.h>
21 #include <meta/ext/object.h>
22 #include <meta/ext/serialization/serializer.h>
23 #include <meta/interface/resource/intf_resource.h>
24 #include <meta/interface/resource/intf_resource_consumer.h>
25 #include <meta/interface/serialization/intf_serializable.h>
26 
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28 
29 class ResourcePlaceholder : public IntroduceInterfaces<BaseObject, ISerializable, CORE_NS::ISetResourceId> {
30     META_OBJECT(ResourcePlaceholder, ClassId::ResourcePlaceholder, IntroduceInterfaces)
31 public:
32     explicit ResourcePlaceholder(CORE_NS::ResourceId id = {}) : rid_(BASE_NS::move(id)) {}
33 
34     ReturnError Export(IExportContext& c) const override
35     {
36         return Serializer(c) & NamedValue("resourceId.name", rid_.name) & NamedValue("resourceId.group", rid_.group);
37     }
38     ReturnError Import(IImportContext& c) override
39     {
40         Serializer s(c);
41         if (s & NamedValue("resourceId.name", rid_.name) & NamedValue("resourceId.group", rid_.group)) {
42             if (auto ri = interface_cast<IResourceConsumer>(c.Context())) {
43                 if (auto rman = ri->GetResourceManager()) {
44                     if (auto res = rman->GetResource(rid_, c.UserContext())) {
45                         return c.SubstituteThis(interface_pointer_cast<IObject>(res));
46                     }
47                 } else {
48                     CORE_LOG_W("No resource manager set when importing [%s]", rid_.ToString().c_str());
49                 }
50             }
51         }
52         CORE_LOG_W("Failed to import resource");
53         return GenericError::FAIL;
54     }
55     void SetResourceId(CORE_NS::ResourceId id) override
56     {
57         rid_ = BASE_NS::move(id);
58     }
59 
60 private:
61     CORE_NS::ResourceId rid_;
62 };
63 
64 META_END_NAMESPACE()
65 
66 #endif
67