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_INTERFACE_IATTACHMENT_CONTAINER_H 17 #define META_INTERFACE_IATTACHMENT_CONTAINER_H 18 19 #include <meta/base/namespace.h> 20 #include <meta/interface/intf_attach.h> 21 22 META_BEGIN_NAMESPACE() 23 24 META_REGISTER_INTERFACE(IAttachmentContainer, "c7ca2933-25b5-425a-ab26-a08fee2bf233") 25 26 /** 27 * @brief The IAttachmentContainer interface defines an interface which can be instantiated 28 * by implementing classes that want to implement the IAttach interface. 29 * 30 * The IAttachmentContainer defines an interface which is compatible with IAttach interface, 31 * so that a class implementing IAttach can mostly forward the IAttach interface calls 32 * to an IAttachmentContainer implementation. 33 * 34 * The MetaObject library contains a built-in thread-safe implementation of IAttachmentContainer, 35 * which can be instantiated by calling 36 * CORE_NS::CreateInstance<META_NS::IAttachmentContainer>(META_NS::IAttachmentContainer::UID); 37 */ 38 class IAttachmentContainer : public CORE_NS::IInterface { 39 META_INTERFACE2(CORE_NS::IInterface, IAttachmentContainer) 40 public: 41 /** 42 * @brief Initialize the container. 43 * @param owner The owner object which wants to implement IAttach. The container does not 44 * take ownership of the owner. 45 */ 46 virtual bool Initialize(const META_NS::IAttach::Ptr& owner) = 0; 47 /** 48 * @brief See IAttach::Attach() 49 */ Attach(const IObject::Ptr & attachment)50 bool Attach(const IObject::Ptr& attachment) 51 { 52 return Attach(attachment, {}); 53 } 54 template<class T> Attach(const T & object)55 bool Attach(const T& object) 56 { 57 return Attach(interface_pointer_cast<IObject>(object), {}); 58 } 59 /** 60 * @brief See IAttach::Attach() 61 */ 62 virtual bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) = 0; 63 template<class T, class U> Attach(const T & object,const U & dataContext)64 bool Attach(const T& object, const U& dataContext) 65 { 66 return Attach(interface_pointer_cast<IObject>(object), interface_pointer_cast<IObject>(dataContext)); 67 } 68 /** 69 * @brief Attach an attachment to this object at given position. 70 * @param pos The index where the attachment should be added. If pos >= container size, the attachment 71 * is added at the end of the container. 72 * @param attachment The attachment to add 73 * @param dataContext The object to use as the data context for the attachment. If dataContext is null, 74 * the object being attached to will be used as the data context. 75 * @return True if the operation succeeded. False otherwise. 76 */ 77 virtual bool Attach(IContainer::SizeType pos, const IObject::Ptr& attachment, const IObject::Ptr& dataContext) = 0; 78 template<class T, class U> Attach(IContainer::SizeType pos,const T & object,const U & dataContext)79 bool Attach(IContainer::SizeType pos, const T& object, const U& dataContext) 80 { 81 return Attach(pos, interface_pointer_cast<IObject>(object), interface_pointer_cast<IObject>(dataContext)); 82 } 83 /** 84 * @brief See IAttach::Detach() 85 */ 86 virtual bool Detach(const IObject::Ptr& attachment) = 0; 87 template<class T> Detach(const T & object)88 bool Detach(const T& object) 89 { 90 return Detach(interface_pointer_cast<IObject>(object)); 91 } 92 /** 93 * @brief See IAttach::GetAttachments() 94 */ GetAttachments()95 BASE_NS::vector<IObject::Ptr> GetAttachments() 96 { 97 return GetAttachments({}, false); 98 } 99 template<class T> GetAttachments()100 BASE_NS::vector<typename T::Ptr> GetAttachments() 101 { 102 return PtrArrayCast<T>(GetAttachments({}, false)); 103 } 104 /** 105 * @brief See IAttach::GetAttachments() 106 */ 107 virtual BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) = 0; 108 /** 109 * @brief Remove all attachments from the container. 110 * @note All of the items are detached, i.e. IAttachment::Detaching is called on all the currently 111 * attached attachments. 112 */ 113 virtual void RemoveAllAttachments() = 0; 114 115 /** 116 * @brief Find attachment by name 117 */ 118 virtual IObject::Ptr FindByName(const BASE_NS::string& name) const = 0; 119 }; 120 121 META_END_NAMESPACE() 122 123 #endif 124