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