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_OBJECT_H
16 #define META_SRC_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_container_query.h>
30 #include <meta/interface/intf_derived.h>
31 #include <meta/interface/intf_metadata.h>
32 #include <meta/interface/intf_object.h>
33 #include <meta/interface/intf_object_context.h>
34 #include <meta/interface/intf_object_registry.h>
35 #include <meta/interface/object_type_info.h>
36 
37 #include "meta_object.h"
38 
META_BEGIN_NAMESPACE()39 META_BEGIN_NAMESPACE()
40 
41 namespace Internal {
42 
43 class Object : public MetaObjectFwd<Object, META_NS::ClassId::Object, IAttach, IContainerQuery> {
44     using Super = MetaObjectFwd;
45 
46 public:
47     bool Build(const IMetadata::Ptr& data) override;
48     void Destroy() override;
49 
50     // IAttach
51     bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
52     bool Detach(const IObject::Ptr& attachment) override;
53     BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) const override;
54     bool HasAttachments() const override;
55     IContainer::Ptr GetAttachmentContainer(bool initializeAlways) const override;
56 
57     const StaticObjectMetadata& GetStaticMetadata() const override;
58 
59     BASE_NS::vector<IContainer::Ptr> FindAllContainers(const ContainerFindOptions& options) const override;
60 
61 private:
62     Property<IObjectContext::Ptr> GetOrConstuctObjectContext() const;
63     void ValidateAttachmentContainer() const;
64 
65     mutable META_NS::IAttachmentContainer::Ptr attachments_;
66 };
67 
68 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces>
69 class ObjectFwd : public ConcreteBaseFwd<FinalClass, ClassInfo, META_NS::Internal::Object, Interfaces...> {
70     using Super = ConcreteBaseFwd<FinalClass, ClassInfo, META_NS::Internal::Object, Interfaces...>;
71 
72 public:
73     IObjectRegistry& GetObjectRegistry() const override
74     {
75         return Object::GetObjectRegistry();
76     }
77 
78     void SetMetadata(const META_NS::IMetadata::Ptr& meta) override
79     {
80         Super::SetMetadata(meta);
81         META_NS::ConstructPropertiesFromMetadata(this, this->GetStaticObjectMetadata(), meta);
82         META_NS::ConstructEventsFromMetadata(this, this->GetStaticObjectMetadata(), meta);
83         META_NS::ConstructFunctionsFromMetadata(this, this->GetStaticObjectMetadata(), meta);
84     }
85 
86 protected:
87     ObjectFwd() = default;
88     ~ObjectFwd() override = default;
89 };
90 
91 } // namespace Internal
92 
93 META_END_NAMESPACE()
94 
95 #endif
96