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 #ifndef META_SRC_META_OBJECT_H
16 #define META_SRC_META_OBJECT_H
17 
18 #include <base/containers/array_view.h>
19 #include <base/containers/vector.h>
20 
21 #include <meta/base/types.h>
22 #include <meta/ext/implementation_macros.h>
23 #include <meta/ext/metadata_helpers.h>
24 #include <meta/ext/object_factory.h>
25 #include <meta/interface/builtin_objects.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_metadata.h>
30 #include <meta/interface/intf_object_context.h>
31 
32 #include "base_object.h"
33 
META_BEGIN_NAMESPACE()34 META_BEGIN_NAMESPACE()
35 
36 namespace Internal {
37 
38 class MetaObject : public BaseObjectFwd<MetaObject, META_NS::ClassId::MetaObject, IObjectContextProvider, IMetadata,
39                        IMetadataInternal> {
40     using Super = BaseObjectFwd;
41 
42 public:
43     static BASE_NS::vector<BASE_NS::Uid> GetInterfacesVector()
44     {
45         return Super::GetInterfacesVector();
46     }
47 
48 protected:
49     // ILifecycle
50     bool Build(const IMetadata::Ptr& data) override;
51     void Destroy() override;
52     IObjectRegistry& GetObjectRegistry() const override;
53     IObject::Ptr Resolve(const RefUri& uri) const override;
54 
55     // IObjectContextProvider
56 
57     IProperty::Ptr PropertyObjectContext() override;
58     IProperty::ConstPtr PropertyObjectContext() const override;
59     void ResetObjectContext() override;
60 
61     // IMetadata
62     IMetadata::Ptr CloneMetadata() const override;
63     IContainer::Ptr GetPropertyContainer() override;
64     IContainer::ConstPtr GetPropertyContainer() const override;
65 
66     void AddFunction(const IFunction::Ptr&) override;
67     void RemoveFunction(const IFunction::Ptr&) override;
68 
69     void AddProperty(const IProperty::Ptr&) override;
70     void RemoveProperty(const IProperty::Ptr&) override;
71 
72     void AddEvent(const IEvent::Ptr&) override;
73     void RemoveEvent(const IEvent::Ptr&) override;
74 
75     void SetProperties(const BASE_NS::vector<IProperty::Ptr>& vec) override;
76     void Merge(const IMetadata::Ptr& data) override;
77 
78     BASE_NS::vector<IProperty::Ptr> GetAllProperties() override;
79     BASE_NS::vector<IProperty::ConstPtr> GetAllProperties() const override;
80     BASE_NS::vector<IFunction::Ptr> GetAllFunctions() override;
81     BASE_NS::vector<IFunction::ConstPtr> GetAllFunctions() const override;
82     BASE_NS::vector<IEvent::Ptr> GetAllEvents() override;
83     BASE_NS::vector<IEvent::ConstPtr> GetAllEvents() const override;
84 
85     IProperty::Ptr GetPropertyByName(BASE_NS::string_view name) override;
86     IProperty::ConstPtr GetPropertyByName(BASE_NS::string_view name) const override;
87     IFunction::Ptr GetFunctionByName(BASE_NS::string_view name) override;
88     IFunction::ConstPtr GetFunctionByName(BASE_NS::string_view name) const override;
89     IEvent::ConstPtr GetEventByName(BASE_NS::string_view name) const override;
90     IEvent::Ptr GetEventByName(BASE_NS::string_view name) override;
91 
92     // IMetadataInternal
93     IMetadata::Ptr GetMetadata() const override;
94     void SetMetadata(const IMetadata::Ptr& meta) override;
95     const StaticObjectMetadata& GetStaticMetadata() const override;
96 
97 private:
98     Property<IObjectContext::Ptr> GetOrConstuctObjectContext() const;
99 
100     mutable Property<IObjectContext::Ptr> objectContext_;
101     IMetadata::Ptr meta_;
102 };
103 
104 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces>
105 class MetaObjectFwd : public ConcreteBaseFwd<FinalClass, ClassInfo, META_NS::Internal::MetaObject, Interfaces...> {
106     using Super = ConcreteBaseFwd<FinalClass, ClassInfo, META_NS::Internal::MetaObject, Interfaces...>;
107 
108 public:
109     IObjectRegistry& GetObjectRegistry() const override
110     {
111         return MetaObject::GetObjectRegistry();
112     }
113 
114     void SetMetadata(const META_NS::IMetadata::Ptr& meta) override
115     {
116         Super::SetMetadata(meta);
117         META_NS::ConstructPropertiesFromMetadata(this, this->GetStaticObjectMetadata(), meta);
118         META_NS::ConstructEventsFromMetadata(this, this->GetStaticObjectMetadata(), meta);
119         META_NS::ConstructFunctionsFromMetadata(this, this->GetStaticObjectMetadata(), meta);
120     }
121 
122 protected:
123     MetaObjectFwd() = default;
124     ~MetaObjectFwd() override = default;
125 };
126 
127 } // namespace Internal
128 
129 class MetaObject : public Internal::MetaObjectFwd<MetaObject, ClassId::MetaObject> {};
130 
131 META_END_NAMESPACE()
132 
133 #endif
134