kokkos-execution 0.0.1
Loading...
Searching...
No Matches
streamers.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_UTILS_STREAMERS_HPP
2#define KOKKOS_EXECUTION_UTILS_STREAMERS_HPP
3
4#include <concepts>
5#include <ostream>
6#include <ranges>
7#include <vector>
8
10
11namespace detail {
12
13template <typename T>
14concept char_like = std::same_as<std::remove_cv_t<T>, char> || std::same_as<std::remove_cv_t<T>, wchar_t>
15 || std::same_as<std::remove_cv_t<T>, char8_t> || std::same_as<std::remove_cv_t<T>, char16_t>
16 || std::same_as<std::remove_cv_t<T>, char32_t>;
17
19template <class R>
20concept streamable_range = std::ranges::input_range<const R> && !char_like<std::ranges::range_value_t<const R>>
21 && requires(std::ostream& out, std::ranges::range_reference_t<const R> value) { out << value; };
22} // namespace detail
23
24template <detail::streamable_range R>
25std::ostream& operator<<(std::ostream& out, const R& values) {
26 out << '[';
27 for (bool first = true; const auto& value: values) {
28 if (!first)
29 out << ", ";
30 out << value;
31 first = false;
32 }
33 return out << ']';
34}
35
36} // namespace Kokkos::Execution::Utils
37
38#if defined(__cpp_impl_reflection)
39
40# include <meta>
41
43
46struct Streamable { };
47
48inline constexpr Streamable streamable{};
50
52consteval bool is_streamable(std::meta::info type) {
53 return !std::meta::annotations_of_with_type(type, ^^Streamable).empty();
54}
55
57template <typename T>
58concept IsStreamable = is_streamable(^^T);
59
60template <typename T>
61std::ostream& stream_to(std::ostream& out, const T& obj) {
62 static constexpr auto members = std::define_static_array(
63 std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::current()));
64
65 out << std::meta::identifier_of(^^T) << ": {";
66
67 template for (constexpr std::meta::info member: members) {
68 if constexpr (member != members.front()) {
69 out << ", ";
70 }
71 out << std::meta::identifier_of(member) << " = " << obj.[:member:];
72 }
73
74 return out << '}';
75}
76
78template <IsStreamable T>
79std::ostream& operator<<(std::ostream& out, const T& obj) {
80 return stream_to(out, obj);
81}
82
83} // namespace Kokkos::Execution::Utils
84
85# define KOKKOS_EXECUTION_UTILS_STREAMABLE [[= Kokkos::Execution::Utils::streamable]]
86#else
87# define KOKKOS_EXECUTION_UTILS_STREAMABLE
88#endif
89
90#endif // KOKKOS_EXECUTION_UTILS_STREAMERS_HPP
A (const-iterable) range of streamable, non-character elements.
Definition streamers.hpp:20
std::ostream & operator<<(std::ostream &out, const R &values)
Definition streamers.hpp:25