2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 #ifndef GMX_COMPAT_MAKE_UNIQUE_H
36 #define GMX_COMPAT_MAKE_UNIQUE_H
38 * \defgroup module_compat C++ "standard" library compatibility helpers.
40 * \brief Provide uniform interface to selected C++ standard and
41 * popular library features.
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.
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.
57 * \brief Provides template gmx::compat::make_unique
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.
64 * \author M. Eric Irrgang <ericirrgang@gmail.com>
65 * \ingroup module_compat
68 /*! \addtogroup module_compat
69 * ### gmx::compat::make_unique
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.
78 #include <type_traits>
88 // All gmx::compat code should use std::unique_ptr
89 using ::std::unique_ptr;
91 template<class T> struct Unique_if {
92 typedef unique_ptr<T> Single_object;
95 template<class T> struct Unique_if<T[]> {
96 typedef unique_ptr<T[]> Unknown_bound;
99 template<class T, size_t N> struct Unique_if<T[N]> {
100 typedef void Known_bound;
103 template<class T, class ... Args>
104 typename Unique_if<T>::Single_object
105 make_unique(Args && ... args)
107 return unique_ptr<T>(new T(::std::forward<Args>(args) ...));
111 typename Unique_if<T>::Unknown_bound
112 make_unique(size_t n)
114 typedef typename ::std::remove_extent<T>::type U;
115 return unique_ptr<T>(new U[n]());
118 template<class T, class ... Args>
119 typename Unique_if<T>::Known_bound
120 make_unique(Args && ...) = delete;
124 } // namespace compat
127 #endif // header guard