Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Added cast operator
Browse files Browse the repository at this point in the history
  • Loading branch information
clechasseur committed Apr 26, 2017
1 parent 15f2fef commit 94c31cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/coveo/linq/detail/linq_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ class average_impl
}
};

// Selector implementation used by cast operator.
template<typename U>
class cast_selector
{
public:
template<typename T>
auto operator()(T&& obj) const -> U {
return static_cast<U>(obj);
}
};

// Implementation of concat operator.
template<typename Seq2>
class concat_impl
Expand Down
13 changes: 13 additions & 0 deletions lib/coveo/linq/linq.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ auto average(const F& num_f)
return detail::average_impl<F>(num_f);
}

// C++ LINQ operator: cast
// .NET equivalent: Cast

// Operator that casts (using static_cast) the elements
// in a sequence to a different type.
template<typename U>
auto cast()
-> detail::select_impl<detail::indexless_selector_proxy<detail::cast_selector<U>>>
{
return detail::select_impl<detail::indexless_selector_proxy<detail::cast_selector<U>>>(
detail::indexless_selector_proxy<detail::cast_selector<U>>(detail::cast_selector<U>()));
}

// C++ LINQ operator: concat
// .NET equivalent: Concat

Expand Down
14 changes: 14 additions & 0 deletions tests/coveo/linq/linq_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ void linq_tests()
| average([](int i) { return i; }));
}

// cast
{
const std::vector<int> vi = { 42, 23, 66 };
const std::vector<double> vd = { 42.0, 23.0, 66.0 };

using namespace coveo::linq;
auto seq_d = from(vi)
| cast<double>();
COVEO_ASSERT(detail::equal(std::begin(seq_d), std::end(seq_d),
std::begin(vd), std::end(vd)));
COVEO_ASSERT(seq_d.has_fast_size());
COVEO_ASSERT(seq_d.size() == vd.size());
}

// concat
{
const std::vector<int> a = { 42, 23 };
Expand Down

0 comments on commit 94c31cf

Please sign in to comment.