2d89df503dec69f08d22e633ef0229ac34ef65b0
[alexxy/gromacs.git] / src / gromacs / compat / make_unique.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #ifndef GMX_COMPAT_MAKE_UNIQUE_H
36 #define GMX_COMPAT_MAKE_UNIQUE_H
37 /*! \libinternal
38  * \defgroup module_compat C++ standard library compatibility helpers.
39  * \brief Provide uniform interface to selected C++ standard library features.
40  *
41  * For some features not available on all platforms supported by
42  * \Gromacs, provide back-ports or mappings to available standard
43  * library implementations as appropriate.
44  */
45
46 /*! \libinternal
47  * \file
48  * \brief Provides template gmx::compat::make_unique
49  *
50  * The implementation of gmx::compat::make_unique is copied with little
51  * modification from C++ standardization doc N3656
52  * at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3656.htm
53  * though additional wrapping has been added for use in \Gromacs.
54  *
55  * \author M. Eric Irrgang <ericirrgang@gmail.com>
56  * \ingroup module_compat
57  * \inlibraryapi
58  */
59 /*! \addtogroup module_compat
60  * ### gmx::compat::make_unique
61  *
62  * Provide a trivially adapted implementation of the C++ standard library `make_unique` function template.
63  * When All supported \Gromacs build platforms provide `std::make_unique`, this should be removed.
64  *
65  */
66 #include <cstddef>
67
68 #include <memory>
69 #include <type_traits>
70 #include <utility>
71
72 namespace gmx
73 {
74 namespace compat
75 {
76
77 ///\cond
78
79 // All gmx::compat code should use std::unique_ptr
80 using ::std::unique_ptr;
81
82 template<class T> struct Unique_if {
83     typedef unique_ptr<T> Single_object;
84 };
85
86 template<class T> struct Unique_if<T[]> {
87     typedef unique_ptr<T[]> Unknown_bound;
88 };
89
90 template<class T, size_t N> struct Unique_if<T[N]> {
91     typedef void Known_bound;
92 };
93
94 template<class T, class ... Args>
95 typename Unique_if<T>::Single_object
96 make_unique(Args && ... args)
97 {
98     return unique_ptr<T>(new T(::std::forward<Args>(args) ...));
99 }
100
101 template<class T>
102 typename Unique_if<T>::Unknown_bound
103 make_unique(size_t n)
104 {
105     typedef typename ::std::remove_extent<T>::type U;
106     return unique_ptr<T>(new U[n]());
107 }
108
109 template<class T, class ... Args>
110 typename Unique_if<T>::Known_bound
111 make_unique(Args && ...) = delete;
112
113 ///\endcond
114
115 }      // namespace gmx::compat
116 }      // namespace gmx
117
118 #endif // header guard