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 #include "object_context.h"
17
18 META_BEGIN_NAMESPACE()
19
20 namespace Internal {
21
SetSuperInstance(const IObject::Ptr & aggr,const IObject::Ptr & super)22 void ObjectContext::SetSuperInstance(const IObject::Ptr& aggr, const IObject::Ptr& super)
23 {
24 ObjectFwd::SetSuperInstance(aggr, super);
25 proxy_ = interface_cast<IProxyObject>(super);
26 assert(proxy_);
27 metadata_ = interface_cast<IMetadata>(super);
28 assert(metadata_);
29 }
30
GetObjectRegistry()31 IObjectRegistry& ObjectContext::GetObjectRegistry()
32 {
33 // If we are shadowing another context, get object registry from the shadowed object
34 if (auto target = GetTarget()) {
35 return interface_cast<IObjectContext>(target)->GetObjectRegistry();
36 }
37 // By default return the global one.
38 return META_NS::GetObjectRegistry();
39 }
40
GetTarget() const41 const IObject::Ptr ObjectContext::GetTarget() const
42 {
43 return proxy_ ? proxy_->GetTarget() : nullptr;
44 }
45
SetTarget(const IObject::Ptr & target)46 bool ObjectContext::SetTarget(const IObject::Ptr& target)
47 {
48 if (target && !interface_cast<META_NS::IObjectContext>(target)) {
49 CORE_LOG_E("ObjectContext shadow targets must implement IObjectContext");
50 return false;
51 }
52 return proxy_ && proxy_->SetTarget(target);
53 }
54
GetOverrides() const55 BASE_NS::vector<IProperty::ConstPtr> ObjectContext::GetOverrides() const
56 {
57 return proxy_ ? proxy_->GetOverrides() : BASE_NS::vector<IProperty::ConstPtr> {};
58 }
59
GetOverride(BASE_NS::string_view name) const60 IProperty::ConstPtr ObjectContext::GetOverride(BASE_NS::string_view name) const
61 {
62 return proxy_ ? proxy_->GetOverride(name) : nullptr;
63 }
64
SetPropertyTarget(const IProperty::Ptr & property)65 IProperty::Ptr ObjectContext::SetPropertyTarget(const IProperty::Ptr& property)
66 {
67 return proxy_ ? proxy_->SetPropertyTarget(property) : nullptr;
68 }
69
GetProxyProperty(BASE_NS::string_view name) const70 IProperty::ConstPtr ObjectContext::GetProxyProperty(BASE_NS::string_view name) const
71 {
72 return proxy_ ? proxy_->GetProxyProperty(name) : nullptr;
73 }
74
GetProperty(BASE_NS::string_view name,MetadataQuery q)75 IProperty::Ptr ObjectContext::GetProperty(BASE_NS::string_view name, MetadataQuery q)
76 {
77 return metadata_ ? metadata_->GetProperty(name) : nullptr;
78 }
79
GetProperty(BASE_NS::string_view name,MetadataQuery q) const80 IProperty::ConstPtr ObjectContext::GetProperty(BASE_NS::string_view name, MetadataQuery q) const
81 {
82 return metadata_ ? metadata_->GetProperty(name) : nullptr;
83 }
84
GetProperties()85 BASE_NS::vector<IProperty::Ptr> ObjectContext::GetProperties()
86 {
87 return metadata_ ? metadata_->GetProperties() : BASE_NS::vector<IProperty::Ptr> {};
88 }
89
GetProperties() const90 BASE_NS::vector<IProperty::ConstPtr> ObjectContext::GetProperties() const
91 {
92 const IMetadata* p = metadata_;
93 return p ? p->GetProperties() : BASE_NS::vector<IProperty::ConstPtr> {};
94 }
95
AddInternalProxy(BASE_NS::string_view propertyPropertyName,BASE_NS::string_view sourcePropertyName)96 void ObjectContext::AddInternalProxy(BASE_NS::string_view propertyPropertyName, BASE_NS::string_view sourcePropertyName)
97 {
98 if (proxy_) {
99 proxy_->AddInternalProxy(propertyPropertyName, sourcePropertyName);
100 }
101 }
102
103 } // namespace Internal
104
105 META_END_NAMESPACE()
106