-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpimpl.h
58 lines (45 loc) · 1.09 KB
/
pimpl.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
/**
* This header provides the opaque impl type
* and its pointer;
**/
#pragma once
#include <memory>
#include <utility>
template<typename T>
class pimpl {
public:
template<typename ...Args>
pimpl(Args&& ...);
pimpl();
~pimpl();
T* operator->();
T& operator*();
private:
std::unique_ptr<T> impl;
};
//**************************************************************
/**
* This constructor exists to provide a way to pass
* initialization values through to the impl object.
*
* Since pimpl itself knows nothing about the impl type,
* we use a constructor with a forwarding reference as
* an argument and then it’s perfect forwarding to the rescue!
**/
template<typename T>
template<typename ...Args>
pimpl<T>::pimpl(Args&& ...args)
: impl{std::make_unique<T>(std::forward<Args>(args)...)} {}
template<typename T>
pimpl<T>::pimpl()
: impl{std::make_unique<T>()} {}
template<typename T>
pimpl<T>::~pimpl() { }
template<typename T>
T* pimpl<T>::operator->() {
return m.get();
}
template<typename T>
T& pimpl<T>::operator*() {
return *m.get();
}