Skip to content

Commit

Permalink
Merge pull request #199 from SylvainCorlay/refactor-xcommon
Browse files Browse the repository at this point in the history
Refactor xtransport
  • Loading branch information
SylvainCorlay committed Oct 24, 2020
2 parents b66b8e7 + 8c839fb commit 3b1d604
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 267 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ set(XWIDGETS_HEADERS
${XWIDGETS_INCLUDE_DIR}/xwidgets/xcheckbox.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xcolor.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xcolor_picker.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xcommon.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xcontroller.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xdropdown.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xeither.hpp
Expand Down Expand Up @@ -126,6 +127,7 @@ set(XWIDGETS_SOURCES
${XWIDGETS_SOURCE_DIR}/xcheckbox.cpp
${XWIDGETS_SOURCE_DIR}/xcolor_picker.cpp
${XWIDGETS_SOURCE_DIR}/xcontroller.cpp
${XWIDGETS_SOURCE_DIR}/xcommon.cpp
${XWIDGETS_SOURCE_DIR}/xdropdown.cpp
${XWIDGETS_SOURCE_DIR}/xhtml.cpp
${XWIDGETS_SOURCE_DIR}/ximage.cpp
Expand Down
134 changes: 134 additions & 0 deletions include/xwidgets/xcommon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/***************************************************************************
* Copyright (c) 2017, Sylvain Corlay and Johan Mabille *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XWIDGETS_COMMON_HPP
#define XWIDGETS_COMMON_HPP

#include <string>
#include <vector>

#include "xeus/xcomm.hpp"

#include "xbinary.hpp"
#include "xcommon.hpp"
#include "xholder.hpp"
#include "xtarget.hpp"
#include "xwidgets_config.hpp"

namespace xw
{
/**********************************************
* property serialization and deserialization *
**********************************************/

// Values

template <class T>
inline void xwidgets_serialize(const T& value, nl::json& j, xeus::buffer_sequence&)
{
j = value;
}

template <class T>
inline void xwidgets_deserialize(T& value, const nl::json& j, const xeus::buffer_sequence&)
{
value = j.template get<T>();
}

/***********************
* xcommon declaration *
***********************/

class XWIDGETS_API xcommon
{
public:

xeus::xguid id() const noexcept;
void display() const;

protected:

xcommon();
xcommon(xeus::xcomm&&);
~xcommon();
xcommon(const xcommon&);
xcommon(xcommon&&);
xcommon& operator=(const xcommon&);
xcommon& operator=(xcommon&&);

bool moved_from() const noexcept;
void handle_custom_message(const nl::json&);
xeus::xcomm& comm();
const xeus::xcomm& comm() const;
const xeus::xmessage*& hold();
const xeus::xmessage* const& hold() const;
const std::vector<xjson_path_type>& buffer_paths() const;

void open(nl::json&& patch, xeus::buffer_sequence&& buffers);
void close();

template <class T>
void notify(const std::string& name, const T& value) const;
void send(nl::json&&, xeus::buffer_sequence&&) const;
void send_patch(nl::json&&, xeus::buffer_sequence&&) const;

private:

bool same_patch(const std::string&,
const nl::json&,
const xeus::buffer_sequence&,
const nl::json&,
const xeus::buffer_sequence&) const;

bool m_moved_from;
const xeus::xmessage* m_hold;
xeus::xcomm m_comm;
std::vector<xjson_path_type> m_buffer_paths;
};

/**************************
* to_json specialization *
*************************/

void XWIDGETS_API to_json(nl::json& j, const xcommon& o);

/********************************************
* xcommon template methods implementations *
********************************************/

template <class T>
inline void xcommon::notify(const std::string& name, const T& value) const
{
nl::json state;
xeus::buffer_sequence buffers;
xwidgets_serialize(value, state[name], buffers);

if (m_hold != nullptr)
{
const auto& hold_state = m_hold->content()["data"]["state"];
const auto& hold_buffers = m_hold->buffers();

auto it = hold_state.find(name);
if (it != hold_state.end())
{
if(same_patch(name,
*it,
hold_buffers,
state[name],
buffers))
{
return;
}
}
}

send_patch(std::move(state), std::move(buffers));
}
}

#endif
9 changes: 0 additions & 9 deletions include/xwidgets/xmedia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace xw

XPROPERTY(value_type, derived_type, value);

const std::vector<xjson_path_type>& buffer_paths() const;

protected:

xmedia();
Expand Down Expand Up @@ -78,13 +76,6 @@ namespace xw
set_property_from_patch(value, patch, buffers);
}

template <class D>
inline const std::vector<xjson_path_type>& xmedia<D>::buffer_paths() const
{
static const std::vector<xjson_path_type> default_buffer_paths = {{"value"}};
return default_buffer_paths;
}

template <class D>
inline xmedia<D>::xmedia()
: base_type()
Expand Down
12 changes: 6 additions & 6 deletions include/xwidgets/xobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

namespace xw
{
/****************************
* base xobject declaration *
****************************/
/***********************
* xobject declaration *
***********************/

template <class D>
class xobject : public xp::xobserved<D>, public xtransport<D>
Expand Down Expand Up @@ -59,9 +59,9 @@ namespace xw
#endif
};

/*******************************
* base xobject implementation *
*******************************/
/**************************
* xobject implementation *
**************************/

template <class D>
inline void xobject<D>::serialize_state(nl::json& state, xeus::buffer_sequence& buffers) const
Expand Down

0 comments on commit 3b1d604

Please sign in to comment.