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 "object_data_container.h"
18
19 META_BEGIN_NAMESPACE()
20 namespace Internal {
21
AddFunction(const IFunction::Ptr & p)22 bool ObjectDataContainer::AddFunction(const IFunction::Ptr& p)
23 {
24 return Attach(p);
25 }
RemoveFunction(const IFunction::Ptr & p)26 bool ObjectDataContainer::RemoveFunction(const IFunction::Ptr& p)
27 {
28 return Detach(p);
29 }
AddProperty(const IProperty::Ptr & p)30 bool ObjectDataContainer::AddProperty(const IProperty::Ptr& p)
31 {
32 return Attach(p);
33 }
RemoveProperty(const IProperty::Ptr & p)34 bool ObjectDataContainer::RemoveProperty(const IProperty::Ptr& p)
35 {
36 return Detach(p);
37 }
AddEvent(const IEvent::Ptr & p)38 bool ObjectDataContainer::AddEvent(const IEvent::Ptr& p)
39 {
40 return Attach(p);
41 }
RemoveEvent(const IEvent::Ptr & p)42 bool ObjectDataContainer::RemoveEvent(const IEvent::Ptr& p)
43 {
44 return Detach(p);
45 }
GetProperties()46 BASE_NS::vector<IProperty::Ptr> ObjectDataContainer::GetProperties()
47 {
48 return PtrArrayCast<IProperty>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IProperty::UID) } }));
49 }
GetProperties() const50 BASE_NS::vector<IProperty::ConstPtr> ObjectDataContainer::GetProperties() const
51 {
52 auto vec = const_cast<ObjectDataContainer&>(*this).GetProperties();
53 BASE_NS::vector<IProperty::ConstPtr> res;
54 res.insert(res.end(), vec.begin(), vec.end());
55 return res;
56 }
GetFunctions()57 BASE_NS::vector<IFunction::Ptr> ObjectDataContainer::GetFunctions()
58 {
59 return PtrArrayCast<IFunction>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IFunction::UID) } }));
60 }
GetFunctions() const61 BASE_NS::vector<IFunction::ConstPtr> ObjectDataContainer::GetFunctions() const
62 {
63 auto vec = const_cast<ObjectDataContainer&>(*this).GetFunctions();
64 BASE_NS::vector<IFunction::ConstPtr> res;
65 res.insert(res.end(), vec.begin(), vec.end());
66 return res;
67 }
GetEvents()68 BASE_NS::vector<IEvent::Ptr> ObjectDataContainer::GetEvents()
69 {
70 return PtrArrayCast<IEvent>(FindAll({ "", TraversalType::NO_HIERARCHY, { TypeId(IEvent::UID) } }));
71 }
GetEvents() const72 BASE_NS::vector<IEvent::ConstPtr> ObjectDataContainer::GetEvents() const
73 {
74 auto vec = const_cast<ObjectDataContainer&>(*this).GetEvents();
75 BASE_NS::vector<IEvent::ConstPtr> res;
76 res.insert(res.end(), vec.begin(), vec.end());
77 return res;
78 }
GetMetadataType(const IObject::Ptr & p)79 static MetadataType GetMetadataType(const IObject::Ptr& p)
80 {
81 if (interface_cast<IProperty>(p)) {
82 return MetadataType::PROPERTY;
83 }
84 if (interface_cast<IEvent>(p)) {
85 return MetadataType::EVENT;
86 }
87 if (interface_cast<IFunction>(p)) {
88 return MetadataType::FUNCTION;
89 }
90 return MetadataType::UNKNOWN;
91 }
92
GetAllMetadatas(MetadataType types) const93 BASE_NS::vector<MetadataInfo> ObjectDataContainer::GetAllMetadatas(MetadataType types) const
94 {
95 BASE_NS::vector<MetadataInfo> res;
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 if (auto s = GetOwner<IStaticMetadata>()) {
107 if (auto pm = s->GetStaticMetadata()) {
108 auto r = GetAllStaticMetadata(*pm, types);
109 res.insert(res.end(), r.begin(), r.end());
110 }
111 }
112 for (auto&& v : FindAll({ "", TraversalType::NO_HIERARCHY, ids })) {
113 MetadataInfo info { GetMetadataType(v), v->GetName() };
114 if (auto prop = interface_cast<IProperty>(v)) {
115 info.propertyType = prop->GetTypeId();
116 }
117 if (std::find(res.begin(), res.end(), info) == res.end()) {
118 res.push_back(info);
119 }
120 }
121 return res;
122 }
123
GetProperty(BASE_NS::string_view name,MetadataQuery q)124 IProperty::Ptr ObjectDataContainer::GetProperty(BASE_NS::string_view name, MetadataQuery q)
125 {
126 IProperty::Ptr p = interface_pointer_cast<IProperty>(
127 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IProperty::UID) } }));
128
129 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
130 p = ConstructPropertyFromMetadata(GetOwner<IOwner>(), name);
131 if (p) {
132 Attach(p);
133 }
134 }
135 return p;
136 }
GetProperty(BASE_NS::string_view name,MetadataQuery q) const137 IProperty::ConstPtr ObjectDataContainer::GetProperty(BASE_NS::string_view name, MetadataQuery q) const
138 {
139 return const_cast<ObjectDataContainer&>(*this).GetProperty(name, q);
140 }
GetFunction(BASE_NS::string_view name,MetadataQuery q)141 IFunction::Ptr ObjectDataContainer::GetFunction(BASE_NS::string_view name, MetadataQuery q)
142 {
143 IFunction::Ptr p = interface_pointer_cast<IFunction>(
144 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IFunction::UID) } }));
145
146 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
147 p = ConstructFromMetadata<IFunction>(GetOwner<IOwner>(), name, MetadataType::FUNCTION);
148 if (p) {
149 Attach(p);
150 }
151 }
152 return p;
153 }
GetFunction(BASE_NS::string_view name,MetadataQuery q) const154 IFunction::ConstPtr ObjectDataContainer::GetFunction(BASE_NS::string_view name, MetadataQuery q) const
155 {
156 return const_cast<ObjectDataContainer&>(*this).GetFunction(name, q);
157 }
GetEvent(BASE_NS::string_view name,MetadataQuery q)158 IEvent::Ptr ObjectDataContainer::GetEvent(BASE_NS::string_view name, MetadataQuery q)
159 {
160 IEvent::Ptr p = interface_pointer_cast<IEvent>(
161 FindAny({ BASE_NS::string(name), TraversalType::NO_HIERARCHY, { TypeId(IEvent::UID) } }));
162
163 if (!p && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
164 p = ConstructFromMetadata<IEvent>(GetOwner<IOwner>(), name, MetadataType::EVENT);
165 if (p) {
166 Attach(p);
167 }
168 }
169 return p;
170 }
GetEvent(BASE_NS::string_view name,MetadataQuery q) const171 IEvent::ConstPtr ObjectDataContainer::GetEvent(BASE_NS::string_view name, MetadataQuery q) const
172 {
173 return const_cast<ObjectDataContainer&>(*this).GetEvent(name, q);
174 }
175
176 } // namespace Internal
177
178 META_END_NAMESPACE()
179