• 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 
17 #include "property.h"
18 
19 #include <meta/interface/intf_owner.h>
20 
21 META_BEGIN_NAMESPACE()
22 namespace Internal {
23 
GetName() const24 BASE_NS::string PropertyBase::GetName() const
25 {
26     return name_;
27 }
GetOwner() const28 IObject::WeakPtr PropertyBase::GetOwner() const
29 {
30     return owner_;
31 }
SetOwner(IObject::Ptr owner)32 void PropertyBase::SetOwner(IObject::Ptr owner)
33 {
34     owner_ = owner;
35 }
SetSelf(IProperty::Ptr self)36 void PropertyBase::SetSelf(IProperty::Ptr self)
37 {
38     self_ = self;
39 }
SetValue(const IAny & value)40 AnyReturnValue PropertyBase::SetValue(const IAny& value)
41 {
42     auto res = SetInternalValue(value);
43     if (res == AnyReturn::SUCCESS) {
44         CallOnChanged();
45     }
46     return res;
47 }
GetValue() const48 const IAny& PropertyBase::GetValue() const
49 {
50     auto v = GetData();
51     CORE_ASSERT(v);
52     return *v;
53 }
IsCompatible(const TypeId & id) const54 bool PropertyBase::IsCompatible(const TypeId& id) const
55 {
56     auto v = GetData();
57     return v && META_NS::IsCompatible(*v, id);
58 }
GetTypeId() const59 TypeId PropertyBase::GetTypeId() const
60 {
61     auto p = GetData();
62     return p ? p->GetTypeId() : TypeId {};
63 }
EventOnChanged(MetadataQuery q) const64 IEvent::Ptr PropertyBase::EventOnChanged(MetadataQuery q) const
65 {
66     // notice that this requires locking outside unlike the old system
67     // if needed, consider separate locking for the construction
68     if (!onChanged_ && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
69         onChanged_ = CreateShared<OnChangedEvent>("OnChanged");
70         onChangedAtomic_.store(onChanged_.get());
71     }
72     return onChanged_;
73 }
NotifyChange() const74 void PropertyBase::NotifyChange() const
75 {
76     CallOnChanged();
77 }
CallOnChanged() const78 void PropertyBase::CallOnChanged() const
79 {
80     pendingInvoke_ = locked_ != 0;
81     if (!locked_ && onChanged_) {
82         onChanged_->Invoke();
83     }
84 }
Lock() const85 void PropertyBase::Lock() const
86 {
87     mutex_.lock();
88     ++locked_;
89 }
Unlock() const90 void PropertyBase::Unlock() const
91 {
92     BASE_NS::shared_ptr<OnChangedEvent> invoke;
93     IPropertyOwner::Ptr owner {};
94     if (--locked_ == 0 && pendingInvoke_) {
95         invoke = onChanged_;
96         pendingInvoke_ = false;
97         owner = interface_pointer_cast<IPropertyOwner>(owner_);
98     }
99     mutex_.unlock();
100     if (owner) {
101         owner->OnPropertyChanged(*this);
102     }
103     if (invoke) {
104         invoke->Invoke();
105     }
106 }
LockShared() const107 void PropertyBase::LockShared() const
108 {
109     Lock();
110 }
UnlockShared() const111 void PropertyBase::UnlockShared() const
112 {
113     Unlock();
114 }
Attaching(const IAttach::Ptr & target,const IObject::Ptr &)115 bool PropertyBase::Attaching(const IAttach::Ptr& target, const IObject::Ptr&)
116 {
117     owner_ = interface_pointer_cast<IObject>(target);
118     return true;
119 }
Detaching(const IAttach::Ptr &)120 bool PropertyBase::Detaching(const IAttach::Ptr&)
121 {
122     owner_ = nullptr;
123     return true;
124 }
SetInternalValue(const IAny & value)125 AnyReturnValue PropertyBase::SetInternalValue(const IAny& value)
126 {
127     if (auto& d = GetData()) {
128         if (d.get() == &value) {
129             return AnyReturn::SUCCESS;
130         }
131         return d->CopyFrom(value);
132     }
133     return AnyReturn::FAIL;
134 }
135 
SetInternalAny(IAny::Ptr any)136 AnyReturnValue GenericProperty::SetInternalAny(IAny::Ptr any)
137 {
138     data_ = BASE_NS::move(any);
139     return AnyReturn::SUCCESS;
140 }
GetInternalAny() const141 IAny::Ptr GenericProperty::GetInternalAny() const
142 {
143     return data_;
144 }
145 
146 } // namespace Internal
147 META_END_NAMESPACE()
148