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 #include "meta_object.h"
16
17 #include <base/util/uid_util.h>
18 #include <core/plugin/intf_class_factory.h>
19
20 #include <meta/api/iteration.h>
21 #include <meta/interface/builtin_objects.h>
22 #include <meta/interface/intf_object_registry.h>
23 #include <meta/interface/intf_proxy_object.h>
24 #include <meta/interface/property/construct_property.h>
25 #include <meta/interface/property/intf_property_internal.h>
26
27 #include "ref_uri_util.h"
28
29 META_BEGIN_NAMESPACE()
30 namespace Internal {
31
32 // IObject
Resolve(const RefUri & uri) const33 IObject::Ptr MetaObject::Resolve(const RefUri& uri) const
34 {
35 if (uri == RefUri::ContextUri()) {
36 return interface_pointer_cast<IObjectInstance>(ObjectContext()->GetValue());
37 }
38 return Super::Resolve(uri);
39 }
40
41 // ILifecycle
Build(const IMetadata::Ptr & data)42 bool MetaObject::Build(const IMetadata::Ptr& data)
43 {
44 bool ret = Super::Build(data);
45 if (ret) {
46 // update owners in case the top most object was not set when we added some of the properties
47 // notice that we are building the object, so no locking is needed.
48 auto me = GetSelf();
49 META_NS::Iterate(
50 GetPropertyContainer(),
51 [&](const IObject::Ptr& p) {
52 if (auto pp = interface_cast<IPropertyInternal>(p)) {
53 pp->SetOwner(me);
54 }
55 return true;
56 },
57 IterateStrategy { TraversalType::NO_HIERARCHY, LockType::NO_LOCK });
58 }
59 return ret;
60 }
61
Destroy()62 void MetaObject::Destroy()
63 {
64 if (auto c = GetMetadata()) {
65 c->GetPropertyContainer()->RemoveAll();
66 }
67 Super::Destroy();
68 }
69
70 // IObjectContextProvider
PropertyObjectContext()71 IProperty::Ptr MetaObject::PropertyObjectContext()
72 {
73 if (!objectContext_) {
74 // By default use the global object context
75 auto context = META_NS::GetDefaultObjectContext();
76 CORE_ASSERT(context);
77 objectContext_ = ConstructProperty<IObjectContext::Ptr>(context->GetObjectRegistry(), "ObjectContext", context);
78 CORE_ASSERT(objectContext_);
79 if (auto internal = interface_cast<IPropertyInternal>(objectContext_.GetProperty())) {
80 internal->SetOwner(GetSelf());
81 }
82 }
83 return objectContext_;
84 }
85
PropertyObjectContext() const86 IProperty::ConstPtr MetaObject::PropertyObjectContext() const
87 {
88 if (!objectContext_) {
89 // By default use the global object context
90 auto context = META_NS::GetDefaultObjectContext();
91 CORE_ASSERT(context);
92 objectContext_ = ConstructProperty<IObjectContext::Ptr>(context->GetObjectRegistry(), "ObjectContext", context);
93 CORE_ASSERT(objectContext_);
94 if (auto internal = interface_cast<IPropertyInternal>(objectContext_.GetProperty())) {
95 internal->SetOwner(GetSelf());
96 }
97 }
98 return objectContext_;
99 }
100
ResetObjectContext()101 void MetaObject::ResetObjectContext()
102 {
103 if (objectContext_) {
104 objectContext_->SetValue(META_NS::GetDefaultObjectContext());
105 }
106 }
107
GetObjectRegistry() const108 IObjectRegistry& MetaObject::GetObjectRegistry() const
109 {
110 if (objectContext_) {
111 if (auto ctx = objectContext_->GetValue()) {
112 return ctx->GetObjectRegistry();
113 }
114 }
115 // No context set
116 return Super::GetObjectRegistry();
117 }
118
CloneMetadata() const119 IMetadata::Ptr MetaObject::CloneMetadata() const
120 {
121 return meta_->CloneMetadata();
122 }
123
GetPropertyContainer()124 IContainer::Ptr MetaObject::GetPropertyContainer()
125 {
126 return meta_->GetPropertyContainer();
127 }
128
GetPropertyContainer() const129 IContainer::ConstPtr MetaObject::GetPropertyContainer() const
130 {
131 return meta_->GetPropertyContainer();
132 }
133
AddFunction(const IFunction::Ptr & p)134 void MetaObject::AddFunction(const IFunction::Ptr& p)
135 {
136 meta_->AddFunction(p);
137 }
RemoveFunction(const IFunction::Ptr & p)138 void MetaObject::RemoveFunction(const IFunction::Ptr& p)
139 {
140 meta_->RemoveFunction(p);
141 }
AddProperty(const IProperty::Ptr & p)142 void MetaObject::AddProperty(const IProperty::Ptr& p)
143 {
144 if (auto pp = interface_pointer_cast<IPropertyInternal>(p)) {
145 pp->SetOwner(GetSelf());
146 }
147 meta_->AddProperty(p);
148 }
RemoveProperty(const IProperty::Ptr & p)149 void MetaObject::RemoveProperty(const IProperty::Ptr& p)
150 {
151 meta_->RemoveProperty(p);
152 }
AddEvent(const IEvent::Ptr & p)153 void MetaObject::AddEvent(const IEvent::Ptr& p)
154 {
155 meta_->AddEvent(p);
156 }
RemoveEvent(const IEvent::Ptr & p)157 void MetaObject::RemoveEvent(const IEvent::Ptr& p)
158 {
159 meta_->RemoveEvent(p);
160 }
SetProperties(const BASE_NS::vector<IProperty::Ptr> & vec)161 void MetaObject::SetProperties(const BASE_NS::vector<IProperty::Ptr>& vec)
162 {
163 meta_->SetProperties(vec);
164 }
Merge(const IMetadata::Ptr & data)165 void MetaObject::Merge(const IMetadata::Ptr& data)
166 {
167 meta_->Merge(data);
168 }
GetAllProperties()169 BASE_NS::vector<IProperty::Ptr> MetaObject::GetAllProperties()
170 {
171 return meta_->GetAllProperties();
172 }
GetAllProperties() const173 BASE_NS::vector<IProperty::ConstPtr> MetaObject::GetAllProperties() const
174 {
175 return static_cast<const IMetadata*>(meta_.get())->GetAllProperties();
176 }
GetAllFunctions()177 BASE_NS::vector<IFunction::Ptr> MetaObject::GetAllFunctions()
178 {
179 return meta_->GetAllFunctions();
180 }
GetAllFunctions() const181 BASE_NS::vector<IFunction::ConstPtr> MetaObject::GetAllFunctions() const
182 {
183 return static_cast<const IMetadata*>(meta_.get())->GetAllFunctions();
184 }
GetAllEvents()185 BASE_NS::vector<IEvent::Ptr> MetaObject::GetAllEvents()
186 {
187 return meta_->GetAllEvents();
188 }
GetAllEvents() const189 BASE_NS::vector<IEvent::ConstPtr> MetaObject::GetAllEvents() const
190 {
191 return static_cast<const IMetadata*>(meta_.get())->GetAllEvents();
192 }
GetPropertyByName(BASE_NS::string_view name)193 IProperty::Ptr MetaObject::GetPropertyByName(BASE_NS::string_view name)
194 {
195 return meta_->GetPropertyByName(name);
196 }
GetPropertyByName(BASE_NS::string_view name) const197 IProperty::ConstPtr MetaObject::GetPropertyByName(BASE_NS::string_view name) const
198 {
199 return meta_->GetPropertyByName(name);
200 }
GetFunctionByName(BASE_NS::string_view name)201 IFunction::Ptr MetaObject::GetFunctionByName(BASE_NS::string_view name)
202 {
203 return meta_->GetFunctionByName(name);
204 }
GetFunctionByName(BASE_NS::string_view name) const205 IFunction::ConstPtr MetaObject::GetFunctionByName(BASE_NS::string_view name) const
206 {
207 return meta_->GetFunctionByName(name);
208 }
GetEventByName(BASE_NS::string_view name) const209 IEvent::ConstPtr MetaObject::GetEventByName(BASE_NS::string_view name) const
210 {
211 return meta_->GetEventByName(name);
212 }
GetEventByName(BASE_NS::string_view name)213 IEvent::Ptr MetaObject::GetEventByName(BASE_NS::string_view name)
214 {
215 return meta_->GetEventByName(name);
216 }
GetMetadata() const217 IMetadata::Ptr MetaObject::GetMetadata() const
218 {
219 return meta_;
220 }
SetMetadata(const IMetadata::Ptr & meta)221 void MetaObject::SetMetadata(const IMetadata::Ptr& meta)
222 {
223 meta_ = meta;
224 }
225
GetStaticMetadata() const226 const StaticObjectMetadata& MetaObject::GetStaticMetadata() const
227 {
228 return GetStaticObjectMetadata();
229 }
230
231 } // namespace Internal
232
233 META_END_NAMESPACE()
234