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_data_container.h"
17
18 #include <algorithm>
19
20 META_BEGIN_NAMESPACE()
21 namespace Internal {
22
AddFunction(const IFunction::Ptr & p)23 bool ObjectDataContainer::AddFunction(const IFunction::Ptr& p)
24 {
25 return Attach(p);
26 }
RemoveFunction(const IFunction::Ptr & p)27 bool ObjectDataContainer::RemoveFunction(const IFunction::Ptr& p)
28 {
29 return Detach(p);
30 }
AddProperty(const IProperty::Ptr & p)31 bool ObjectDataContainer::AddProperty(const IProperty::Ptr& p)
32 {
33 return Attach(p);
34 }
RemoveProperty(const IProperty::Ptr & p)35 bool ObjectDataContainer::RemoveProperty(const IProperty::Ptr& p)
36 {
37 return Detach(p);
38 }
AddEvent(const IEvent::Ptr & p)39 bool ObjectDataContainer::AddEvent(const IEvent::Ptr& p)
40 {
41 return Attach(p);
42 }
RemoveEvent(const IEvent::Ptr & p)43 bool ObjectDataContainer::RemoveEvent(const IEvent::Ptr& p)
44 {
45 return Detach(p);
46 }
GetProperties()47 BASE_NS::vector<IProperty::Ptr> ObjectDataContainer::GetProperties()
48 {
49 return PtrArrayCast<IProperty>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IProperty::UID) } }));
50 }
GetProperties() const51 BASE_NS::vector<IProperty::ConstPtr> ObjectDataContainer::GetProperties() const
52 {
53 auto vec = const_cast<ObjectDataContainer&>(*this).GetProperties();
54 BASE_NS::vector<IProperty::ConstPtr> res;
55 res.insert(res.end(), vec.begin(), vec.end());
56 return res;
57 }
GetFunctions()58 BASE_NS::vector<IFunction::Ptr> ObjectDataContainer::GetFunctions()
59 {
60 return PtrArrayCast<IFunction>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IFunction::UID) } }));
61 }
GetFunctions() const62 BASE_NS::vector<IFunction::ConstPtr> ObjectDataContainer::GetFunctions() const
63 {
64 auto vec = const_cast<ObjectDataContainer&>(*this).GetFunctions();
65 BASE_NS::vector<IFunction::ConstPtr> res;
66 res.insert(res.end(), vec.begin(), vec.end());
67 return res;
68 }
GetEvents()69 BASE_NS::vector<IEvent::Ptr> ObjectDataContainer::GetEvents()
70 {
71 return PtrArrayCast<IEvent>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IEvent::UID) } }));
72 }
GetEvents() const73 BASE_NS::vector<IEvent::ConstPtr> ObjectDataContainer::GetEvents() const
74 {
75 auto vec = const_cast<ObjectDataContainer&>(*this).GetEvents();
76 BASE_NS::vector<IEvent::ConstPtr> res;
77 res.insert(res.end(), vec.begin(), vec.end());
78 return res;
79 }
GetMetadataType(const IObject::Ptr & p)80 static MetadataType GetMetadataType(const IObject::Ptr& p)
81 {
82 if (interface_cast<IProperty>(p)) {
83 return MetadataType::PROPERTY;
84 }
85 if (interface_cast<IEvent>(p)) {
86 return MetadataType::EVENT;
87 }
88 if (interface_cast<IFunction>(p)) {
89 return MetadataType::FUNCTION;
90 }
91 return MetadataType::UNKNOWN;
92 }
93
ConvertToTypeIds(MetadataType types)94 static BASE_NS::vector<TypeId> ConvertToTypeIds(MetadataType types)
95 {
96 BASE_NS::vector<TypeId> ids;
97 if (uint8_t(types) & uint8_t(MetadataType::PROPERTY)) {
98 ids.push_back(TypeId(IProperty::UID));
99 }
100 if (uint8_t(types) & uint8_t(MetadataType::EVENT)) {
101 ids.push_back(TypeId(IEvent::UID));
102 }
103 if (uint8_t(types) & uint8_t(MetadataType::FUNCTION)) {
104 ids.push_back(TypeId(IFunction::UID));
105 }
106 return ids;
107 }
108
GetAllMetadatas(MetadataType types) const109 BASE_NS::vector<MetadataInfo> ObjectDataContainer::GetAllMetadatas(MetadataType types) const
110 {
111 BASE_NS::vector<MetadataInfo> res;
112 if (auto s = GetOwner<IStaticMetadata>()) {
113 if (auto pm = s->GetStaticMetadata()) {
114 auto r = GetAllStaticMetadata(*pm, types);
115 res.insert(res.end(), r.begin(), r.end());
116 }
117 }
118 BASE_NS::vector<TypeId> ids = ConvertToTypeIds(types);
119 for (auto&& v : FindAll({ "", TraversalType::NO_HIERARCHY, ids })) {
120 // only compare name and type
121 if (std::find_if(res.begin(), res.end(), [name = v->GetName(), type = GetMetadataType(v)](const auto& e) {
122 return e.name == name && e.type == type;
123 }) == res.end()) {
124 MetadataInfo info { GetMetadataType(v), v->GetName() };
125 if (auto prop = interface_cast<IProperty>(v)) {
126 info.propertyType = prop->GetTypeId();
127 }
128 res.push_back(info);
129 }
130 }
131 return res;
132 }
133
GetMetadata(MetadataType type,BASE_NS::string_view name) const134 MetadataInfo ObjectDataContainer::GetMetadata(MetadataType type, BASE_NS::string_view name) const
135 {
136 if (auto s = GetOwner<IStaticMetadata>()) {
137 if (auto pm = s->GetStaticMetadata()) {
138 if (auto p = FindStaticMetadata(*pm, name, type)) {
139 MetadataInfo info { p->type, p->name, p->interfaceInfo };
140 if (p->type == MetadataType::PROPERTY) {
141 info.propertyType = GetMetaPropertyType(*p);
142 info.readOnly = p->flags & static_cast<uint8_t>(Internal::PropertyFlag::READONLY);
143 }
144 info.data = p;
145 return info;
146 }
147 }
148 }
149 BASE_NS::vector<TypeId> ids = ConvertToTypeIds(type);
150 if (auto v = FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, ids })) {
151 MetadataInfo info { GetMetadataType(v), v->GetName() };
152 if (auto prop = interface_cast<IProperty>(v)) {
153 info.propertyType = prop->GetTypeId();
154 }
155 return info;
156 }
157 return {};
158 }
159
GetProperty(BASE_NS::string_view name,MetadataQuery q)160 IProperty::Ptr ObjectDataContainer::GetProperty(BASE_NS::string_view name, MetadataQuery q)
161 {
162 IProperty::Ptr p = interface_pointer_cast<IProperty>(
163 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IProperty::UID) } }));
164
165 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
166 auto res = ConstructFromMetadata<IProperty>(GetOwner<IOwner>(), name, MetadataType::PROPERTY);
167 p = res.object;
168 if (p && !res.isForward) {
169 Attach(p);
170 }
171 }
172 return p;
173 }
GetProperty(BASE_NS::string_view name,MetadataQuery q) const174 IProperty::ConstPtr ObjectDataContainer::GetProperty(BASE_NS::string_view name, MetadataQuery q) const
175 {
176 return const_cast<ObjectDataContainer&>(*this).GetProperty(name, q);
177 }
GetFunction(BASE_NS::string_view name,MetadataQuery q)178 IFunction::Ptr ObjectDataContainer::GetFunction(BASE_NS::string_view name, MetadataQuery q)
179 {
180 IFunction::Ptr p = interface_pointer_cast<IFunction>(
181 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IFunction::UID) } }));
182
183 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
184 auto res = ConstructFromMetadata<IFunction>(GetOwner<IOwner>(), name, MetadataType::FUNCTION);
185 p = res.object;
186 if (p && !res.isForward) {
187 Attach(p);
188 }
189 }
190 return p;
191 }
GetFunction(BASE_NS::string_view name,MetadataQuery q) const192 IFunction::ConstPtr ObjectDataContainer::GetFunction(BASE_NS::string_view name, MetadataQuery q) const
193 {
194 return const_cast<ObjectDataContainer&>(*this).GetFunction(name, q);
195 }
GetEvent(BASE_NS::string_view name,MetadataQuery q)196 IEvent::Ptr ObjectDataContainer::GetEvent(BASE_NS::string_view name, MetadataQuery q)
197 {
198 IEvent::Ptr p = interface_pointer_cast<IEvent>(
199 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IEvent::UID) } }));
200
201 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
202 auto res = ConstructFromMetadata<IEvent>(GetOwner<IOwner>(), name, MetadataType::EVENT);
203 p = res.object;
204 if (p && !res.isForward) {
205 Attach(p);
206 }
207 }
208 return p;
209 }
GetEvent(BASE_NS::string_view name,MetadataQuery q) const210 IEvent::ConstPtr ObjectDataContainer::GetEvent(BASE_NS::string_view name, MetadataQuery q) const
211 {
212 return const_cast<ObjectDataContainer&>(*this).GetEvent(name, q);
213 }
214
215 } // namespace Internal
216
217 META_END_NAMESPACE()
218