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.
39 * \brief Provide uniform interface to selected C++ standard library features.
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.
48 * \brief Provides template gmx::compat::make_unique
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.
55 * \author M. Eric Irrgang <ericirrgang@gmail.com>
56 * \ingroup module_compat
59 /*! \addtogroup module_compat
60 * ### gmx::compat::make_unique
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.
69 #include <type_traits>
79 // All gmx::compat code should use std::unique_ptr
80 using ::std::unique_ptr;
82 template<class T> struct Unique_if {
83 typedef unique_ptr<T> Single_object;
86 template<class T> struct Unique_if<T[]> {
87 typedef unique_ptr<T[]> Unknown_bound;
90 template<class T, size_t N> struct Unique_if<T[N]> {
91 typedef void Known_bound;
94 template<class T, class ... Args>
95 typename Unique_if<T>::Single_object
96 make_unique(Args && ... args)
98 return unique_ptr<T>(new T(::std::forward<Args>(args) ...));
102 typename Unique_if<T>::Unknown_bound
103 make_unique(size_t n)
105 typedef typename ::std::remove_extent<T>::type U;
106 return unique_ptr<T>(new U[n]());
109 template<class T, class ... Args>
110 typename Unique_if<T>::Known_bound
111 make_unique(Args && ...) = delete;
115 } // namespace compat
118 #endif // header guard