2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
36 /*! \libinternal \file
37 * \brief Provides ported functions/classes from gsl/pointers
39 * Adapted from the Guidelines Support Library v2.0.0. at
40 * https://github.com/Microsoft/GSL
42 * \todo Replace comments referring to c++14-constexpr with constexpr
43 * when we require C++14.
45 * \author Mark Abraham <mark.j.abraham@gmail.com>
46 * \ingroup module_compat
49 #ifndef GMX_COMPAT_POINTERS_H
50 #define GMX_COMPAT_POINTERS_H
52 #include <type_traits>
55 #include "gromacs/utility/gmxassert.h"
62 //! Contract-assurance macros that work like a simple version of the GSL ones
64 #if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
65 #define Expects(cond) GMX_ASSERT((cond), "Precondition violation")
66 #define Ensures(cond) GMX_ASSERT((cond), "Postcondition violation")
68 // icc 18.0.0 in a RelWithAssert build has an ICE, even if we directly
69 // embed the contents of GMX_ASSERT, so it seems the lambda in
70 // GMX_ASSERT is too complex for it in this use case.
77 * \brief Restricts a pointer or smart pointer to only hold non-null values.
79 * Has zero size overhead over T.
81 * If T is a pointer (i.e. T == U*) then
82 * - allow construction from U*
83 * - disallow construction from nullptr_t
84 * - disallow default construction
85 * - ensure construction from null U* fails (only in debug builds)
86 * - allow implicit conversion to U*
88 * \todo Eliminate this when we require a version of C++ that supports
95 static_assert(std::is_assignable<T &, std::nullptr_t>::value, "T cannot be assigned nullptr.");
97 //! Move constructor. Asserts in debug mode if \c is nullptr.
98 template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type >
99 /*c++14-constexpr*/ explicit not_null(U &&u) : ptr_(std::forward<U>(u))
101 Expects(ptr_ != nullptr);
104 //! Simple constructor. Asserts in debug mode if \c u is nullptr.
105 template <typename = typename std::enable_if<!std::is_same<std::nullptr_t, T>::value>::type >
106 /*c++14-constexpr*/ explicit not_null(T u) : ptr_(u)
108 Expects(ptr_ != nullptr);
111 //! Copy constructor.
112 template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type >
113 constexpr not_null(const not_null<U> &other) : not_null(other.get())
117 //! Default constructors and assignment.
119 not_null(not_null &&other) noexcept = default;
120 not_null(const not_null &other) = default;
121 not_null &operator=(const not_null &other) = default;
126 /*c++14-constexpr*/ T get() const
128 Ensures(ptr_ != nullptr);
132 constexpr operator T() const { return get(); }
133 constexpr T operator->() const { return get(); }
136 //! Deleted to prevent compilation when someone attempts to assign a null pointer constant.
138 not_null(std::nullptr_t) = delete;
139 not_null &operator=(std::nullptr_t) = delete;
142 //! Deleted unwanted operators because pointers only point to single objects.
144 not_null &operator++() = delete;
145 not_null &operator--() = delete;
146 not_null operator++(int) = delete;
147 not_null operator--(int) = delete;
148 not_null &operator+=(std::ptrdiff_t) = delete;
149 not_null &operator-=(std::ptrdiff_t) = delete;
150 void operator[](std::ptrdiff_t) const = delete;
157 //! Convenience function for making not_null pointers.
159 not_null<T> make_not_null(T &&t)
161 return not_null < typename std::remove_cv < typename std::remove_reference<T>::type>::type >{
166 //! Operators to compare not_null pointers.
168 template <class T, class U>
169 auto operator==(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() == rhs.get())
171 return lhs.get() == rhs.get();
174 template <class T, class U>
175 auto operator!=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() != rhs.get())
177 return lhs.get() != rhs.get();
180 template <class T, class U>
181 auto operator<(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() < rhs.get())
183 return lhs.get() < rhs.get();
186 template <class T, class U>
187 auto operator<=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() <= rhs.get())
189 return lhs.get() <= rhs.get();
192 template <class T, class U>
193 auto operator>(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() > rhs.get())
195 return lhs.get() > rhs.get();
198 template <class T, class U>
199 auto operator>=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() >= rhs.get())
201 return lhs.get() >= rhs.get();
205 //! Deleted unwanted arithmetic operators.
207 template <class T, class U>
208 std::ptrdiff_t operator-(const not_null<T> &, const not_null<U> &) = delete;
210 not_null<T> operator-(const not_null<T> &, std::ptrdiff_t) = delete;
212 not_null<T> operator+(const not_null<T> &, std::ptrdiff_t) = delete;
214 not_null<T> operator+(std::ptrdiff_t, const not_null<T> &) = delete;
217 } // namespace compat