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_EXT_RESOURCE_RESOURCE_H 17 #define META_EXT_RESOURCE_RESOURCE_H 18 19 #include <meta/ext/implementation_macros.h> 20 #include <meta/ext/object.h> 21 #include <meta/ext/serialization/serializer.h> 22 #include <meta/interface/interface_helpers.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()27META_BEGIN_NAMESPACE() 28 29 class Resource : public IntroduceInterfaces<CORE_NS::IResource, ISerializable, CORE_NS::ISetResourceId> { 30 public: 31 bool SerialiseAsResourceId(IExportContext& c) const 32 { 33 auto ri = interface_cast<IResourceConsumer>(c.Context()); 34 return ri && ri->GetResourceManager(); 35 } 36 ReturnError ExportResourceId(IExportContext& c) const 37 { 38 ReturnError res = GenericError::FAIL; 39 if (resourceId_.IsValid()) { 40 if (auto ph = GetObjectRegistry().Create<CORE_NS::ISetResourceId>(ClassId::ResourcePlaceholder)) { 41 ph->SetResourceId(resourceId_); 42 ISerNode::Ptr node; 43 res = c.ExportValueToNode(interface_pointer_cast<IObject>(ph), node); 44 if (res) { 45 return c.SubstituteThis(node); 46 } 47 } 48 } else { 49 CORE_LOG_E("Empty resource id when serialising resource"); 50 } 51 return res; 52 } 53 ReturnError Export(IExportContext& c) const override 54 { 55 if (SerialiseAsResourceId(c)) { 56 return ExportResourceId(c); 57 } 58 return Serializer(c) & AutoSerialize(); 59 } 60 ReturnError Import(IImportContext& c) override 61 { 62 return Serializer(c) & AutoSerialize(); 63 } 64 CORE_NS::ResourceId GetResourceId() const override 65 { 66 return resourceId_; 67 } 68 69 void SetResourceId(CORE_NS::ResourceId id) override 70 { 71 resourceId_ = BASE_NS::move(id); 72 } 73 74 private: 75 CORE_NS::ResourceId resourceId_; 76 }; 77 78 META_END_NAMESPACE() 79 80 #endif 81