52bf6184bff6b113bf9d9f5dbbd3a95ef661f11f
[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,2018, 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  *
40  * \brief Provide uniform interface to selected C++ standard and
41  * popular library features.
42  *
43  * For some features not available on all platforms supported by
44  * \Gromacs, provides back-ports or mappings to available standard
45  * library implementations as appropriate.
46  *
47  * Backported implementations of features from newer C++ standards, or
48  * from community libraries such as Abseil or the Guidelines Support
49  * Library may also be considered for inclusion here. We generally
50  * expect that features included here are either already standard,
51  * proposed for the standard, or so useful in solving a frequently
52  * occuring problem that their use is not controversial.
53  */
54
55 /*!
56  * \file
57  * \brief Provides template gmx::compat::make_unique
58  *
59  * The implementation of gmx::compat::make_unique is copied with little
60  * modification from C++ standardization doc N3656
61  * at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3656.htm
62  * though additional wrapping has been added for use in \Gromacs.
63  *
64  * \author M. Eric Irrgang <ericirrgang@gmail.com>
65  * \ingroup module_compat
66  * \inlibraryapi
67  */
68 /*! \addtogroup module_compat
69  * ### gmx::compat::make_unique
70  *
71  * Provide a trivially adapted implementation of the C++ standard library `make_unique` function template.
72  * When All supported \Gromacs build platforms provide `std::make_unique`, this should be removed.
73  *
74  */
75 #include <cstddef>
76
77 #include <memory>
78 #include <type_traits>
79 #include <utility>
80
81 namespace gmx
82 {
83 namespace compat
84 {
85
86 ///\cond
87
88 // All gmx::compat code should use std::unique_ptr
89 using ::std::unique_ptr;
90
91 template<class T> struct Unique_if {
92     typedef unique_ptr<T> Single_object;
93 };
94
95 template<class T> struct Unique_if<T[]> {
96     typedef unique_ptr<T[]> Unknown_bound;
97 };
98
99 template<class T, size_t N> struct Unique_if<T[N]> {
100     typedef void Known_bound;
101 };
102
103 template<class T, class ... Args>
104 typename Unique_if<T>::Single_object
105 make_unique(Args && ... args)
106 {
107     return unique_ptr<T>(new T(::std::forward<Args>(args) ...));
108 }
109
110 template<class T>
111 typename Unique_if<T>::Unknown_bound
112 make_unique(size_t n)
113 {
114     typedef typename ::std::remove_extent<T>::type U;
115     return unique_ptr<T>(new U[n]());
116 }
117
118 template<class T, class ... Args>
119 typename Unique_if<T>::Known_bound
120 make_unique(Args && ...) = delete;
121
122 ///\endcond
123
124 }      // namespace compat
125 }      // namespace gmx
126
127 #endif // header guard