Skip to content

Commit

Permalink
Merge pull request #262 from JohanMabille/xtl
Browse files Browse the repository at this point in the history
Xtl
  • Loading branch information
JohanMabille authored May 31, 2024
2 parents 3547b9d + d7cf044 commit b8c88bf
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 194 deletions.
12 changes: 3 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,13 @@ message(STATUS "xwidgets binary version: v${XWIDGETS_BINARY_VERSION}")

option(XWIDGETS_BUILD_SHARED_LIBS "Build xwidgets shared library." ON)
option(XWIDGETS_BUILD_STATIC_LIBS "Build xwidgets static library (default if BUILD_SHARED_LIBS is OFF)." ON)
option(XWIDGETS_BUILD_TESTS "xwidgets test suite" OFF)

# Dependencies
# ============

set(xtl_REQUIRED_VERSION 0.7.0)
set(xeus_REQUIRED_VERSION 5.0.0)
set(xproperty_REQUIRED_VERSION 0.11.0)

if(NOT TARGET xtl)
find_package(xtl ${xtl_REQUIRED_VERSION} REQUIRED)
endif()
set(xproperty_REQUIRED_VERSION 0.12.0)

if(NOT TARGET xeus)
find_package(xeus ${xeus_REQUIRED_VERSION} REQUIRED)
Expand Down Expand Up @@ -160,7 +156,7 @@ macro(xwidgets_create_target target_name linkage output_name)
${target_name} PUBLIC $<BUILD_INTERFACE:${XWIDGETS_INCLUDE_DIR}> $<INSTALL_INTERFACE:include>
)

target_link_libraries(${target_name} PUBLIC xtl ${XEUS_TARGET_NAME} xproperty)
target_link_libraries(${target_name} PUBLIC ${XEUS_TARGET_NAME} xproperty)

set_target_properties(
${target_name}
Expand Down Expand Up @@ -228,8 +224,6 @@ endif()

# Tests =====

option(XWIDGETS_BUILD_TESTS "xwidgets test suite" OFF)

include(CTest)

if(XWIDGETS_BUILD_TESTS)
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
- ninja
# Host dependencies
- xeus>=5.0.0,<6
- xproperty=0.11.0
- xproperty=0.12.0
- nlohmann_json=3.11.3
# Test dependencies
- doctest >= 2.4.6
Expand Down
20 changes: 9 additions & 11 deletions include/xwidgets/xbutton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

#include <functional>
#include <list>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>

#include <xtl/xoptional.hpp>

#include "xcolor.hpp"
#include "xeither.hpp"
Expand All @@ -40,14 +38,14 @@ namespace xw
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
void apply_patch(const nl::json&, const xeus::buffer_sequence&);

XPROPERTY(xtl::xoptional<html_color>, derived_type, button_color);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_family);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_size);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_style);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_variant);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_weight);
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_color);
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_decoration);
XPROPERTY(std::optional<html_color>, derived_type, button_color);
XPROPERTY(std::optional<std::string>, derived_type, font_family);
XPROPERTY(std::optional<std::string>, derived_type, font_size);
XPROPERTY(std::optional<std::string>, derived_type, font_style);
XPROPERTY(std::optional<std::string>, derived_type, font_variant);
XPROPERTY(std::optional<std::string>, derived_type, font_weight);
XPROPERTY(std::optional<std::string>, derived_type, text_color);
XPROPERTY(std::optional<std::string>, derived_type, text_decoration);

protected:

Expand Down
5 changes: 2 additions & 3 deletions include/xwidgets/xcheckbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
#ifndef XWIDGETS_CHECKBOX_HPP
#define XWIDGETS_CHECKBOX_HPP

#include <optional>
#include <string>

#include <xtl/xoptional.hpp>

#include "xboolean.hpp"
#include "xmaterialize.hpp"
#include "xstyle.hpp"
Expand All @@ -34,7 +33,7 @@ namespace xw
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
void apply_patch(const nl::json&, const xeus::buffer_sequence&);

