Skip to content

Commit

Permalink
Merge pull request #152 from JohanMabille/holder
Browse files Browse the repository at this point in the history
xholder constructor/assigment taking a shared pointer
  • Loading branch information
JohanMabille committed Jul 10, 2018
2 parents 886fc34 + deab69a commit 2d4c1a5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
22 changes: 11 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
endif()

# if(MSVC)
# target_compile_definitions(xwidgets PUBLIC -DNOMINMAX)
# target_compile_options(xwidgets PUBLIC /DGUID_WINDOWS /MP /bigobj)
# target_compile_options(xwidgets PUBLIC /wd4251 /wd 4996)
# # Allows to compile in debug without requiring each dependencies to
# # be compiled in debug
# if(${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
# set(CMAKE_CXX_FLAGS_DEBUG "/DNDEBUG /MD /Zi /Ob0 /Od /RTC1")
# message(STATUS "Overriding CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
# endif()
# endif()
if(MSVC)
target_compile_definitions(xwidgets PUBLIC -DNOMINMAX)
target_compile_options(xwidgets PUBLIC /DGUID_WINDOWS /MP /bigobj)
target_compile_options(xwidgets PUBLIC /wd4251 /wd 4996)
# Allows to compile in debug without requiring each dependencies to
# be compiled in debug
if(${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "/DNDEBUG /MD /Zi /Ob0 /Od /RTC1")
message(STATUS "Overriding CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
endif()
endif()

# Tests
# =====
Expand Down
23 changes: 23 additions & 0 deletions include/xwidgets/xholder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace xw
xholder(const CRTP<D>& rhs);
template <class D>
xholder(CRTP<D>&& rhs);
template <class D, class = std::enable_if_t<std::is_base_of<CRTP<D>, D>::value, void>>
xholder(std::shared_ptr<D> rhs);
xholder(implementation_type* holder);

xholder& operator=(const xholder& rhs);
Expand All @@ -58,6 +60,9 @@ namespace xw
xholder& operator=(const CRTP<D>& rhs);
template <class D>
xholder& operator=(CRTP<D>&& rhs);
template <class D>
std::enable_if_t<std::is_base_of<CRTP<D>, D>::value, xholder&>
operator=(std::shared_ptr<D> ptr);

void swap(xholder& rhs);

Expand Down Expand Up @@ -334,6 +339,13 @@ namespace xw
{
}

template <template <class> class CRTP>
template <class D, class>
xholder<CRTP>::xholder(std::shared_ptr<D> rhs)
: xholder(make_shared_holder<CRTP, D>(rhs))
{
}

template <template <class> class CRTP>
xholder<CRTP>::xholder(xholder&& rhs)
: p_holder(rhs.p_holder)
Expand Down Expand Up @@ -379,6 +391,17 @@ namespace xw
return *this;
}

template <template <class> class CRTP>
template <class D>
std::enable_if_t<std::is_base_of<CRTP<D>, D>::value, xholder<CRTP>&>
xholder<CRTP>::operator=(std::shared_ptr<D> ptr)
{
using std::swap;
xholder<CRTP> tmp(make_shared_holder<CRTP, D>(ptr));
swap(tmp, *this);
return *this;
}

template <template <class> class CRTP>
void xholder<CRTP>::swap(xholder& rhs)
{
Expand Down
18 changes: 18 additions & 0 deletions test/test_xholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,22 @@ namespace xw
hm2.erase("y");
ASSERT_EQ(button_tester::instance_count(), 0);
}

TEST(xholder, constructor)
{
using holder = xholder<xwidget>;
button b1, b2;
auto b3 = std::make_shared<button>();

holder h1(b1);
holder h2(std::move(b2));
holder h3(b3);

button b11, b22;
auto b33 = std::make_shared<button>();

h1 = b11;
h2 = std::move(b22);
h3 = b33;
}
}

0 comments on commit 2d4c1a5

Please sign in to comment.