• 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_ATTACHMENT_H
17 #define META_EXT_ATTACHMENT_H
18 
19 #include <meta/ext/object_fwd.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 BaseInterface>
36 class AttachmentBaseFwd : public IntroduceInterfaces<ObjectFwd, BaseInterface> {
37     using MyBase = IntroduceInterfaces<ObjectFwd, BaseInterface>;
38     META_OBJECT_NO_CLASSINFO(AttachmentBaseFwd, MyBase)
39 protected:
40     using Super::Super;
41 
42     /** AttachmentFwd calls this when IAttachment::Attaching is called by the framework. */
43     virtual bool AttachTo(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext) = 0;
44     /** AttachmentFwd calls this when IAttachment::Detaching is called by the framework. */
45     virtual bool DetachFrom(const META_NS::IAttach::Ptr& target) = 0;
46 
47 public:
48     META_BEGIN_STATIC_DATA()
49     META_STATIC_PROPERTY_DATA(IAttachment, IObject::WeakPtr, DataContext)
50     META_STATIC_PROPERTY_DATA(IAttachment, IAttach::WeakPtr, AttachedTo)
51     META_END_STATIC_DATA()
52 
53     /** Implementation of IAttachment::DataContext */
54     META_IMPLEMENT_READONLY_PROPERTY(IObject::WeakPtr, DataContext)
55     /** Implementation of IAttachment::AttachedTo */
56     META_IMPLEMENT_READONLY_PROPERTY(IAttach::WeakPtr, AttachedTo)
57 
58 private:
59     /** Private implementation of IAttachment::Attaching, handle properties and call the AttachTo method defined by
60      *  this class */
61     bool Attaching(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext) final
62     {
63         if (AttachTo(target, dataContext)) {
64             META_ACCESS_PROPERTY(AttachedTo)->SetValue(target);
65             META_ACCESS_PROPERTY(DataContext)->SetValue(dataContext);
66             return true;
67         }
68         return false;
69     }
70     /** Private implementation of IAttachment::Detaching, handle properties and call the DetachFrom method defined by
71      *  this class */
72     bool Detaching(const META_NS::IAttach::Ptr& target) final
73     {
74         if (DetachFrom(target)) {
75             META_ACCESS_PROPERTY(AttachedTo)->SetValue({});
76             META_ACCESS_PROPERTY(DataContext)->SetValue({});
77             return true;
78         }
79         return false;
80     }
81 };
82 
83 /**
84  * @brief The AttachmentFwd is a specialization of AttachmentBaseFwd for the simple cases where
85  *        the application uses ClassId::Object as the base class and implements META_NS::IAttachment directly.
86  */
87 class AttachmentFwd : public AttachmentBaseFwd<META_NS::IAttachment> {};
88 
89 META_END_NAMESPACE()
90 
91 #endif
92