gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ostream_helpers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Arteris, Inc. and its applicable licensors and
3  * affiliates.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef BASE_STL_HELPERS_OSTREAM_HELPERS_HH
31 #define BASE_STL_HELPERS_OSTREAM_HELPERS_HH
32 
33 #include <iostream>
34 #include <memory>
35 #include <tuple>
36 #include <utility>
37 
38 #include "base/type_traits.hh"
39 #include "magic_enum/magic_enum.hh"
40 
41 namespace gem5::stl_helpers
42 {
43 
44 namespace opExtract_impl
45 {
46 
47 /*
48  * In order to provide a specialization for operator<< with stl_helpers-enabled
49  * types
50  * without loosing the hability to use it with other types, a dual-dispatch
51  * mechanism is used. The only entry point in the system is through a primary
52  * dispatch function that won't resolve for non-helped types. Then, recursive
53  * calls go through the secondary dispatch interface that sort between helped
54  * and non-helped types. Helped typed will enter the system back through the
55  * primary dispatch interface while other types will look for operator<<
56  * through regular lookup, especially ADL.
57  */
58 
59 template<typename T>
60 std::ostream&
61 opExtractSecDisp(std::ostream& os, const T& v);
62 
63 template <typename E>
64 std::enable_if_t<std::is_enum_v<E>,
65 std::ostream&>
66 opExtractPrimDisp(std::ostream& os, const E& e)
67 {
68  return os << magic_enum::enum_name(e);
69 }
70 
71 template <typename... T>
72 std::ostream&
73 opExtractPrimDisp(std::ostream& os, const std::tuple<T...>& p)
74 {
75  std::apply([&](auto&&... e) {
76  std::size_t n{0};
77  os << '(';
78  ((opExtractSecDisp(os, e) << (++n != sizeof...(T) ? ", " : "")), ...);
79  os << ')';
80  }, p);
81  return os;
82 }
83 
84 template <typename T, typename U>
85 std::ostream&
86 opExtractPrimDisp(std::ostream& os, const std::pair<T, U>& p)
87 {
88  return opExtractPrimDisp(os, std::tie(p.first, p.second));
89 }
90 
91 template <typename T>
92 std::enable_if_t<is_iterable_v<T>, std::ostream&>
93 opExtractPrimDisp(std::ostream& os, const T& v)
94 {
95  os << "[ ";
96  for (auto& e: v) {
97  opExtractSecDisp(os, e) << ", ";
98  }
99  return os << ']';
100 }
101 
102 template <typename T>
103 std::ostream&
104 opExtractPrimDisp(std::ostream& os, const std::optional<T>& o)
105 {
106  if (o) {
107  return opExtractSecDisp(os, *o);
108  } else {
109  return os << '-';
110  }
111 }
112 
113 template <typename T>
114 std::ostream&
115 opExtractPrimDisp(std::ostream& os, T* p);
116 
117 template <typename T>
118 std::ostream&
119 opExtractPrimDisp(std::ostream& os, const std::shared_ptr<T>& p)
120 {
121  return opExtractPrimDisp(os, p.get());
122 }
123 
124 template <typename T>
125 std::ostream&
126 opExtractPrimDisp(std::ostream& os, const std::unique_ptr<T>& p)
127 {
128  return opExtractPrimDisp(os, p.get());
129 }
130 
131 template <typename, typename = void>
132 constexpr bool isOpExtractNativelySupported = false;
133 
134 template <typename T>
135 constexpr bool isOpExtractNativelySupported<T,
136  std::void_t<decltype(
137  std::declval<std::ostream&>() << std::declval<T>())>> = true;
138 
139 template <typename, typename = void>
140 constexpr bool isOpExtractHelped = false;
141 
142 template <typename T>
143 constexpr bool isOpExtractHelped<T,
144  std::void_t<decltype(
145  opExtractPrimDisp(std::declval<std::ostream&>(),
146  std::declval<T>()))>>
147  = true;
148 
149 template <typename T>
150 constexpr bool needsDispatch =
151  isOpExtractHelped<T> && !isOpExtractNativelySupported<T>;
152 
153 template <typename T>
154 std::ostream&
155 opExtractPrimDisp(std::ostream& os, T* p)
156 {
157  if (!p) {
158  return os << "nullptr";
159  }
160  if constexpr (isOpExtractHelped<T> || isOpExtractNativelySupported<T>) {
161  os << '(' << p << ": ";
162  opExtractSecDisp(os, *p);
163  return os << ')';
164  } else {
165  return os << p;
166  }
167 }
168 
169 template<typename T>
170 std::ostream&
171 opExtractSecDisp(std::ostream& os, const T& v)
172 {
173  if constexpr (needsDispatch<T>) {
174  return opExtractPrimDisp(os, v);
175  } else {
176  return os << v;
177  }
178 }
179 
180 } // namespace opExtract_impl
181 
182 // Add "using stl_helpers::operator<<" in the scope where you want to use it.
183 template<typename T>
184 std::enable_if_t<opExtract_impl::needsDispatch<T>, std::ostream&>
185 operator<<(std::ostream& os, const T& v)
186 {
188 }
189 
190 } // namespace gem5::stl_helpers
191 
192 #endif // BASE_STL_HELPERS_OSTREAM_HELPERS_HH
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:65
gem5::stl_helpers::opExtract_impl::opExtractPrimDisp
std::enable_if_t< std::is_enum_v< E >, std::ostream & > opExtractPrimDisp(std::ostream &os, const E &e)
Definition: ostream_helpers.hh:66
gem5::stl_helpers::opExtract_impl::isOpExtractNativelySupported
constexpr bool isOpExtractNativelySupported
Definition: ostream_helpers.hh:132
gem5::stl_helpers::opExtract_impl::opExtractSecDisp
std::ostream & opExtractSecDisp(std::ostream &os, const T &v)
Definition: ostream_helpers.hh:171
gem5::SparcISA::int_reg::o
constexpr RegId o(int index)
Definition: int.hh:147
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::stl_helpers::operator<<
std::enable_if_t< opExtract_impl::needsDispatch< T >, std::ostream & > operator<<(std::ostream &os, const T &v)
Definition: ostream_helpers.hh:185
std::pair
STL pair class.
Definition: stl.hh:58
gem5::VegaISA::v
Bitfield< 0 > v
Definition: pagetable.hh:65
gem5::stl_helpers::opExtract_impl::needsDispatch
constexpr bool needsDispatch
Definition: ostream_helpers.hh:150
gem5::ArmISA::n
Bitfield< 31 > n
Definition: misc_types.hh:513
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:810
gem5::stl_helpers::opExtract_impl::isOpExtractHelped
constexpr bool isOpExtractHelped
Definition: ostream_helpers.hh:140
gem5::X86ISA::E
Bitfield< 31, 0 > E
Definition: int.hh:56
type_traits.hh
gem5::stl_helpers
Definition: hash_helpers.hh:48

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17