1/*
2 * Copyright (c) 2021 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 INF_CAST_IMPL_H
17#define INF_CAST_IMPL_H
18
19#include "utils/aie_macros.h"
20
21namespace OHOS {
22namespace AI {
23template<class I, class C>
24class InfCastImpl {
25public:
26    /**
27     * Interface class implement create class pointer function.
28     *
29     * @return I* Pointer of the class.
30     */
31    static I* Create()
32    {
33        C *p = nullptr;
34        AIE_NEW(p, C);
35        CHK_RET(p == nullptr, nullptr);
36        return reinterpret_cast<I*>(p);
37    }
38
39    /**
40     * Interface class implement create class pointer function.
41     *
42     * @param [in] type The int type param for the class.
43     * @return I* Pointer of the class.
44     */
45    static I* Create(int type)
46    {
47        C *p = nullptr;
48        AIE_NEW(p, C(type));
49        CHK_RET(p == nullptr, nullptr);
50        return reinterpret_cast<I*>(p);
51    }
52
53    /**
54     * Interface class implement create class pointer function.
55     *
56     * @param [in] t The template type param for the class.
57     * @return I* Pointer of the class.
58     */
59    template<class T>
60    static I* Create(T& t)
61    {
62        C *p = nullptr;
63        AIE_NEW(p, C(t));
64        CHK_RET(p == nullptr, nullptr);
65        return reinterpret_cast<I*>(p);
66    }
67
68    /**
69     * Interface class implement create class pointer function.
70     *
71     * @param [in] T1 The first template type param for the class.
72     * @param [in] T2 The second template type param for the class.
73     * @return I* Pointer of the class.
74     */
75    template<class T1, class T2>
76    static I* Create(T1& t1, T2& t2)
77    {
78        C *p = nullptr;
79        AIE_NEW(p, C(t1, t2));
80        CHK_RET(p == nullptr, nullptr);
81        return reinterpret_cast<I*>(p);
82    }
83
84    /**
85     * Interface class implement create class pointer function.
86     *
87     * @param [in] T1 The first template type param for the class.
88     * @param [in] T2 The second template type param for the class.
89     * @param [in] T3 The third template type param for the class.
90     * @return I* Pointer of the class.
91     */
92    template<class T1, class T2, class T3>
93    static I* Create(T1& t1, T2& t2, T3& t3)
94    {
95        C *p = nullptr;
96        AIE_NEW(p, C(t1, t2, t3));
97        CHK_RET(p == nullptr, nullptr);
98        return reinterpret_cast<I*>(p);
99    }
100
101    /**
102     * Interface class implement delete class pointer, and set the pointer to nullptr.
103     */
104    static void Destroy(I*& pi)
105    {
106        C *pc = reinterpret_cast<C*>(pi);
107        AIE_DELETE(pc);
108        pi = nullptr;
109    }
110
111    /**
112     * Convert The I* pointer to C*.
113     *
114     * @return C* pointer of the class.
115     */
116    static C& Ref(I* p)
117    {
118        return *reinterpret_cast<C*>(p);
119    }
120
121    static const C& Ref(const I* p)
122    {
123        return *reinterpret_cast<const C*>(p);
124    }
125};
126
127#define DEFINE_IMPL_CLASS_CAST(CastClass, InterfaceClass, ImplementationClass) \
128class CastClass : public InfCastImpl<InterfaceClass, ImplementationClass> {};
129} // namespace AI
130} // namespace OHOS
131
132#endif // INF_CAST_IMPL_H