Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Corrade/Utility/DebugStl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,28 @@ template<class ...Args> Debug& operator<<(Debug& debug, const std::tuple<Args...

namespace Implementation {

CORRADE_HAS_TYPE(
HasOstreamOperator,
typename std::enable_if<std::is_same<
decltype(DeclareLvalueReference<std::ostream>() << std::declval<T>()),
std::add_lvalue_reference<std::ostream>::type
>::value>::type
);

CORRADE_HAS_TYPE(
HasDebugStreamOperator,
typename std::enable_if<std::is_same<
decltype(DeclareLvalueReference<Debug>() << std::declval<T>()),
std::add_lvalue_reference<Debug>::type
>::value>::type
);

/* Used by Debug::operator<<(Implementation::DebugOstreamFallback&&) */
struct DebugOstreamFallback {
template<class T> /*implicit*/ DebugOstreamFallback(const T& t): applier(&DebugOstreamFallback::applyImpl<T>), value(&t) {}
template<
class T,
typename = typename std::enable_if<HasOstreamOperator<T>::value && !HasDebugStreamOperator<T>::value>::type
> /*implicit*/ DebugOstreamFallback(const T& t): applier(&DebugOstreamFallback::applyImpl<T>), value(&t) {}

void apply(std::ostream& s) const {
(this->*applier)(s);
Expand All @@ -130,4 +149,14 @@ CORRADE_UTILITY_EXPORT Debug& operator<<(Debug& debug, Implementation::DebugOstr

}}

template<typename T>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing: can you

  • add a short documentation block for this new function,
  • mention this in the class documentation so people get to know it exists -- there's a "Printing STL types" section, so maybe extending that a bit ("Printing STL types, interaction with iostreams"?)
  • and add a test in Test/DebugTest.cpp? There's ostreamFallback and ostreamFallbackPriority, so something similar (ostreamDelegate?) and also similarly checking the priority works for the inverse case, picking the ostream operator over the Debug one -- btw., don't forget to list the two new methods in the addTests() so these get properly called

Thank you! 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! wow ostreamDelegate is way better than any name I considered

typename std::enable_if<
Corrade::Utility::Implementation::HasDebugStreamOperator<T>::value && !Corrade::Utility::Implementation::HasOstreamOperator<T>::value,
std::ostream
>::type &operator<<(std::ostream &os, const T &val) {
Corrade::Utility::Debug debug{&os, Corrade::Utility::Debug::Flag::NoNewlineAtTheEnd};
debug << val;
return os;
}

#endif
6 changes: 6 additions & 0 deletions src/Corrade/Utility/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,10 @@ template<class T> using IsStringLike = std::integral_constant<bool,

}}

/**
@brief Like @cpp std::declval @ce, but declares an lvalue reference
*/
template<typename T>
typename std::add_lvalue_reference<T>::type DeclareLvalueReference() noexcept;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I'm usually not putting a newline after template<..>, keeping the whole signature a single line. (Or generally not adding a newline unless really needed to keep the code vertically terse.)


#endif