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_ATTACHMENT_H
17 #define META_EXT_ATTACHMENT_H
18 
19 #include <meta/ext/object.h>
20 #include <meta/interface/builtin_objects.h>
21 #include <meta/interface/intf_attachment.h>
22 
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24 
25 /**
26  * @brief The AttachmentBaseFwd class can be used as a base class by classes that want to create an object
27  *        which implements the IAttachment interface.
28  *
29  *        Instead of directly implementing the IAttachment interface methods, AttachmentFwd defines
30  *        two methods, AttachTo and DetachFrom which are called when the corresponding Attaching/Detaching
31  *        methods of IAttachment are called.
32  *
33  *        AttachmentFwd handles attachment target and IAttachment::AttachedTo automatically.
34  */
35 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, const META_NS::ClassInfo& SuperClassInfo,
36     class BaseInterface, class... Interfaces>
37 class AttachmentBaseFwd
38     : public META_NS::ObjectFwd<FinalClass, ClassInfo, SuperClassInfo, BaseInterface, Interfaces...> {
39 protected:
40     using Fwd = META_NS::ObjectFwd<FinalClass, ClassInfo, SuperClassInfo, BaseInterface, Interfaces...>;
41 
42     using Fwd::Fwd;
43 
44     /** AttachmentFwd calls this when IAttachment::Attaching is called by the framework. */
45     virtual bool AttachTo(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext) = 0;
46     /** AttachmentFwd calls this when IAttachment::Detaching is called by the framework. */
47     virtual bool DetachFrom(const META_NS::IAttach::Ptr& target) = 0;
48     /** Implementation of IAttachment::DataContext */
49     META_IMPLEMENT_INTERFACE_READONLY_PROPERTY(IAttachment, IObject::WeakPtr, DataContext);
50     /** Implementation of IAttachment::AttachedTo */
51     META_IMPLEMENT_INTERFACE_READONLY_PROPERTY(IAttachment, IAttach::WeakPtr, AttachedTo, {});
52 
53 private:
54     /** Private implementation of IAttachment::Attaching, handle properties and call the AttachTo method defined by
55      *  this class */
56     bool Attaching(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext) final
57     {
58         if (AttachTo(target, dataContext)) {
59             META_ACCESS_PROPERTY(AttachedTo)->SetValue(target);
60             META_ACCESS_PROPERTY(DataContext)->SetValue(dataContext);
61             return true;
62         }
63         return false;
64     }
65     /** Private implementation of IAttachment::Detaching, handle properties and call the DetachFrom method defined by
66      *  this class */
67     bool Detaching(const META_NS::IAttach::Ptr& target) final
68     {
69         if (DetachFrom(target)) {
70             META_ACCESS_PROPERTY(AttachedTo)->SetValue({});
71             META_ACCESS_PROPERTY(DataContext)->SetValue({});
72             return true;
73         }
74         return false;
75     }
76 };
77 
78 /**
79  * @brief The AttachmentFwd is a specialization of AttachmentBaseFwd for the simple cases where
80  *        the application uses ClassId::Object as the base class and implements META_NS::IAttachment directly.
81  */
82 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces>
83 class AttachmentFwd
84     : public AttachmentBaseFwd<FinalClass, ClassInfo, META_NS::ClassId::Object, META_NS::IAttachment, Interfaces...> {
85     using Fwd = META_NS::AttachmentBaseFwd<FinalClass, ClassInfo, META_NS::ClassId::Object, META_NS::IAttachment,
86         Interfaces...>;
87 
88 protected:
89     using Fwd::Fwd;
90 };
91 
92 META_END_NAMESPACE()
93 
94 #endif
95