• 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_API_RESOURCE_DERIVED_FROM_RESOURCE_H
17 #define META_API_RESOURCE_DERIVED_FROM_RESOURCE_H
18 
19 #include <mutex>
20 #include <shared_mutex>
21 
22 #include <meta/api/metadata_util.h>
23 #include <meta/ext/implementation_macros.h>
24 #include <meta/interface/resource/intf_object_resource.h>
25 #include <meta/interface/resource/intf_resource.h>
26 
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28 
29 class DerivedFromResource : public IDerivedFromResource {
30 public:
31     explicit DerivedFromResource(const ObjectId& id = {}) : resourceType_(id) {}
32 
33     bool SetResource(const CORE_NS::IResource::Ptr& p) override
34     {
35         auto ores = interface_cast<IObjectResource>(p);
36         if (!ores || p->GetResourceType() != resourceType_.ToUid()) {
37             CORE_LOG_W("Invalid resource type");
38             return false;
39         }
40         if (auto resm = interface_cast<META_NS::IMetadata>(p)) {
41             if (auto m = interface_cast<META_NS::IMetadata>(this)) {
42                 for (auto&& p : resm->GetProperties()) {
43                     CopyToDefaultAndReset(p, *m);
44                 }
45             }
46         }
47         std::unique_lock lock { mutex_ };
48         resource_ = p->GetResourceId();
49         return true;
50     }
51     CORE_NS::ResourceId GetResource() const override
52     {
53         std::shared_lock lock { mutex_ };
54         return resource_;
55     }
56 
57     CORE_NS::IResource::Ptr CreateResource() const override
58     {
59         auto res = GetObjectRegistry().Create<CORE_NS::IResource>(META_NS::ClassId::ObjectResource);
60         if (auto ores = interface_cast<IObjectResource>(res)) {
61             ores->SetResourceType(resourceType_);
62             if (auto i = interface_cast<IMetadata>(res)) {
63                 if (auto m = interface_cast<IMetadata>(this)) {
64                     CloneToDefault(*m, *i);
65                 }
66             }
67         }
68         return res;
69     }
70 
71     void SetResourceType(const ObjectId& id)
72     {
73         resourceType_ = id;
74     }
75 
76 protected:
77     mutable std::shared_mutex mutex_;
78     CORE_NS::ResourceId resource_;
79     ObjectId resourceType_;
80 };
81 
CreateObjectResource(const META_NS::ObjectId & instance,const META_NS::ObjectId & subid)82 inline CORE_NS::IResource::Ptr CreateObjectResource(const META_NS::ObjectId& instance, const META_NS::ObjectId& subid)
83 {
84     auto res = META_NS::GetObjectRegistry().Create<CORE_NS::IResource>(META_NS::ClassId::ObjectResource);
85     if (auto ores = interface_cast<META_NS::IObjectResource>(res)) {
86         ores->SetResourceType(subid);
87         if (auto i = interface_cast<META_NS::IMetadata>(res)) {
88             META_NS::CreatePropertiesFromStaticMeta(instance, *i, true);
89         }
90     }
91     return res;
92 }
93 
94 META_END_NAMESPACE()
95 
96 #endif
97