Skip to content

Commit

Permalink
BugFix: moveable but not-default-constructible types as replies in sync.
Browse files Browse the repository at this point in the history
There was a compilation error in the case when moveable but
not-default-constructible type is used as reply in sync interaction.
  • Loading branch information
eao197 committed Sep 4, 2021
1 parent 9eaa0a5 commit fdab8ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dev/so_5_extra/sync/pub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,10 @@ class request_reply_t final
typeid(request_reply_t).name() );
}

return *result;
if constexpr( is_reply_moveable )
return std::move(*result);
else
return *result;
}

public :
Expand Down
46 changes: 45 additions & 1 deletion dev/test/so_5_extra/sync/moveable_only/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,38 @@ static_assert( std::is_move_constructible_v<reply_t> &&
static_assert( !std::is_copy_constructible_v<reply_t> &&
!std::is_copy_assignable_v<reply_t> );

// reply2 is not default constructible.
class reply2_t final
{
std::string m_value;

public :
reply2_t( std::string value ) : m_value{ std::move(value) } {}

reply2_t( const reply2_t & ) = delete;
reply2_t & operator=( const reply2_t & ) = delete;

reply2_t( reply2_t && ) = default;
reply2_t & operator=( reply2_t && ) = default;

const std::string &
value() const noexcept { return m_value; }
};

struct triple_t final
{
int m_v;
};

class service_t final : public so_5::agent_t
{
public :
service_t( context_t ctx ) : so_5::agent_t{ std::move(ctx) }
{
so_subscribe_self().event( &service_t::on_request );
so_subscribe_self()
.event( &service_t::on_request )
.event( &service_t::on_triple_request )
;
}

private :
Expand All @@ -50,12 +76,20 @@ class service_t final : public so_5::agent_t
{
cmd->make_reply( std::to_string(cmd->request() * 2) );
}

void
on_triple_request( sync_ns::request_mhood_t<triple_t, reply2_t> cmd )
{
cmd->make_reply( std::to_string(cmd->request().m_v * 3) );
}
};

TEST_CASE( "simple shutdown on empty environment" )
{
std::string result1;
std::string result2;
std::string result3;
std::string result4;

run_with_time_limit( [&] {
so_5::launch( [&](so_5::environment_t & env) {
Expand All @@ -69,12 +103,22 @@ TEST_CASE( "simple shutdown on empty environment" )
auto r2 = sync_ns::request_opt_reply<int, reply_t>( svc, 5s, 3 );
result2 = r2->value();

using my_triple_request =
sync_ns::request_reply_t<triple_t, reply2_t>;
auto r3 = my_triple_request::ask_value( svc, 5s, 4 );
result3 = r3.value();

auto r4 = my_triple_request::ask_opt_value( svc, 5s, 5 );
result4 = r4->value();

env.stop();
} );
},
5 );

REQUIRE( result1 == "4" );
REQUIRE( result2 == "6" );
REQUIRE( result3 == "12" );
REQUIRE( result4 == "15" );
}

0 comments on commit fdab8ce

Please sign in to comment.