XPROPERTY(xtl::xoptional<std::string>, derived_type, background);
XPROPERTY(std::optional<std::string>, derived_type, background);
XPROPERTY(std::string, derived_type, description_width);

protected:
Expand Down
4 changes: 2 additions & 2 deletions include/xwidgets/xcommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef XWIDGETS_COMMON_HPP
#define XWIDGETS_COMMON_HPP

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include <xeus/xcomm.hpp>
#include <xtl/xoptional.hpp>

#include "xbinary.hpp"
#include "xwidgets_config.hpp"
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace xw
*
* If the optional is empty, then no setting is set explicitly.
*/
static xtl::xoptional<bool> global_echo_update();
static std::optional<bool> global_echo_update();

bool
same_patch(const std::string&, const nl::json&, const xeus::buffer_sequence&, const nl::json&, const xeus::buffer_sequence&)
Expand Down
7 changes: 3 additions & 4 deletions include/xwidgets/xeither.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
#ifndef XWIDGETS_EITHER_HPP
#define XWIDGETS_EITHER_HPP

#include <optional>
#include <stdexcept>
#include <string>
#include <unordered_set>

#include "xtl/xoptional.hpp"

template <typename... T>
auto XEITHER(T... vals)
{
Expand All @@ -35,9 +34,9 @@ auto XEITHER_OPTIONAL(T... vals)
const std::unordered_set<std::string> options({vals...});
return [options](const auto& proposal)
{
if (xtl::has_value(proposal))
if (proposal.has_value())
{
auto position = options.find(xtl::value(proposal));
auto position = options.find(proposal.value());
if (position == options.end())
{
throw std::runtime_error("Invalid proposal for optional string enum");
Expand Down
39 changes: 19 additions & 20 deletions include/xwidgets/xholder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef XWIDGETS_HOLDER_HPP
#define XWIDGETS_HOLDER_HPP

#include <any>
#include <memory>
#include <stdexcept>
#include <string>
Expand All @@ -17,8 +18,6 @@
#include <nlohmann/json.hpp>
#include <xeus/xguid.hpp>
#include <xeus/xmessage.hpp>
#include <xtl/xany.hpp>
#include <xtl/xclosure.hpp>

#include "xbinary.hpp"
#include "xwidgets_config.hpp"
Expand Down Expand Up @@ -74,8 +73,8 @@ namespace xw
void serialize_state(nl::json& state, xeus::buffer_sequence& buffers) const;
const std::vector<xjson_path_type>& buffer_paths() const;

xtl::any value() &;
const xtl::any value() const&;
std::any value() &;
const std::any value() const&;

template <class D>
D& get() &;
Expand Down Expand Up @@ -139,8 +138,8 @@ namespace xw
virtual void serialize_state(nl::json& state, xeus::buffer_sequence& buffers) const = 0;
virtual const std::vector<xjson_path_type>& buffer_paths() const = 0;

virtual xtl::any value() & = 0;
virtual const xtl::any value() const& = 0;
virtual std::any value() & = 0;
virtual const std::any value() const& = 0;

protected:

Expand Down Expand Up @@ -196,14 +195,14 @@ namespace xw
return m_value.buffer_paths();
}

xtl::any value() & override
std::any value() & override
{
return xtl::closure(m_value);
return std::ref(m_value);
}

const xtl::any value() const& override
const std::any value() const& override
{
return xtl::closure(m_value);
return std::cref(m_value);
}

private:
Expand Down Expand Up @@ -259,14 +258,14 @@ namespace xw
return p_value->buffer_paths();
}

xtl::any value() & override
std::any value() & override
{
return xtl::closure(*p_value);
return std::ref(*p_value);
}

const xtl::any value() const& override
const std::any value() const& override
{
return xtl::closure(*p_value);
return std::cref(*p_value);
}

private:
Expand Down Expand Up @@ -326,14 +325,14 @@ namespace xw
return p_value->buffer_paths();
}

xtl::any value() & override
std::any value() & override
{
return xtl::closure(*p_value);
return std::ref(*p_value);
}

const xtl::any value() const& override
const std::any value() const& override
{
return xtl::closure(*p_value);
return std::cref(*p_value);
}

private:
Expand Down Expand Up @@ -413,13 +412,13 @@ namespace xw
template <class D>
D& xholder::get() &
{
return xtl::any_cast<xtl::closure_wrapper<D&>>(this->value()).get();
return std::any_cast<std::reference_wrapper<D>>(this->value()).get();
}

template <class D>
const D& xholder::get() const&
{
return xtl::any_cast<xtl::closure_wrapper<const D&>>(this->value()).get();
return std::any_cast<std::reference_wrapper<const D>>(this->value()).get();
}
}

