KiCad PCB EDA Suite
make_unique.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
30 #ifndef MAKE_UNIQUE_H
31 #define MAKE_UNIQUE_H
32 
33 // Define std::make_unique if the compiler is C++11, but not C++14
34 // (which provides this itself)
35 // When C++14 support is a KiCad pre-requisite, this entire file
36 // can be removed.
37 #if __cplusplus == 201103L
38 
39 #include <cstddef>
40 #include <memory>
41 #include <type_traits>
42 #include <utility>
43 
44 // It's a bit naughty to add things to std::, but the point is to
45 // "polyfill" this function in only if std:: doesn't have it
46 //
47 // This implementation is the one proposed by Stephan T. Lavavej
48 // in N3656: https://isocpp.org/files/papers/N3656.txt
49 // This is more or less exactly how it is implemented in GCC (e.g. 6.3.1)
50 // when C++14 is enabled.
51 namespace std
52 {
53  template<class T> struct _Unique_if {
54  typedef unique_ptr<T> _Single_object;
55  };
56 
57  template<class T> struct _Unique_if<T[]> {
58  typedef unique_ptr<T[]> _Unknown_bound;
59  };
60 
61  template<class T, size_t N> struct _Unique_if<T[N]> {
62  typedef void _Known_bound;
63  };
64 
66  template<class T, class... Args>
67  typename _Unique_if<T>::_Single_object
68  make_unique(Args&&... args) {
69  return unique_ptr<T>(new T(std::forward<Args>(args)...));
70  }
71 
73  template<class T>
74  typename _Unique_if<T>::_Unknown_bound
75  make_unique(size_t n) {
76  typedef typename remove_extent<T>::type U;
77  return unique_ptr<T>(new U[n]());
78  }
79 
81  template<class T, class... Args>
82  typename _Unique_if<T>::_Known_bound
83  make_unique(Args&&...) = delete;
84 }
85 
86 #endif // __cplusplus == 201103L
87 
88 #endif // WXSTRUCT_H_
T
enum T contains all this lexer's tokens.