• 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 API_CORE_RESOURCES_IRESOURCE_H
17 #define API_CORE_RESOURCES_IRESOURCE_H
18 
19 #include <base/containers/shared_ptr.h>
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <core/io/intf_file.h>
23 #include <core/plugin/intf_interface.h>
24 
25 CORE_BEGIN_NAMESPACE()
26 
27 /**
28  * @brief Type to identify resource
29  */
30 struct ResourceId {
31     BASE_NS::string name;
32     BASE_NS::string group;
33 
34     ResourceId() = default;
ResourceIdResourceId35     ResourceId(const char* name) : name(name) {}
ResourceIdResourceId36     ResourceId(BASE_NS::string name) : name(BASE_NS::move(name)) {}
ResourceIdResourceId37     ResourceId(BASE_NS::string name, BASE_NS::string group) : name(BASE_NS::move(name)), group(BASE_NS::move(group)) {}
38 
IsValidResourceId39     bool IsValid() const
40     {
41         return !name.empty();
42     }
43 
ToStringResourceId44     BASE_NS::string ToString() const
45     {
46         if (group.empty()) {
47             return name;
48         }
49         return group + "::" + name;
50     }
51 
52     bool operator==(const ResourceId& id) const
53     {
54         return name == id.name && group == id.group;
55     }
56     bool operator!=(const ResourceId& id) const
57     {
58         return !(*this == id);
59     }
60 };
61 using ResourceType = BASE_NS::Uid;
62 
63 /**
64  * @brief Type that is used to select resource ids
65  */
66 struct MatchingResourceId {
67     /// Match all names and groups
MatchingResourceIdMatchingResourceId68     MatchingResourceId() : hasWildCardName(true), hasWildCardGroup(true) {}
69     /// Match all names in given group
MatchingResourceIdMatchingResourceId70     MatchingResourceId(BASE_NS::string group) : hasWildCardName(true), group(BASE_NS::move(group)) {}
MatchingResourceIdMatchingResourceId71     MatchingResourceId(BASE_NS::string_view group) : hasWildCardName(true), group(group) {}
72     /// Match specific resource
MatchingResourceIdMatchingResourceId73     MatchingResourceId(const ResourceId& id) : name(id.name), group(id.group) {}
74 
75     bool hasWildCardName {};
76     BASE_NS::string name;
77     bool hasWildCardGroup {};
78     BASE_NS::string group;
79 };
80 
81 /**
82  * @brief Base class for resources
83  */
84 class IResource : public IInterface {
85 public:
86     using base = IInterface;
87     static constexpr BASE_NS::Uid UID { "597e8395-4990-492c-9cac-ad32804c0dfd" };
88     using Ptr = BASE_NS::shared_ptr<IResource>;
89     using ConstPtr = BASE_NS::shared_ptr<const IResource>;
90     using WeakPtr = BASE_NS::weak_ptr<IResource>;
91     using ConstWeakPtr = BASE_NS::weak_ptr<const IResource>;
92 
93     /** Get the resource's type UID.
94      * @return Type UID.
95      */
96     virtual ResourceType GetResourceType() const = 0;
97 
98     /** Get the resource's id. A resource can be queried from the resource manager with the same id.
99      * @return Resource id.
100      */
101     virtual ResourceId GetResourceId() const = 0;
102 
103 protected:
104     IResource() = default;
105     virtual ~IResource() = default;
106     IResource(const IResource&) = delete;
107     void operator=(const IResource&) = delete;
108 };
109 
110 /**
111  * @brief Interface to set resource id for existing resource object
112  */
113 class ISetResourceId : public IInterface {
114 public:
115     using base = IInterface;
116     static constexpr BASE_NS::Uid UID { "1d2aa2c6-7a08-49bf-a360-aecf201a8681" };
117     using Ptr = BASE_NS::shared_ptr<ISetResourceId>;
118     using ConstPtr = BASE_NS::shared_ptr<const ISetResourceId>;
119     using WeakPtr = BASE_NS::weak_ptr<ISetResourceId>;
120     using ConstWeakPtr = BASE_NS::weak_ptr<const ISetResourceId>;
121 
122     virtual void SetResourceId(CORE_NS::ResourceId) = 0;
123 
124 protected:
125     ISetResourceId() = default;
126     virtual ~ISetResourceId() = default;
127     ISetResourceId(const ISetResourceId&) = delete;
128     void operator=(const ISetResourceId&) = delete;
129 };
130 
131 class IResourceManager;
132 using ResourceContextPtr = BASE_NS::shared_ptr<IInterface>;
133 using ResourceManagerPtr = BASE_NS::shared_ptr<IResourceManager>;
134 
135 /**
136  * @brief Base class for options that can be used to construct resources
137  */
138 class IResourceOptions : public IInterface {
139 public:
140     using base = IInterface;
141     static constexpr BASE_NS::Uid UID { "c523171f-c6b8-40a2-939a-eb10672bb87c" };
142     using Ptr = BASE_NS::shared_ptr<IResourceOptions>;
143     using ConstPtr = BASE_NS::shared_ptr<const IResourceOptions>;
144     using WeakPtr = BASE_NS::weak_ptr<IResourceOptions>;
145     using ConstWeakPtr = BASE_NS::weak_ptr<const IResourceOptions>;
146 
147     virtual bool Load(IFile& options, const ResourceManagerPtr&, const ResourceContextPtr&) = 0;
148     virtual bool Save(IFile& options, const ResourceManagerPtr&) const = 0;
149 
150 protected:
151     IResourceOptions() = default;
152     virtual ~IResourceOptions() = default;
153     IResourceOptions(const IResourceOptions&) = delete;
154     void operator=(const IResourceOptions&) = delete;
155 };
156 
CORE_END_NAMESPACE()157 CORE_END_NAMESPACE()
158 
159 template<>
160 inline uint64_t BASE_NS::hash(const CORE_NS::ResourceId& id)
161 {
162     return BASE_NS::Hash(id.name, id.group);
163 }
164 
165 #endif // API_CORE_RESOURCES_IRESOURCE_H
166