-
Notifications
You must be signed in to change notification settings - Fork 0
/
classid.cpp
58 lines (42 loc) · 1.15 KB
/
classid.cpp
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
#include <iostream>
#include <string>
#define BEGIN_REQUEST( NAME ) \
struct NAME : public Base { \
static Key staticKey( ) { return details::IDGenerator< NAME >::key( ); } \
NAME( ) : Base( NAME::staticKey( ) ) { }
#define END_REQUEST };
using Key = void *;
namespace details {
template< typename T > struct IDGenerator {
static Key key( ) {
static IDGenerator< T > singleton;
return &singleton;
}
};
} // namespace details.
class Base {
public:
Base( const Base & ) = delete;
Base( Base && ) = delete;
Key key( ) const noexcept { return m_key; }
protected:
virtual ~Base( ) = default;
Base( Key k ) : m_key( k ) { }
private:
Key m_key;
};
BEGIN_REQUEST( Store )
std::string mediaID;
bool store;
END_REQUEST
BEGIN_REQUEST( Shutdown )
END_REQUEST
int main( ) {
std::cout << "Store::staticKey: " << Store::staticKey( ) << '\n';
std::cout << "Shutdown::staticKey: " << Shutdown::staticKey( ) << '\n';
Store store;
Shutdown shutdown;
std::cout << "Store::key( ): " << store.key( ) << '\n';
std::cout << "Shutdown::key( ): " << shutdown.key( ) << '\n';
return 0;
}