mdspan - basic accessor policy
[gromacs.git] / src / gromacs / mdspan / accessor_policy.h
blobd2520e23da1ebafe229d0bb87a3dabd601d6bdc8
1 /*
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 * This file is a modified version of original work of Sandia Corporation.
37 * In the spirit of the original code, this particular file can be distributed
38 * on the terms of Sandia Corporation.
41 * Kokkos v. 2.0
42 * Copyright (2014) Sandia Corporation
44 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
45 * the U.S. Government retains certain rights in this software.
47 * Kokkos is licensed under 3-clause BSD terms of use:
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions are
51 * met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
60 * 3. Neither the name of the Corporation nor the names of the
61 * contributors may be used to endorse or promote products derived from
62 * this software without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
65 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
68 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 * Questions? Contact Christian R. Trott (crtrott@sandia.gov)
78 /*! \libinternal \file
79 * \brief Declares accessor policies for mdspan.
81 * Implement ways how to convert a linear offset from a pointer to memory access.
82 * \author David Hollman <dshollm@sandia.gov>
83 * \author Christian Blau <cblau@gwdg.de>
84 * \libinternal
85 * \ingroup mdspan
87 #ifndef MDSPAN_ACCESSOR_POLICY_H
88 #define MDSPAN_ACCESSOR_POLICY_H
90 #include <cstddef>
92 namespace gmx
95 /*! \libinternal \brief The most basic memory access model for mdspan.
96 * \tparam ElementType the type held in memory to be accessed
98 template<class ElementType>
99 class accessor_basic
101 public:
102 //! Type of element to be accessed.
103 using element_type = ElementType;
104 //! Pointer to element to be accessed.
105 using pointer = ElementType*;
106 //! How to determine a memory offset, provided by self accessor.
107 using offset_policy = accessor_basic;
108 //! Type of references.
109 using reference = ElementType&;
111 /*! \brief Shift a pointer by an offset.
112 * \param[in] p Pointer to reference memory location.
113 * \param[in] i offset from memory location.
114 * \returns pointer to offset memory location.
116 constexpr typename offset_policy::pointer
117 offset( pointer p, ptrdiff_t i ) const noexcept
118 { return typename offset_policy::pointer(p+i); }
120 /*! \brief Access element from an offset to given pointer.
121 * \param[in] p Pointer to reference memory location.
122 * \param[in] i offset from memory location.
123 * \returns reference to element stored at offset from memory location.
125 constexpr reference access( pointer p, ptrdiff_t i ) const noexcept
126 { return p[i]; }
128 /*! \brief Decay pointer to pointer to ElementType.
129 * NOTE This function does nothing, because it is the trivial implementation of an accessor.
130 * \returns input pointer as pointer to ElementType
132 constexpr ElementType* decay( pointer p ) const noexcept
133 { return p; }
136 } // namespace gmx
137 #endif /* end of include guard: MDSPAN_ACCESSOR_POLICY_H */