MakeUnique.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_MAKEUNIQUE_HPP
30 #define AURORA_MAKEUNIQUE_HPP
31 
32 #include <memory>
33 
34 
35 namespace aurora
36 {
37 
38 // For documentation and modern compilers
39 #ifdef AURORA_HAS_VARIADIC_TEMPLATES
40 
43 
52 template <typename T, typename... Args>
53 std::unique_ptr<T> makeUnique(Args&&... args)
54 {
55  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
56 }
57 
59 
60 // ---------------------------------------------------------------------------------------------------------------------------
61 
62 
63 // Unoptimized fallback for compilers that don't support variadic templates, emulated by preprocessor metaprogramming
64 #else // AURORA_HAS_VARIADIC_TEMPLATES
65 
66 #include <Aurora/SmartPtr/Detail/Factories.hpp>
67 
68 // Define metafunction to generate overloads for aurora::CopiedPtr
69 #define AURORA_DETAIL_UNIQUEPTR_FACTORY(n) AURORA_DETAIL_SMARTPTR_FACTORY(std::unique_ptr, makeUnique, n)
70 
71 // Generate code
72 AURORA_PP_ENUMERATE(AURORA_PP_LIMIT, AURORA_DETAIL_UNIQUEPTR_FACTORY)
73 
74 #endif // AURORA_HAS_VARIADIC_TEMPLATES
75 
76 } // namespace aurora
77 
78 #endif // AURORA_MAKEUNIQUE_HPP
#define AURORA_PP_ENUMERATE(n, macro)
Apply a macro repeated times.
Definition: Preprocessor.hpp:155
#define AURORA_PP_LIMIT
Limit for preprocessor metaprogramming arguments.
Definition: Preprocessor.hpp:58
std::unique_ptr< T > makeUnique(Args &&...args)
Emplaces an object directly inside the unique pointer.
Definition: MakeUnique.hpp:53
Definition: DispatchTraits.hpp:39