|
38 | 38 | #include <string> |
39 | 39 | #include <exception> |
40 | 40 | #include <type_traits> |
| 41 | +#include <string_view> |
| 42 | +#include <concepts> |
| 43 | + |
| 44 | +#include "fmt/format.h" |
41 | 45 |
|
42 | 46 | #include "FWCore/Utilities/interface/thread_safety_macros.h" |
43 | 47 | #include "FWCore/Utilities/interface/Likely.h" |
44 | 48 | #include "FWCore/Utilities/interface/Visibility.h" |
45 | 49 |
|
46 | 50 | namespace cms { |
47 | 51 |
|
48 | | - class Exception; |
49 | | - namespace detail { |
50 | | - // Used for SFINAE to control the instantiation of the stream insertion |
51 | | - // member template needed to support streaming output to an object |
52 | | - // of type cms::Exception, or a subclass of cms::Exception. |
53 | | - template <typename E> |
54 | | - using exception_type = |
55 | | - std::enable_if_t<std::is_base_of_v<Exception, std::remove_reference_t<E>>, std::remove_reference_t<E>>; |
56 | | - |
57 | | - } // namespace detail |
58 | | - |
59 | 52 | class dso_export Exception : public std::exception { |
60 | 53 | public: |
61 | 54 | explicit Exception(std::string const& aCategory); |
@@ -118,13 +111,20 @@ namespace cms { |
118 | 111 | // |
119 | 112 |
|
120 | 113 | template <typename E, typename T> |
121 | | - friend typename detail::exception_type<E>& operator<<(E&& e, T const& stuff); |
| 114 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 115 | + friend E& operator<<(E&& e, T const& stuff); |
122 | 116 |
|
123 | 117 | template <typename E> |
124 | | - friend typename detail::exception_type<E>& operator<<(E&& e, std::ostream& (*f)(std::ostream&)); |
| 118 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 119 | + friend E& operator<<(E&& e, std::ostream& (*f)(std::ostream&)); |
125 | 120 |
|
126 | 121 | template <typename E> |
127 | | - friend typename detail::exception_type<E>& operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)); |
| 122 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 123 | + friend E& operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)); |
| 124 | + |
| 125 | + template <typename... Args> |
| 126 | + inline void format(fmt::format_string<Args...> format, Args&&... args); |
| 127 | + inline void vformat(std::string_view fmt, fmt::format_args args); |
128 | 128 |
|
129 | 129 | // This function is deprecated and we are in the process of removing |
130 | 130 | // all code that uses it from CMSSW. It will then be deleted. |
@@ -152,20 +152,30 @@ namespace cms { |
152 | 152 |
|
153 | 153 | // -------- implementation --------- |
154 | 154 |
|
| 155 | + template <typename... Args> |
| 156 | + inline void Exception::format(fmt::format_string<Args...> format, Args&&... args) { |
| 157 | + ost_ << fmt::format(std::move(format), std::forward<Args>(args)...); |
| 158 | + } |
| 159 | + |
| 160 | + inline void Exception::vformat(std::string_view format, fmt::format_args args) { ost_ << fmt::vformat(format, args); } |
| 161 | + |
155 | 162 | template <typename E, typename T> |
156 | | - inline typename detail::exception_type<E>& operator<<(E&& e, T const& stuff) { |
| 163 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 164 | + inline E& operator<<(E&& e, T const& stuff) { |
157 | 165 | e.ost_ << stuff; |
158 | 166 | return e; |
159 | 167 | } |
160 | 168 |
|
161 | 169 | template <typename E> |
162 | | - inline typename detail::exception_type<E>& operator<<(E&& e, std::ostream& (*f)(std::ostream&)) { |
| 170 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 171 | + inline E& operator<<(E&& e, std::ostream& (*f)(std::ostream&)) { |
163 | 172 | f(e.ost_); |
164 | 173 | return e; |
165 | 174 | } |
166 | 175 |
|
167 | 176 | template <typename E> |
168 | | - inline typename detail::exception_type<E>& operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)) { |
| 177 | + requires std::derived_from<std::remove_reference_t<E>, Exception> |
| 178 | + inline E& operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)) { |
169 | 179 | f(e.ost_); |
170 | 180 | return e; |
171 | 181 | } |
|
0 commit comments