Skip to content

Commit

Permalink
Merge pull request #60 from fried-water/move_support
Browse files Browse the repository at this point in the history
Add move only type support.
  • Loading branch information
joboccara authored Oct 12, 2020
2 parents 639afdd + 9efa49c commit 56f7e33
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
18 changes: 14 additions & 4 deletions include/pipes/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
namespace pipes
{

// range >>= pipeline
// range >>= pipeline (rvalue ranges)

template<typename Range, typename Pipeline, detail::IsARange<Range> = true, detail::IsAPipeline<Pipeline> = true>
void operator>>=(Range&& range, Pipeline&& pipeline)
std::enable_if_t<!std::is_lvalue_reference<Range>::value> operator>>=(Range&& range, Pipeline&& pipeline)
{
using std::begin;
using std::end;
using std::begin;
using std::end;
std::copy(std::make_move_iterator(begin(range)), std::make_move_iterator(end(range)), pipeline);
}

// range >>= pipeline (lvalue ranges)

template<typename Range, typename Pipeline, detail::IsARange<Range> = true, detail::IsAPipeline<Pipeline> = true>
std::enable_if_t<std::is_lvalue_reference<Range>::value> operator>>=(Range&& range, Pipeline&& pipeline)
{
using std::begin;
using std::end;
std::copy(begin(range), end(range), pipeline);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pipes/override.hpp"

#include <algorithm>
#include <memory>
#include <vector>

TEST_CASE("transform")
Expand Down Expand Up @@ -77,3 +78,26 @@ TEST_CASE("transform operator=")
REQUIRE(results1.size() == 1);
REQUIRE(results2.size() == 0);
}

TEST_CASE("transform move_only types")
{
// Can't use initializer list since it needs to copy
std::vector<std::unique_ptr<int>> input;
input.push_back(std::make_unique<int>(1));
input.push_back(std::make_unique<int>(2));

std::vector<std::unique_ptr<int>> result;

std::move(input) >>= pipes::transform([](auto&& ptr) -> decltype(auto) { return std::move(ptr); })
>>= pipes::push_back(result);

// unique_ptr op == compares ptr not value
REQUIRE(result.size() == 2);
REQUIRE(*result[0] == 1);
REQUIRE(*result[1] == 2);

// input elements were moved from
REQUIRE(input.size() == 2);
REQUIRE(input[0] == nullptr);
REQUIRE(input[1] == nullptr);
}

0 comments on commit 56f7e33

Please sign in to comment.