2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, 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 #if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
62 #define Expects(cond) GMX_ASSERT((cond), "Precondition violation")
63 #define Ensures(cond) GMX_ASSERT((cond), "Postcondition violation")
65 // icc 18.0.0 in a RelWithAssert build has an ICE, even if we directly
66 // embed the contents of GMX_ASSERT, so it seems the lambda in
67 // GMX_ASSERT is too complex for it in this use case.
74 * \brief Restricts a pointer or smart pointer to only hold non-null values.
76 * Has zero size overhead over T.
78 * If T is a pointer (i.e. T == U*) then
79 * - allow construction from U*
80 * - disallow construction from nullptr_t
81 * - disallow default construction
82 * - ensure construction from null U* fails (only in debug builds)
83 * - allow implicit conversion to U*
85 * \todo Eliminate this when we require a version of C++ that supports
92 static_assert(std::is_assignable
<T
&, std::nullptr_t
>::value
, "T cannot be assigned nullptr.");
94 //! Move constructor. Asserts in debug mode if \c is nullptr.
95 template <typename U
, typename
= typename
std::enable_if
<std::is_convertible
<U
, T
>::value
>::type
>
96 constexpr explicit not_null(U
&&u
) : ptr_(std::forward
<U
>(u
))
98 Expects(ptr_
!= nullptr);
101 //! Simple constructor. Asserts in debug mode if \c u is nullptr.
102 template <typename
= typename
std::enable_if
<!std::is_same
<std::nullptr_t
, T
>::value
>::type
>
103 constexpr explicit not_null(T u
) : ptr_(u
)
105 Expects(ptr_
!= nullptr);
108 //! Copy constructor.
109 template <typename U
, typename
= typename
std::enable_if
<std::is_convertible
<U
, T
>::value
>::type
>
110 constexpr not_null(const not_null
<U
> &other
) : not_null(other
.get())
114 //! Default constructors and assignment.
116 not_null(not_null
&&other
) noexcept
= default;
117 not_null(const not_null
&other
) = default;
118 not_null
&operator=(const not_null
&other
) = default;
123 constexpr T
get() const
125 Ensures(ptr_
!= nullptr);
129 constexpr operator T() const { return get(); }
130 constexpr T
operator->() const { return get(); }
133 //! Deleted to prevent compilation when someone attempts to assign a null pointer constant.
135 not_null(std::nullptr_t
) = delete;
136 not_null
&operator=(std::nullptr_t
) = delete;
139 //! Deleted unwanted operators because pointers only point to single objects.
141 not_null
&operator++() = delete;
142 not_null
&operator--() = delete;
143 not_null
operator++(int) = delete;
144 not_null
operator--(int) = delete;
145 not_null
&operator+=(std::ptrdiff_t) = delete;
146 not_null
&operator-=(std::ptrdiff_t) = delete;
147 void operator[](std::ptrdiff_t) const = delete;
154 //! Convenience function for making not_null pointers from plain pointers.
159 return not_null
< typename
std::remove_cv
< typename
std::remove_reference
<T
>::type
>::type
>{
164 //! Convenience function for making not_null pointers from smart pointers.
166 not_null
<typename
T::pointer
>
169 return not_null
< typename
std::remove_reference
<T
>::type::pointer
>{
174 //! Operators to compare not_null pointers.
176 template <class T
, class U
>
177 auto operator==(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() == rhs
.get())
179 return lhs
.get() == rhs
.get();
182 template <class T
, class U
>
183 auto operator!=(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() != rhs
.get())
185 return lhs
.get() != rhs
.get();
188 template <class T
, class U
>
189 auto operator<(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() < rhs
.get())
191 return lhs
.get() < rhs
.get();
194 template <class T
, class U
>
195 auto operator<=(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() <= rhs
.get())
197 return lhs
.get() <= rhs
.get();
200 template <class T
, class U
>
201 auto operator>(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() > rhs
.get())
203 return lhs
.get() > rhs
.get();
206 template <class T
, class U
>
207 auto operator>=(const not_null
<T
> &lhs
, const not_null
<U
> &rhs
)->decltype(lhs
.get() >= rhs
.get())
209 return lhs
.get() >= rhs
.get();
213 //! Deleted unwanted arithmetic operators.
215 template <class T
, class U
>
216 std::ptrdiff_t operator-(const not_null
<T
> &, const not_null
<U
> &) = delete;
218 not_null
<T
> operator-(const not_null
<T
> &, std::ptrdiff_t) = delete;
220 not_null
<T
> operator+(const not_null
<T
> &, std::ptrdiff_t) = delete;
222 not_null
<T
> operator+(std::ptrdiff_t, const not_null
<T
> &) = delete;
225 } // namespace compat