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 #ifndef META_EXT_OBJECT_H
17 #define META_EXT_OBJECT_H
18
19 #include <meta/base/interface_macros.h>
20 #include <meta/base/namespace.h>
21 #include <meta/base/types.h>
22 #include <meta/ext/base_object.h>
23 #include <meta/ext/metadata_helpers.h>
24 #include <meta/ext/object_factory.h>
25 #include <meta/interface/interface_helpers.h>
26 #include <meta/interface/intf_attachment.h>
27 #include <meta/interface/intf_attachment_container.h>
28 #include <meta/interface/intf_container_query.h>
29 #include <meta/interface/intf_metadata.h>
30 #include <meta/interface/intf_named.h>
31 #include <meta/interface/intf_object.h>
32 #include <meta/interface/intf_object_context.h>
33 #include <meta/interface/intf_object_flags.h>
34 #include <meta/interface/intf_object_registry.h>
35 #include <meta/interface/property/intf_property.h>
36 #include <meta/interface/static_object_metadata.h>
37
META_BEGIN_NAMESPACE()38 META_BEGIN_NAMESPACE()
39
40 /// Object implementation that is full object with metadata and attachments
41 class MetaObject : public IntroduceInterfaces<BaseObject, IMetadata, IOwner, IAttach> {
42 using Super = IntroduceInterfaces<BaseObject, IMetadata, IOwner, IAttach>;
43
44 public:
45 bool Build(const IMetadata::Ptr& d) override
46 {
47 bool res = Super::Build(d);
48 if (res) {
49 data_ = GetObjectRegistry().ConstructObjectDataContainer();
50 auto attc = interface_cast<IAttachmentContainer>(data_);
51 res = attc && attc->Initialize(GetSelf<IAttach>());
52 }
53 return res;
54 }
55
56 bool AddProperty(const IProperty::Ptr& p) override
57 {
58 return data_->AddProperty(p);
59 }
60 bool RemoveProperty(const IProperty::Ptr& p) override
61 {
62 return data_->RemoveProperty(p);
63 }
64 bool AddFunction(const IFunction::Ptr& p) override
65 {
66 return data_->AddFunction(p);
67 }
68 bool RemoveFunction(const IFunction::Ptr& p) override
69 {
70 return data_->RemoveFunction(p);
71 }
72 bool AddEvent(const IEvent::Ptr& p) override
73 {
74 return data_->AddEvent(p);
75 }
76 bool RemoveEvent(const IEvent::Ptr& p) override
77 {
78 return data_->RemoveEvent(p);
79 }
80 BASE_NS::vector<META_NS::IProperty::Ptr> GetProperties() override
81 {
82 return data_->GetProperties();
83 }
84 BASE_NS::vector<META_NS::IProperty::ConstPtr> GetProperties() const override
85 {
86 return const_cast<const IMetadata&>(*data_).GetProperties();
87 }
88 BASE_NS::vector<IFunction::Ptr> GetFunctions() override
89 {
90 return data_->GetFunctions();
91 }
92 BASE_NS::vector<IFunction::ConstPtr> GetFunctions() const override
93 {
94 return const_cast<const IMetadata&>(*data_).GetFunctions();
95 }
96 BASE_NS::vector<IEvent::Ptr> GetEvents() override
97 {
98 return data_->GetEvents();
99 }
100 BASE_NS::vector<IEvent::ConstPtr> GetEvents() const override
101 {
102 return const_cast<const IMetadata&>(*data_).GetEvents();
103 }
104 BASE_NS::vector<MetadataInfo> GetAllMetadatas(MetadataType types) const override
105 {
106 return const_cast<const IMetadata&>(*data_).GetAllMetadatas(types);
107 }
108 MetadataInfo GetMetadata(MetadataType type, BASE_NS::string_view name) const override
109 {
110 return const_cast<const IMetadata&>(*data_).GetMetadata(type, name);
111 }
112 using IMetadata::GetProperty;
113 IProperty::Ptr GetProperty(BASE_NS::string_view name, MetadataQuery q) override
114 {
115 return data_->GetProperty(name, q);
116 }
117 IProperty::ConstPtr GetProperty(BASE_NS::string_view name, MetadataQuery q) const override
118 {
119 return const_cast<const IMetadata&>(*data_).GetProperty(name, q);
120 }
121 using IMetadata::GetFunction;
122 IFunction::ConstPtr GetFunction(BASE_NS::string_view name, MetadataQuery q) const override
123 {
124 return const_cast<const IMetadata&>(*data_).GetFunction(name, q);
125 }
126 IFunction::Ptr GetFunction(BASE_NS::string_view name, MetadataQuery q) override
127 {
128 return data_->GetFunction(name, q);
129 }
130 using IMetadata::GetEvent;
131 IEvent::ConstPtr GetEvent(BASE_NS::string_view name, MetadataQuery q) const override
132 {
133 return const_cast<const IMetadata&>(*data_).GetEvent(name, q);
134 }
135 IEvent::Ptr GetEvent(BASE_NS::string_view name, MetadataQuery q) override
136 {
137 return data_->GetEvent(name, q);
138 }
139
140 BASE_NS::string GetName() const override
141 {
142 if (auto cont = GetAttachmentContainer(false)) {
143 auto res = cont->FindAny({ "", TraversalType::NO_HIERARCHY, { IObjectName::UID }, false });
144 if (auto oname = interface_cast<IObjectName>(res)) {
145 return oname->GetName();
146 }
147 }
148 return Super::GetName();
149 }
150
151 protected: // IAttach
152 bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override
153 {
154 return interface_cast<IAttachmentContainer>(data_)->Attach(attachment, dataContext);
155 }
156
157 bool Detach(const IObject::Ptr& attachment) override
158 {
159 return interface_cast<IAttachmentContainer>(data_)->Detach(attachment);
160 }
161 BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) const override
162 {
163 return interface_cast<IAttachmentContainer>(data_)->GetAttachments(uids, strict);
164 }
165 bool HasAttachments() const override
166 {
167 return interface_cast<IContainer>(data_)->GetSize() != 0;
168 }
169 IContainer::Ptr GetAttachmentContainer(bool initializeAlways) const override
170 {
171 return interface_pointer_cast<IContainer>(data_);
172 }
173
174 protected:
175 MetaObject() = default;
176 ~MetaObject() override = default;
177 META_NO_COPY_MOVE(MetaObject)
178
179 private:
180 mutable IMetadata::Ptr data_;
181 };
182
183 META_END_NAMESPACE()
184
185 #endif
186