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_IOBJECT_H
17 #define META_INTERFACE_IOBJECT_H
18
19 #include <core/log.h>
20
21 #include <meta/base/bit_field.h>
22 #include <meta/base/ids.h>
23 #include <meta/base/interface_macros.h>
24 #include <meta/base/interface_traits.h>
25 #include <meta/base/meta_types.h>
26 #include <meta/base/namespace.h>
27 #include <meta/base/types.h>
28
29 META_BEGIN_NAMESPACE()
30
31 class IObjectRegistry;
32 class RefUri;
33 META_REGISTER_INTERFACE(IObject, "50c06aa7-0db7-435b-9c6f-c15cac8ef5c7")
34
35 /**
36 * @brief The IObject interface defines an object that can contain bindable properties and functions
37 * within meta object library scope. Such objects also provide a queryable metadata which can
38 * be used for enumerating all the properties and functions exposed by a given object.
39 *
40 * @note To implement IObject interface in a custom class, use (Base/Meta)ObjectFwd,
41 * found in meta/ext/object.h.
42 */
43 class IObject : public CORE_NS::IInterface {
44 META_INTERFACE(CORE_NS::IInterface, IObject)
45 public:
46 /**
47 * @brief Returns the UId of the class which implements this interface.
48 */
49 virtual ObjectId GetClassId() const = 0;
50 /**
51 * @brief Returns the name of the class which implements this interface.
52 */
53 virtual BASE_NS::string_view GetClassName() const = 0;
54 /**
55 * @brief Returns the name of this object, as default this is the instance id.
56 */
57 virtual BASE_NS::string GetName() const = 0;
58 /**
59 * @brief Get uids of interfaces this object implements
60 */
61 virtual BASE_NS::vector<BASE_NS::Uid> GetInterfaces() const = 0;
62 /**
63 * @brief Tries to find the last object the uri points to using this object as base.
64 */
65 IObject::Ptr Resolve(const RefUri& uri) const;
66 /**
67 * @brief Get shared ptr to top most self, notice that this might be different object than "this".
68 * Note: Works by default only for objects constructed via ObjectRegistry
69 */
70 IObject::Ptr GetSelf() const;
71 };
72
73 class IObjectInstance : public IObject {
74 META_INTERFACE(IObject, IObjectInstance, "c64b029b-1617-449f-8489-d1dd1a3c3227")
75 public:
76 /**
77 * @brief Return the framework assigned instance UID for the object instance.
78 */
79 virtual InstanceId GetInstanceId() const = 0;
80 /**
81 * @brief Tries to find the last object the uri points to using this object as base.
82 */
83 virtual IObject::Ptr Resolve(const RefUri& uri) const = 0;
84
85 template<typename Interface>
Resolve(const RefUri & uri)86 typename Interface::Ptr Resolve(const RefUri& uri) const
87 {
88 return interface_pointer_cast<Interface>(Resolve(uri));
89 }
90 /**
91 * @brief Get shared ptr to top most self, notice that this might be different object than "this".
92 * Note: Works by default only for objects constructed via ObjectRegistry
93 */
94 virtual IObject::Ptr GetSelf() const = 0;
95
96 template<typename Interface>
GetSelf()97 typename Interface::Ptr GetSelf() const
98 {
99 return interface_pointer_cast<Interface>(GetSelf());
100 }
101 };
102
103 /**
104 * @brief The IResetableObject interface can be implemented by objects whose state can be reset to a default state.
105 * Usually this means that any properties are reset to a default value and the object's state is returned
106 * to an initial state.
107 * @note Depending on the implementation, the default state may be different between calls.
108 */
109 class IResetableObject : public CORE_NS::IInterface {
110 META_INTERFACE(CORE_NS::IInterface, IResetableObject, "1f4954f7-6823-4a9f-b9c7-b5d62b231791")
111 public:
112 /**
113 * @brief Resets the target object to a default state.
114 */
115 virtual void ResetObject() = 0;
116 };
117
Resolve(const RefUri & uri)118 inline IObject::Ptr IObject::Resolve(const RefUri& uri) const
119 {
120 if (auto oi = GetInterface<IObjectInstance>()) {
121 return oi->Resolve(uri);
122 }
123 CORE_LOG_E("Called Resolve for non-IObjectInstance");
124 return nullptr;
125 }
126
GetSelf()127 inline IObject::Ptr IObject::GetSelf() const
128 {
129 if (auto oi = GetInterface<IObjectInstance>()) {
130 return oi->GetSelf();
131 }
132 CORE_LOG_E("Called GetSelf for non-IObjectInstance");
133 return nullptr;
134 }
135
136 template<typename T, typename = BASE_NS::enable_if<IsInterfacePtr_v<T>>>
GetSelf(const T & object)137 IObject::Ptr GetSelf(const T& object)
138 {
139 if (auto o = interface_cast<IObjectInstance>(object)) {
140 return o->GetSelf();
141 }
142 return nullptr;
143 }
144
145 template<typename Intf, typename T, typename = BASE_NS::enable_if<IsInterfacePtr_v<T>>>
GetSelf(const T & object)146 typename Intf::Ptr GetSelf(const T& object)
147 {
148 if (auto o = interface_cast<IObjectInstance>(object)) {
149 return o->template GetSelf<Intf>();
150 }
151 return nullptr;
152 }
153
154 template<typename T, typename = BASE_NS::enable_if<IsInterfacePtr_v<T>>>
Resolve(const T & object,const RefUri & uri)155 IObject::Ptr Resolve(const T& object, const RefUri& uri)
156 {
157 if (auto o = interface_cast<IObjectInstance>(object)) {
158 return o->Resolve(uri);
159 }
160 return nullptr;
161 }
162
163 template<typename Intf, typename T, typename = BASE_NS::enable_if<IsInterfacePtr_v<T>>>
Resolve(const T & object,const RefUri & uri)164 typename Intf::Ptr Resolve(const T& object, const RefUri& uri)
165 {
166 if (auto o = interface_cast<IObjectInstance>(object)) {
167 return o->template Resolve<Intf>(uri);
168 }
169 return nullptr;
170 }
171
172 META_INTERFACE_TYPE(META_NS::IObject)
173 META_INTERFACE_TYPE(META_NS::IObjectInstance)
174
175 META_END_NAMESPACE()
176
177 #endif
178