vecmem 1.18.0
Loading...
Searching...
No Matches
view.ipp
1/* VecMem project, part of the ACTS project (R&D line)
2 *
3 * (c) 2023-2025 CERN for the benefit of the ACTS project
4 *
5 * Mozilla Public License Version 2.0
6 */
7#pragma once
8
9// Local include(s).
10#include "vecmem/edm/details/schema_traits.hpp"
11#include "vecmem/utils/type_traits.hpp"
12
13// System include(s).
14#include <cassert>
15
16namespace vecmem {
17namespace edm {
18
19template <typename... VARTYPES>
20VECMEM_HOST_AND_DEVICE view<schema<VARTYPES...>>::view(
21 size_type capacity, const memory_view_type& size)
22 : m_capacity(capacity),
23 m_views{},
24 m_size{size},
25 m_payload{0, nullptr},
26 m_layout{0, nullptr},
27 m_host_layout{0, nullptr} {}
28
29template <typename... VARTYPES>
30template <typename... OTHERTYPES,
31 std::enable_if_t<
32 vecmem::details::conjunction_v<std::is_constructible<
35 vecmem::details::disjunction_v<
36 vecmem::details::negation<std::is_same<
39 bool>>
40VECMEM_HOST_AND_DEVICE view<schema<VARTYPES...>>::view(
42 : m_capacity{other.capacity()},
43 m_views{other.variables()},
44 m_size{other.size()},
45 m_payload{other.payload()},
46 m_layout{other.layout()},
47 m_host_layout{other.host_layout()} {}
48
49template <typename... VARTYPES>
50template <typename... OTHERTYPES,
51 std::enable_if_t<
52 vecmem::details::conjunction_v<std::is_constructible<
55 vecmem::details::disjunction_v<
56 vecmem::details::negation<std::is_same<
59 bool>>
60VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::operator=(
61 const view<schema<OTHERTYPES...>>& rhs) -> view& {
62
63 // Note that self-assignment with this function should never be a thing.
64 // So we don't need to check for it in production code.
65 assert(static_cast<const void*>(this) != static_cast<const void*>(&rhs));
66
67 // Copy the data from the other view.
68 m_capacity = rhs.capacity();
69 m_views = rhs.variables();
70 m_size = rhs.size();
71 m_payload = rhs.payload();
72 m_layout = rhs.layout();
73 m_host_layout = rhs.host_layout();
74
75 // Return a reference to this object.
76 return *this;
77}
78
79template <typename... VARTYPES>
80VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::capacity() const
81 -> size_type {
82
83 return m_capacity;
84}
85
86template <typename... VARTYPES>
87template <std::size_t INDEX>
88VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::get()
90
91 return vecmem::get<INDEX>(m_views);
92}
93
94template <typename... VARTYPES>
95template <std::size_t INDEX>
96VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::get() const
97 -> const tuple_element_t<INDEX, tuple_type>& {
98
99 return vecmem::get<INDEX>(m_views);
100}
101
102template <typename... VARTYPES>
103VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::variables()
104 -> tuple_type& {
105
106 return m_views;
107}
108
109template <typename... VARTYPES>
110VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::variables() const
111 -> const tuple_type& {
112
113 return m_views;
114}
115
116template <typename... VARTYPES>
117VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::size() const
119
120 return m_size;
121}
122
123template <typename... VARTYPES>
124VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::payload() const
126
127 return m_payload;
128}
129
130template <typename... VARTYPES>
131VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::layout() const
133
134 return m_layout;
135}
136
137template <typename... VARTYPES>
138VECMEM_HOST_AND_DEVICE auto view<schema<VARTYPES...>>::host_layout() const
140
141 return m_host_layout;
142}
143
144namespace details {
145
146template <std::size_t INDEX, typename... VARTYPES>
148
149 VECMEM_HOST
150 static std::vector<vecmem::data::vector_view<int>::size_type> get(
151 const view<schema<VARTYPES...>>& soa) {
152
153 return get_impl(soa, soa.template get<INDEX>());
154 }
155
156private:
157 template <typename T>
158 VECMEM_HOST static std::vector<vecmem::data::vector_view<int>::size_type>
159 get_impl(const view<schema<VARTYPES...>>& soa, const T&) {
160
162 }
163
164 template <typename T>
165 VECMEM_HOST static std::vector<vecmem::data::vector_view<int>::size_type>
166 get_impl(const view<schema<VARTYPES...>>&,
168
170 }
171
172}; // struct get_capacities_impl
173
174template <typename... VARTYPES>
176
177 VECMEM_HOST
178 static std::vector<vecmem::data::vector_view<int>::size_type> get(
179 const view<schema<VARTYPES...>>& soa) {
180
181 // If we got this far, this *must* be a jagged vector.
182 static_assert(
184 tuple_element_t<0u, tuple<VARTYPES...>>>::value,
185 "The first variable in the schema must be a jagged vector");
186
187 // Get the capacities with the helper function available for jagged
188 // vectors.
189 return vecmem::data::get_capacities(soa.template get<0>());
190 }
191
192}; // struct get_capacities_impl
193
194} // namespace details
195
196template <typename... VARTYPES>
197VECMEM_HOST std::vector<vecmem::data::vector_view<int>::size_type>
199
200 // Make sure that there's a jagged vector in here.
201 static_assert(
203 "Function can only be used on containers with jagged vectors");
204
205 return details::get_capacities_impl<sizeof...(VARTYPES) - 1,
206 VARTYPES...>::get(soa);
207}
208
209} // namespace edm
210} // namespace vecmem
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
A view for jagged vectors.
Definition jagged_vector_view.hpp:45
std::conditional_t< vecmem::details::disjunction_v< std::is_const< typename details::view_type< VARTYPES >::payload_type >... >, details::const_memory_view, details::memory_view > memory_view_type
Type of the view(s) into the raw data of the view.
Definition view.hpp:65
details::size_type size_type
Size type used for the container.
Definition view.hpp:51
Technical base type for view<schema<VARTYPES...>>
Definition view.hpp:29
VECMEM_HOST std::vector< typename vector_view< T >::size_type > get_capacities(const jagged_vector_view< T > &data)
Get the capacities of the inner vectors of a jagged vector.
Definition jagged_vector_view.ipp:111
VECMEM_HOST std::vector< vecmem::data::vector_view< int >::size_type > get_capacities(const view< schema< VARTYPES... > > &soa)
Helper function to get the capacities of the jagged vectors in a view.
Definition view.ipp:198
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
VECMEM_HOST_AND_DEVICE constexpr const auto & get(const tuple< Ts... > &t) noexcept
Get a constant element out of a tuple.
Definition tuple.ipp:58
typename tuple_element< I, T >::type tuple_element_t
Convenience accessor for the I-th element of a tuple.
Definition tuple.hpp:204
Implementation for std::negation.
Definition type_traits.hpp:91
Technical base type for has_jagged_vector<schema<VARTYPES...>>
Definition schema_traits.hpp:238
Definition view_traits.hpp:33
Meta type describing the "schema" of an SoA container.
Definition schema.hpp:46
Definition schema_traits.hpp:108