Dune Core Modules (unstable)

container.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_COMMON_CONCEPTS_CONTAINER_HH
6#define DUNE_COMMON_CONCEPTS_CONTAINER_HH
7
8// check whether c++20 concept can be used
9#if __has_include(<version>) && __has_include(<concepts>)
10 #include <version>
11 #if __cpp_concepts >= 201907L && __cpp_lib_concepts >= 202002L
12 #ifndef DUNE_ENABLE_CONCEPTS
13 #define DUNE_ENABLE_CONCEPTS 1
14 #endif
15 #endif
16#endif
17
18#if DUNE_ENABLE_CONCEPTS
19
20#include <concepts>
21#include <iterator>
22
23namespace Dune::Concept {
24
53template<class T>
54concept Container =
55 std::regular<T> &&
56 std::swappable<T> &&
57requires(T a, const T ca)
58{
59 typename T::value_type;
60 requires std::unsigned_integral<typename T::size_type>;
61 requires std::forward_iterator<typename T::iterator>;
62 requires std::forward_iterator<typename T::const_iterator>;
63 { a.begin() } -> std::same_as<typename T::iterator>;
64 { a.end() } -> std::same_as<typename T::iterator>;
65 { ca.begin() } -> std::same_as<typename T::const_iterator>;
66 { ca.end() } -> std::same_as<typename T::const_iterator>;
67 { a.cbegin() } -> std::same_as<typename T::const_iterator>;
68 { a.cend() } -> std::same_as<typename T::const_iterator>;
69 { a.size() } -> std::same_as<typename T::size_type>;
70 { a.max_size() } -> std::same_as<typename T::size_type>;
71 { a.empty() } -> std::convertible_to<bool>;
72};
73
95template<class T>
96concept RandomAccessContainer =
97 Container<T> &&
98requires(T a, const T ca, typename T::size_type i)
99{
100 requires std::same_as<typename T::reference, typename T::value_type&>;
101 requires std::same_as<typename T::const_reference, const typename T::value_type&>;
102 requires std::random_access_iterator<typename T::iterator>;
103 requires std::random_access_iterator<typename T::const_iterator>;
104 { a[i] } -> std::same_as<typename T::reference>;
105 { ca[i] } -> std::same_as<typename T::const_reference>;
106};
107
108} // end namespace Dune::Concept
109
110#endif // DUNE_ENABLE_CONCEPTS
111
112#endif // DUNE_COMMON_CONCEPTS_CONTAINER_HH
Namespace for concepts.
Definition: concept.hh:34
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)