2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020,2021, 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 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 * \ingroup module_compat
46 #ifndef GMX_COMPAT_POINTERS_H
47 #define GMX_COMPAT_POINTERS_H
49 #include <type_traits>
52 #include "gromacs/utility/gmxassert.h"
59 //! Contract-assurance macros that work like a simple version of the GSL ones
61 #define Expects(cond) GMX_ASSERT((cond), "Precondition violation")
62 #define Ensures(cond) GMX_ASSERT((cond), "Postcondition violation")
66 * \brief Restricts a pointer or smart pointer to only hold non-null values.
68 * Has zero size overhead over T.
70 * If T is a pointer (i.e. T == U*) then
71 * - allow construction from U*
72 * - disallow construction from nullptr_t
73 * - disallow default construction
74 * - ensure construction from null U* fails (only in debug builds)
75 * - allow implicit conversion to U*
77 * \todo Eliminate this when we require a version of C++ that supports
84 static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
86 //! Move constructor. Asserts in debug mode if \c is nullptr.
87 template<typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
88 constexpr explicit not_null(U&& u) : ptr_(std::forward<U>(u))
90 Expects(ptr_ != nullptr);
93 //! Simple constructor. Asserts in debug mode if \c u is nullptr.
94 template<typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
95 constexpr explicit not_null(T u) : ptr_(u)
97 Expects(ptr_ != nullptr);
100 //! Copy constructor.
101 template<typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
102 constexpr not_null(const not_null<U>& other) : not_null(other.get())
106 //! Default constructors and assignment.
108 not_null(not_null&& other) noexcept = default;
109 not_null(const not_null& other) = default;
110 not_null& operator=(const not_null& other) = default;
115 constexpr T get() const
117 Ensures(ptr_ != nullptr);
121 constexpr operator T() const { return get(); }
122 constexpr T operator->() const { return get(); }
125 //! Deleted to prevent compilation when someone attempts to assign a null pointer constant.
127 not_null(std::nullptr_t) = delete;
128 not_null& operator=(std::nullptr_t) = delete;
131 //! Deleted unwanted operators because pointers only point to single objects.
133 not_null& operator++() = delete;
134 not_null& operator--() = delete;
135 not_null operator++(int) = delete;
136 not_null operator--(int) = delete;
137 not_null& operator+=(std::ptrdiff_t) = delete;
138 not_null& operator-=(std::ptrdiff_t) = delete;
139 void operator[](std::ptrdiff_t) const = delete;
146 //! Convenience function for making not_null pointers from plain pointers.
148 not_null<T> make_not_null(T&& t)
150 return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{ std::forward<T>(t) };
153 //! Convenience function for making not_null pointers from smart pointers.
155 not_null<typename T::pointer> make_not_null(T& t)
157 return not_null<typename std::remove_reference_t<T>::pointer>{ t.get() };
160 //! Operators to compare not_null pointers.
162 template<class T, class U>
163 auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
165 return lhs.get() == rhs.get();
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();
199 //! Deleted unwanted arithmetic operators.
201 template<class T, class U>
202 std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
204 not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
206 not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
208 not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
211 } // namespace compat