forked from Greedysky/TTKDownloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttkprivate.h
81 lines (61 loc) · 2.38 KB
/
ttkprivate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef TTKPRIVATE_H
#define TTKPRIVATE_H
/* =================================================
* This file is part of the TTK Downloader project
* Copyright (C) 2015 - 2020 Greedysky Studio
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; If not, see <http://www.gnu.org/licenses/>.
================================================= */
#define TTK_DECLARE_PRIVATE(Class) \
friend class Class##Private; \
TTKPrivateInterface<Class, Class##Private> ttk_d;
#define TTK_DECLARE_PUBLIC(Class) \
friend class Class;
#define TTK_INIT_PRIVATE \
ttk_d.setPublic(this);
#define TTK_INIT_PUBLIC(Class) \
ttk_d.setPrivate(new Class##Private);
#define TTK_D(Class) Class##Private *const d = static_cast<Class##Private *>(ttk_d())
#define TTK_Q(Class) Class *const q = static_cast<Class *>(ttk_q())
template <typename PUB>
/*! @brief The class of the ttk private base.
* @author Greedysky <[email protected]>
*/
class TTKPrivate
{
public:
TTKPrivate() { ttk_q_ptr = nullptr; }
virtual ~TTKPrivate() { }
inline void setPublic(PUB* pub) { ttk_q_ptr = pub; }
protected:
inline PUB *ttk_q() const { return ttk_q_ptr; }
private:
PUB* ttk_q_ptr;
};
template <typename PUB, typename PVT>
/*! @brief The class of the ttk private interface.
* @author Greedysky <[email protected]>
*/
class TTKPrivateInterface
{
friend class TTKPrivate<PUB>;
public:
TTKPrivateInterface() { pvt_ptr = new PVT; }
~TTKPrivateInterface() { delete pvt_ptr; }
inline void setPrivate(PVT* pvt) { delete pvt_ptr; pvt_ptr = pvt; }
inline void setPublic(PUB* pub) { pvt_ptr->setPublic(pub); }
inline PVT *operator()() const { return static_cast<PVT*>(pvt_ptr); }
private:
TTKPrivateInterface(const TTKPrivateInterface&) { }
TTKPrivateInterface& operator=(const TTKPrivateInterface&) { }
TTKPrivate<PUB>* pvt_ptr;
};
#endif // TTKPRIVATE_H