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_ICONTAINER_QUERY_H
17 #define META_INTERFACE_ICONTAINER_QUERY_H
18 
19 #include <meta/interface/intf_container.h>
20 
21 META_BEGIN_NAMESPACE()
22 
23 META_REGISTER_INTERFACE(IContainerQuery, "f125ea3d-7c87-4514-8fab-faf8625e7072")
24 
25 /**
26  * @brief The IContainerQuery interface defines an interface for querying an object
27  *        for any containers it might contain that are compatible with a given set
28  *        of interfaces.
29  */
30 class IContainerQuery : public CORE_NS::IInterface {
31     META_INTERFACE(CORE_NS::IInterface, IContainerQuery)
32 public:
33     /**
34      * @brief The FindOptions struct defines a set of options that can be used to find containers.
35      */
36     struct ContainerFindOptions {
37         /** The list of uids the returned containers must be compatible with.
38          *  If the list is empty, all known containers are returned. */
39         BASE_NS::vector<BASE_NS::Uid> uids;
40         /** Maximum number of results to return. If 0, no limit will be enforced. */
41         size_t maxCount {};
42     };
43     /**
44      * @brief Returns a list of containers the object is aware of that are compatible
45      *        with the set of interfaces given as a parameter.
46      * @param uids The list of uids the returned containers must be compatible with.
47      *             If the list is empty, all known containers are returned.
48      * @param maxCount Maximum number of results to return. If 0, no limit will be enforced.
49      */
50     virtual BASE_NS::vector<IContainer::Ptr> FindAllContainers(const ContainerFindOptions& options) const = 0;
51     /**
52      * @brief Returns all containers matching the search criteria.
53      */
FindAllContainers(const BASE_NS::vector<BASE_NS::Uid> & uids)54     BASE_NS::vector<IContainer::Ptr> FindAllContainers(const BASE_NS::vector<BASE_NS::Uid>& uids) const
55     {
56         return FindAllContainers(ContainerFindOptions { uids, 0 });
57     }
58     /**
59      * @brief Returns all containers which the object is aware of.
60      */
FindAllContainers()61     BASE_NS::vector<IContainer::Ptr> FindAllContainers() const
62     {
63         return FindAllContainers(ContainerFindOptions {});
64     }
65     /**
66      * @brief Returns the matching containers for a given type.
67      */
68     template<class T>
FindAllContainers()69     BASE_NS::vector<IContainer::Ptr> FindAllContainers() const
70     {
71         static_assert(IsKindOfIInterface_v<T*>, "Type must be derived from IInterface");
72         return FindAllContainers({ { T::UID }, 0 });
73     }
74     /**
75      * @brief Returns the first matching container for a given interface.
76      */
77     template<class T>
FindAnyContainer()78     IContainer::Ptr FindAnyContainer() const
79     {
80         static_assert(IsKindOfIInterface_v<T*>, "Type must be derived from IInterface");
81         if (auto c = FindAllContainers({ { T::UID }, 1 }); !c.empty()) {
82             return c.front();
83         }
84         return {};
85     }
86 };
87 
88 META_END_NAMESPACE()
89 
90 #endif // META_INTERFACE_ICONTAINER_QUERY_H
91