Skip to content

Commit

Permalink
Merge pull request #205 from SylvainCorlay/double-crtp
Browse files Browse the repository at this point in the history
No double CRTP
  • Loading branch information
SylvainCorlay committed Oct 24, 2020
2 parents a83c9a6 + e54fee5 commit 2bf5532
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 56 deletions.
27 changes: 1 addition & 26 deletions include/xwidgets/xobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include "xtl/xoptional.hpp"
#include "xtl/xjson.hpp"

#include "xproperty/xobserved.hpp"

#include "xmaterialize.hpp"
#include "xtransport.hpp"

Expand All @@ -26,7 +24,7 @@ namespace xw
***********************/

template <class D>
class xobject : public xp::xobserved<D>, public xtransport<D>
class xobject : public xtransport<D>
{
public:

Expand All @@ -45,18 +43,9 @@ namespace xw
XPROPERTY(xtl::xoptional<std::string>, derived_type, _view_module_version, XWIDGETS_BASE_VERSION);
XPROPERTY(xtl::xoptional<std::string>, derived_type, _view_name, "WidgetView");

using base_type::notify;

protected:

// MSVC wrongly raises compiler error C2385 over ambiguous call to constructor
// without this workaround.
#ifdef _MSC_VER
xobject();
xobject(xeus::xcomm&&, bool owning = false);
#else
using base_type::base_type;
#endif
};

/**************************
Expand Down Expand Up @@ -84,20 +73,6 @@ namespace xw
set_property_from_patch(_view_module_version, patch, buffers);
set_property_from_patch(_view_name, patch, buffers);
}

#ifdef _MSC_VER
template <class D>
inline xobject<D>::xobject()
: base_type()
{
}

template <class D>
inline xobject<D>::xobject(xeus::xcomm&& com, bool owning)
: base_type(std::move(com), owning)
{
}
#endif
}

#endif
43 changes: 13 additions & 30 deletions include/xwidgets/xtransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "xeus/xcomm.hpp"
#include "xeus/xinterpreter.hpp"

#include "xproperty/xobserved.hpp"

#include "xbinary.hpp"
#include "xcommon.hpp"
#include "xholder.hpp"
Expand Down Expand Up @@ -43,16 +45,15 @@ namespace xw
**************************/

template <class D>
class xtransport : public xcommon
class xtransport : public xcommon, public xp::xobserved<D>
{
public:

using base_type = xcommon;
using derived_type = D;
using observed_type = xp::xobserved<D>;

derived_type& derived_cast() & noexcept;
const derived_type& derived_cast() const & noexcept;
derived_type derived_cast() && noexcept;
using base_type::notify;

protected:

Expand Down Expand Up @@ -94,7 +95,7 @@ namespace xw

template <class D>
inline xtransport<D>::xtransport()
: base_type()
: base_type(), observed_type()
{
this->comm().on_message(std::bind(&xtransport::handle_message, this, std::placeholders::_1));
get_transport_registry().register_weak(this);
Expand All @@ -111,7 +112,7 @@ namespace xw

template <class D>
inline xtransport<D>::xtransport(xeus::xcomm&& comm, bool owning)
: xcommon(std::move(comm))
: xcommon(std::move(comm)), observed_type()
{
this->comm().on_message(std::bind(&xtransport::handle_message, this, std::placeholders::_1));
if (!owning)
Expand All @@ -122,15 +123,15 @@ namespace xw

template <class D>
inline xtransport<D>::xtransport(const xtransport& other)
: xcommon(other)
: xcommon(other), observed_type()
{
this->comm().on_message(std::bind(&xtransport::handle_message, this, std::placeholders::_1));
get_transport_registry().register_weak(this);
}

template <class D>
inline xtransport<D>::xtransport(xtransport&& other)
: xcommon(std::move(other))
: xcommon(std::move(other)), observed_type()
{
this->comm().on_message(std::bind(&xtransport::handle_message, this, std::placeholders::_1));
get_transport_registry().register_weak(this); // Replacing the address of the moved transport with `this`.
Expand All @@ -156,31 +157,13 @@ namespace xw
return *this;
}

template <class D>
inline auto xtransport<D>::derived_cast() & noexcept -> derived_type&
{
return *static_cast<derived_type*>(this);
}

template <class D>
inline auto xtransport<D>::derived_cast() const & noexcept -> const derived_type&
{
return *static_cast<const derived_type*>(this);
}

template <class D>
inline auto xtransport<D>::derived_cast() && noexcept -> derived_type
{
return *static_cast<derived_type*>(this);
}

template <class D>
inline void xtransport<D>::open()
{
// serialize state
nl::json state;
xeus::buffer_sequence buffers;
derived_cast().serialize_state(state, buffers);
this->derived_cast().serialize_state(state, buffers);

// open comm
base_type::open(std::move(state), std::move(buffers));
Expand All @@ -207,7 +190,7 @@ namespace xw
this->hold() = std::addressof(message);;
insert_buffer_paths(const_cast<nl::json&>(state), buffer_paths);
/*D*/
derived_cast().apply_patch(state, buffers);
this->derived_cast().apply_patch(state, buffers);
/*D*/
this->hold() = nullptr;
}
Expand All @@ -216,7 +199,7 @@ namespace xw
nl::json state;
xeus::buffer_sequence buffers;
/*D*/
derived_cast().serialize_state(state, buffers);
this->derived_cast().serialize_state(state, buffers);
/*D*/
send_patch(std::move(state), std::move(buffers));
}
Expand All @@ -226,7 +209,7 @@ namespace xw
if (it != data.end())
{
/*D*/
derived_cast().handle_custom_message(it.value());
this->derived_cast().handle_custom_message(it.value());
/*D*/
}
}
Expand Down

0 comments on commit 2bf5532

Please sign in to comment.