Skip to content

Avoid injecting template to adl #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 5 additions & 7 deletions include/xcpp/xdisplay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,32 @@ namespace nl = nlohmann;
namespace xcpp
{
template <class T>
void display(const T& t)
void display(T&& t)
{
using ::xcpp::mime_bundle_repr;
xeus::get_interpreter().display_data(
mime_bundle_repr(t),
dispatch_mime_bundle_repr(std::forward<T>(t)),
nl::json::object(),
nl::json::object()
);
}

template <class T>
void display(const T& t, xeus::xguid id, bool update=false)
void display(T&& t, xeus::xguid id, bool update=false)
{
nl::json transient;
transient["display_id"] = id;
using ::xcpp::mime_bundle_repr;
if (update)
{
xeus::get_interpreter().update_display_data(
mime_bundle_repr(t),
dispatch_mime_bundle_repr(std::forward<T>(t)),
nl::json::object(),
std::move(transient)
);
}
else
{
xeus::get_interpreter().display_data(
mime_bundle_repr(t),
dispatch_mime_bundle_repr(std::forward<T>(t)),
nl::json::object(),
std::move(transient)
);
Expand Down
34 changes: 31 additions & 3 deletions include/xcpp/xmime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <complex>
#include <sstream>
#include <type_traits>

#include "nlohmann/json.hpp"

Expand Down Expand Up @@ -39,11 +40,14 @@ namespace xcpp
return bundle;
}

// Helper struct for SFINAE concept check - obsolete with C++17
template<class...>
using void_t = void;
}

// Default implementation of mime_bundle_repr
template <class T>
nl::json mime_bundle_repr(const T& value)
nl::json fallback_mime_bundle_repr(const T& value)
{
auto bundle = nl::json::object();
bundle["text/plain"] = cling::printValue(&value);
Expand All @@ -52,17 +56,41 @@ namespace xcpp

// Implementation for std::complex.
template <class T>
nl::json mime_bundle_repr(const std::complex<T>& value)
nl::json fallback_mime_bundle_repr(const std::complex<T>& value)
{
return detail::mime_bundle_repr_via_sstream(value);
}

// Implementation for long double. This is a workaround for
// https://github.com/jupyter-xeus/xeus-cling/issues/220
inline nl::json mime_bundle_repr(const long double& value)
inline nl::json fallback_mime_bundle_repr(const long double& value)
{
return detail::mime_bundle_repr_via_sstream(value);
}

template<typename T, typename = void>
struct MimeBundleReprDispatcher
{
template<typename U>
static nl::json dispatch(U&& u) {
return fallback_mime_bundle_repr(std::forward<U>(u));
}
};

template<typename T>
struct MimeBundleReprDispatcher<T, detail::void_t<decltype(mime_bundle_repr(std::declval<T>()))>>
{
template<typename U>
static nl::json dispatch(U&& u) {
return mime_bundle_repr(std::forward<U>(u));
}
};

template<typename T>
nl::json dispatch_mime_bundle_repr(T&& t)
{
return MimeBundleReprDispatcher<T>::dispatch(std::forward<T>(t));
}
}

#endif
3 changes: 1 addition & 2 deletions src/xmime_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ namespace xcpp
{
// Use an llvm::raw_ostream to prepend '0x' in front of the pointer value.
cling::ostrstream code;
code << "using xcpp::mime_bundle_repr;";
code << "mime_bundle_repr(";
code << "xcpp::dispatch_mime_bundle_repr(";
code << "*(" << xcpp::cling_detail::getTypeString(V);
code << &value;
code << "));";
Expand Down