Expand Down
9 changes: 4 additions & 5 deletions include/xwidgets/xhtml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
#ifndef XWIDGETS_HTML_HPP
#define XWIDGETS_HTML_HPP

#include <optional>
#include <string>

#include <xtl/xoptional.hpp>

#include "xmaterialize.hpp"
#include "xstring.hpp"
#include "xstyle.hpp"
Expand All @@ -34,10 +33,10 @@ namespace xw
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
void apply_patch(const nl::json&, const xeus::buffer_sequence&);

XPROPERTY(xtl::xoptional<std::string>, derived_type, background);
XPROPERTY(std::optional<std::string>, derived_type, background);
XPROPERTY(std::string, derived_type, description_width);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_size);
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_color);
XPROPERTY(std::optional<std::string>, derived_type, font_size);
XPROPERTY(std::optional<std::string>, derived_type, text_color);

protected:

Expand Down
47 changes: 47 additions & 0 deletions include/xwidgets/xjson.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/***************************************************************************
* Copyright (c) 2024, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XWIDGETS_JSON_HPP
#define XWIDGETS_JSON_HPP

#include <optional>

#include <nlohmann/json.hpp>

namespace nlohmann
{
template <class T>
struct adl_serializer<std::optional<T>>
{
static void to_json(nlohmann::json& j, const std::optional<T>& o)
{
if (!o.has_value())
{
j = nullptr;
}
else
{
j = o.value();
}
}

static void from_json(const nlohmann::json& j, std::optional<T>& o)
{
if (j.is_null())
{
o = std::nullopt;
}
else
{
o = j.get<T>();
}
}
};
}

#endif
19 changes: 9 additions & 10 deletions include/xwidgets/xlabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
#ifndef XWIDGETS_LABEL_HPP
#define XWIDGETS_LABEL_HPP

#include <optional>
#include <string>

#include <xtl/xoptional.hpp>

#include "xmaterialize.hpp"
#include "xstring.hpp"
#include "xstyle.hpp"
Expand All @@ -35,15 +34,15 @@ namespace xw
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
void apply_patch(const nl::json&, const xeus::buffer_sequence&);

XPROPERTY(xtl::xoptional<std::string>, derived_type, background);
XPROPERTY(std::optional<std::string>, derived_type, background);
XPROPERTY(std::string, derived_type, description_width);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_family);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_size);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_style);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_variant);
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_weight);
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_color);
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_decoration);
XPROPERTY(std::optional<std::string>, derived_type, font_family);
XPROPERTY(std::optional<std::string>, derived_type, font_size);
XPROPERTY(std::optional<std::string>, derived_type, font_style);
XPROPERTY(std::optional<std::string>, derived_type, font_variant);
XPROPERTY(std::optional<std::string>, derived_type, font_weight);
XPROPERTY(std::optional<std::string>, derived_type, text_color);
XPROPERTY(std::optional<std::string>, derived_type, text_decoration);

protected:

Expand Down
Loading

0 comments on commit b8c88bf

Please sign in to comment.