openMSX
memory.hh
Go to the documentation of this file.
1 #ifndef MEMORY_HH
2 #define MEMORY_HH
3 
4 #include <memory>
5 #include <utility>
6 
7 // See this blog for a motivation for the make_unique() function:
8 // http://herbsutter.com/gotw/_102/
9 // This function will almost certainly be added in the next revision of the
10 // c++ language.
11 
12 
13 // The above blog also gives an implementation for make_unique(). Unfortunately
14 // vs2012 doesn't support variadic templates yet, so for now we have to use
15 // the longer (and less general) version below.
16 #if 0
17 template<typename T, typename ...Args>
18 std::unique_ptr<T> make_unique(Args&& ...args)
19 {
20  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
21 }
22 #else
23 
24 // Emulate variadic templates for upto 13 arguments (yes we need a version
25 // with 13 parameters!).
26 template<typename T>
27 std::unique_ptr<T> make_unique()
28 {
29  return std::unique_ptr<T>(new T());
30 }
31 
32 template<typename T, typename P1>
33 std::unique_ptr<T> make_unique(P1&& p1)
34 {
35  return std::unique_ptr<T>(new T(
36  std::forward<P1>(p1)));
37 }
38 
39 template<typename T, typename P1, typename P2>
40 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2)
41 {
42  return std::unique_ptr<T>(new T(
43  std::forward<P1>(p1), std::forward<P2>(p2)));
44 }
45 
46 template<typename T, typename P1, typename P2, typename P3>
47 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3)
48 {
49  return std::unique_ptr<T>(new T(
50  std::forward<P1>(p1), std::forward<P2>(p2),
51  std::forward<P3>(p3)));
52 }
53 
54 template<typename T, typename P1, typename P2, typename P3, typename P4>
55 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4)
56 {
57  return std::unique_ptr<T>(new T(
58  std::forward<P1>(p1), std::forward<P2>(p2),
59  std::forward<P3>(p3), std::forward<P4>(p4)));
60 }
61 
62 template<typename T, typename P1, typename P2, typename P3, typename P4,
63  typename P5>
64 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
65 {
66  return std::unique_ptr<T>(new T(
67  std::forward<P1>(p1), std::forward<P2>(p2),
68  std::forward<P3>(p3), std::forward<P4>(p4),
69  std::forward<P5>(p5)));
70 }
71 
72 template<typename T, typename P1, typename P2, typename P3, typename P4,
73  typename P5, typename P6>
74 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
75  P6&& p6)
76 {
77  return std::unique_ptr<T>(new T(
78  std::forward<P1>(p1), std::forward<P2>(p2),
79  std::forward<P3>(p3), std::forward<P4>(p4),
80  std::forward<P5>(p5), std::forward<P6>(p6)));
81 }
82 
83 template<typename T, typename P1, typename P2, typename P3, typename P4,
84  typename P5, typename P6, typename P7>
85 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
86  P6&& p6, P7&& p7)
87 {
88  return std::unique_ptr<T>(new T(
89  std::forward<P1>(p1), std::forward<P2>(p2),
90  std::forward<P3>(p3), std::forward<P4>(p4),
91  std::forward<P5>(p5), std::forward<P6>(p6),
92  std::forward<P7>(p7)));
93 }
94 
95 template<typename T, typename P1, typename P2, typename P3, typename P4,
96  typename P5, typename P6, typename P7, typename P8>
97 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
98  P6&& p6, P7&& p7, P8&& p8)
99 {
100  return std::unique_ptr<T>(new T(
101  std::forward<P1>(p1), std::forward<P2>(p2),
102  std::forward<P3>(p3), std::forward<P4>(p4),
103  std::forward<P5>(p5), std::forward<P6>(p6),
104  std::forward<P7>(p7), std::forward<P8>(p8)));
105 }
106 
107 template<typename T, typename P1, typename P2, typename P3, typename P4,
108  typename P5, typename P6, typename P7, typename P8,
109  typename P9>
110 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
111  P6&& p6, P7&& p7, P8&& p8, P9&& p9)
112 {
113  return std::unique_ptr<T>(new T(
114  std::forward<P1>(p1), std::forward<P2>(p2),
115  std::forward<P3>(p3), std::forward<P4>(p4),
116  std::forward<P5>(p5), std::forward<P6>(p6),
117  std::forward<P7>(p7), std::forward<P8>(p8),
118  std::forward<P9>(p9)));
119 }
120 
121 template<typename T, typename P1, typename P2, typename P3, typename P4,
122  typename P5, typename P6, typename P7, typename P8,
123  typename P9, typename P10>
124 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
125  P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10)
126 {
127  return std::unique_ptr<T>(new T(
128  std::forward<P1>(p1), std::forward<P2 >(p2 ),
129  std::forward<P3>(p3), std::forward<P4 >(p4 ),
130  std::forward<P5>(p5), std::forward<P6 >(p6 ),
131  std::forward<P7>(p7), std::forward<P8 >(p8 ),
132  std::forward<P9>(p9), std::forward<P10>(p10)));
133 }
134 
135 template<typename T, typename P1, typename P2, typename P3, typename P4,
136  typename P5, typename P6, typename P7, typename P8,
137  typename P9, typename P10, typename P11>
138 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
139  P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10,
140  P11&& p11)
141 {
142  return std::unique_ptr<T>(new T(
143  std::forward<P1 >(p1 ), std::forward<P2 >(p2 ),
144  std::forward<P3 >(p3 ), std::forward<P4 >(p4 ),
145  std::forward<P5 >(p5 ), std::forward<P6 >(p6 ),
146  std::forward<P7 >(p7 ), std::forward<P8 >(p8 ),
147  std::forward<P9 >(p9 ), std::forward<P10>(p10),
148  std::forward<P11>(p11)));
149 }
150 
151 template<typename T, typename P1, typename P2, typename P3, typename P4,
152  typename P5, typename P6, typename P7, typename P8,
153  typename P9, typename P10, typename P11, typename P12>
154 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
155  P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10,
156  P11&& p11, P12&& p12)
157 {
158  return std::unique_ptr<T>(new T(
159  std::forward<P1 >(p1 ), std::forward<P2 >(p2 ),
160  std::forward<P3 >(p3 ), std::forward<P4 >(p4 ),
161  std::forward<P5 >(p5 ), std::forward<P6 >(p6 ),
162  std::forward<P7 >(p7 ), std::forward<P8 >(p8 ),
163  std::forward<P9 >(p9 ), std::forward<P10>(p10),
164  std::forward<P11>(p11), std::forward<P12>(p12)));
165 }
166 
167 template<typename T, typename P1, typename P2, typename P3, typename P4,
168  typename P5, typename P6, typename P7, typename P8,
169  typename P9, typename P10, typename P11, typename P12,
170  typename P13>
171 std::unique_ptr<T> make_unique(P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5,
172  P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10,
173  P11&& p11, P12&& p12, P13&& p13)
174 {
175  return std::unique_ptr<T>(new T(
176  std::forward<P1 >(p1 ), std::forward<P2 >(p2 ),
177  std::forward<P3 >(p3 ), std::forward<P4 >(p4 ),
178  std::forward<P5 >(p5 ), std::forward<P6 >(p6 ),
179  std::forward<P7 >(p7 ), std::forward<P8 >(p8 ),
180  std::forward<P9 >(p9 ), std::forward<P10>(p10),
181  std::forward<P11>(p11), std::forward<P12>(p12),
182  std::forward<P13>(p13)));
183 }
184 
185 #endif
186 
187 #endif
std::unique_ptr< T > make_unique()
Definition: memory.hh:27