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 API_CORE_PLUGIN_IINTERFACE_H
17 #define API_CORE_PLUGIN_IINTERFACE_H
18 
19 #include <base/containers/refcnt_ptr.h>
20 #include <base/namespace.h>
21 #include <base/util/uid.h>
22 #include <core/namespace.h>
23 
CORE_BEGIN_NAMESPACE()24 CORE_BEGIN_NAMESPACE()
25 /** Base type for named interfaces provided by plugin.
26  */
27 class IInterface {
28 public:
29     static constexpr BASE_NS::Uid UID { "00000000-0000-0000-0000-000000000000" };
30 
31     using Ptr = BASE_NS::refcnt_ptr<IInterface>;
32 
33     /** Access to provided sub-interfaces
34      */
35     virtual const IInterface* GetInterface(const BASE_NS::Uid& uid) const = 0;
36     virtual IInterface* GetInterface(const BASE_NS::Uid& uid) = 0;
37 
38     template<typename InterfaceType>
39     const InterfaceType* GetInterface() const
40     {
41         return static_cast<const InterfaceType*>(GetInterface(InterfaceType::UID));
42     }
43 
44     template<typename InterfaceType>
45     InterfaceType* GetInterface()
46     {
47         return static_cast<InterfaceType*>(GetInterface(InterfaceType::UID));
48     }
49 
50     /** Take a new reference of the object.
51      */
52     virtual void Ref() = 0;
53 
54     /* Releases one reference of the object.
55      * No methods of the class shall be called after unref.
56      * The object could be destroyed, if last reference
57      */
58     virtual void Unref() = 0;
59 
60 protected:
61     IInterface() = default;
62     virtual ~IInterface() = default;
63 };
64 CORE_END_NAMESPACE()
65 
66 #endif
67