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_METADATA_H
16 #define META_SRC_METADATA_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/interface/interface_helpers.h>
24 #include <meta/interface/intf_container.h>
25 #include <meta/interface/intf_metadata.h>
26 
27 #include "container/flat_container.h"
28 
META_BEGIN_NAMESPACE()29 META_BEGIN_NAMESPACE()
30 
31 namespace Internal {
32 
33 class Metadata : public IntroduceInterfaces<IMetadata> {
34 public:
35     Metadata();
36     Metadata(const IMetadata& data);
37     ~Metadata() override = default;
38 
39     IMetadata::Ptr CloneMetadata() const override;
40 
41 protected:
42     IContainer::Ptr GetPropertyContainer() override;
43     IContainer::ConstPtr GetPropertyContainer() const override;
44 
45     void AddFunction(const IFunction::Ptr&) override;
46     void RemoveFunction(const IFunction::Ptr&) override;
47 
48     void AddProperty(const IProperty::Ptr&) override;
49     void RemoveProperty(const IProperty::Ptr&) override;
50 
51     void AddEvent(const IEvent::Ptr&) override;
52     void RemoveEvent(const IEvent::Ptr&) override;
53 
54     void SetProperties(const BASE_NS::vector<IProperty::Ptr>&) override;
55 
56     void Merge(const IMetadata::Ptr&) override;
57 
58     BASE_NS::vector<IProperty::Ptr> GetAllProperties() override;
59     BASE_NS::vector<IProperty::ConstPtr> GetAllProperties() const override;
60     BASE_NS::vector<IFunction::Ptr> GetAllFunctions() override;
61     BASE_NS::vector<IFunction::ConstPtr> GetAllFunctions() const override;
62     BASE_NS::vector<IEvent::Ptr> GetAllEvents() override;
63     BASE_NS::vector<IEvent::ConstPtr> GetAllEvents() const override;
64 
65     IProperty::Ptr GetPropertyByName(BASE_NS::string_view name) override;
66     IProperty::ConstPtr GetPropertyByName(BASE_NS::string_view name) const override;
67     IFunction::Ptr GetFunctionByName(BASE_NS::string_view name) override;
68     IFunction::ConstPtr GetFunctionByName(BASE_NS::string_view name) const override;
69     IEvent::ConstPtr GetEventByName(BASE_NS::string_view name) const override;
70     IEvent::Ptr GetEventByName(BASE_NS::string_view name) override;
71 
72 private:
73     BASE_NS::shared_ptr<class MetadataPropertyContainer> propertyContainer_;
74     IContainer::Ptr properties_;
75     BASE_NS::vector<IFunction::Ptr> functionMetadata_;
76     BASE_NS::vector<IEvent::Ptr> eventMetadata_;
77 };
78 
79 class MetadataPropertyContainer
80     : public IntroduceInterfaces<IObject, IContainer, IRequiredInterfaces, IContainerPreTransaction, IIterable> {
81 public:
82     MetadataPropertyContainer();
83     ~MetadataPropertyContainer() override = default;
84     META_NO_COPY_MOVE(MetadataPropertyContainer);
85 
86     BASE_NS::vector<IObject::Ptr> GetAll() const override;
87     IObject::Ptr GetAt(SizeType index) const override;
88     SizeType GetSize() const override;
89     BASE_NS::vector<IObject::Ptr> FindAll(const FindOptions& options) const override;
90     IObject::Ptr FindAny(const FindOptions& options) const override;
91     IObject::Ptr FindByName(BASE_NS::string_view name) const override;
92     bool Add(const IObject::Ptr& object) override;
93     bool Insert(SizeType index, const IObject::Ptr& object) override;
94     bool Remove(SizeType index) override;
95     bool Remove(const IObject::Ptr& child) override;
96     bool Move(SizeType fromIndex, SizeType toIndex) override;
97     bool Move(const IObject::Ptr& child, SizeType toIndex) override;
98     bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override;
99     void RemoveAll() override;
100     bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
101     bool IsAncestorOf(const IObject::ConstPtr& object) const override;
102     BASE_NS::vector<TypeId> GetRequiredInterfaces() const override;
103     BASE_NS::shared_ptr<IEvent> EventOnAdded() const override;
104     BASE_NS::shared_ptr<IEvent> EventOnRemoved() const override;
105     BASE_NS::shared_ptr<IEvent> EventOnMoved() const override;
106     BASE_NS::shared_ptr<IEvent> EventOnAdding() const override;
107     BASE_NS::shared_ptr<IEvent> EventOnRemoving() const override;
108     IterationResult Iterate(const IterationParameters& params) override;
109     IterationResult Iterate(const IterationParameters& params) const override;
110 
111     ObjectId GetClassId() const override
112     {
113         return {};
114     }
115     BASE_NS::string_view GetClassName() const override
116     {
117         return "MetadataPropertyContainer";
118     }
119     BASE_NS::string GetName() const override
120     {
121         return "";
122     }
123     BASE_NS::vector<BASE_NS::Uid> GetInterfaces() const override
124     {
125         return GetInterfacesVector();
126     }
127 
128 private:
129     FlatContainer impl_;
130     mutable BASE_NS::shared_ptr<EventImpl<IOnChildChanged>> onAdded_ { CreateShared<EventImpl<IOnChildChanged>>(
131         "OnAdded") };
132     mutable BASE_NS::shared_ptr<EventImpl<IOnChildChanged>> onRemoved_ { CreateShared<EventImpl<IOnChildChanged>>(
133         "OnRemoved") };
134     mutable BASE_NS::shared_ptr<EventImpl<IOnChildMoved>> onMoved_ { CreateShared<EventImpl<IOnChildMoved>>(
135         "OnMoved") };
136     mutable BASE_NS::shared_ptr<EventImpl<IOnChildChanging>> onAdding_ { CreateShared<EventImpl<IOnChildChanging>>(
137         "OnAdding") };
138     mutable BASE_NS::shared_ptr<EventImpl<IOnChildChanging>> onRemoving_ { CreateShared<EventImpl<IOnChildChanging>>(
139         "OnRemoving") };
140 };
141 
142 } // namespace Internal
143 
144 META_END_NAMESPACE()
145 #endif
146