-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
join_with adaptor is extracted from ureact/adaptor/join.hpp
- Loading branch information
Showing
5 changed files
with
123 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// Copyright (C) 2014-2017 Sebastian Jeckel. | ||
// Copyright (C) 2020-2023 Krylov Yaroslav. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef UREACT_ADAPTOR_JOIN_WITH_HPP | ||
#define UREACT_ADAPTOR_JOIN_WITH_HPP | ||
|
||
#include <ureact/adaptor/process.hpp> | ||
#include <ureact/detail/container_type_traits.hpp> | ||
|
||
UREACT_BEGIN_NAMESPACE | ||
|
||
namespace detail | ||
{ | ||
|
||
struct JoinWithAdaptor : Adaptor | ||
{ | ||
/*! | ||
* @brief Emits the sequence obtained from flattening received event value, with the delimiter in between elements | ||
* | ||
* The delimiter can be a single element or an iterable container of elements. | ||
*/ | ||
template <typename InE, typename Pattern> | ||
UREACT_WARN_UNUSED_RESULT constexpr auto operator()( | ||
const events<InE>& source, Pattern&& pattern ) const | ||
{ | ||
using E = container_value_t<InE>; | ||
return process<E>( source, // | ||
[first = true, pattern = std::forward<Pattern>( pattern )] // | ||
( event_range<InE> range, event_emitter<E> out ) mutable { | ||
if( !first ) | ||
{ | ||
if constexpr( std::is_convertible_v<decltype( pattern ), E> ) | ||
{ | ||
out << pattern; | ||
} | ||
else if constexpr( std::is_convertible_v<container_value_t<decltype( pattern )>, | ||
E> ) | ||
{ | ||
for( const auto& i : pattern ) | ||
out << i; | ||
} | ||
else | ||
{ | ||
static_assert( always_false<Pattern>, "Unsupported separator type" ); | ||
} | ||
} | ||
first = false; | ||
|
||
for( const auto& e : range ) | ||
for( const auto& i : e ) | ||
out << i; | ||
} ); | ||
} | ||
|
||
template <typename Pattern> | ||
UREACT_WARN_UNUSED_RESULT constexpr auto operator()( Pattern&& pattern ) const | ||
{ | ||
return make_partial<JoinWithAdaptor>( std::forward<Pattern>( pattern ) ); | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
inline constexpr detail::JoinWithAdaptor join_with; | ||
|
||
UREACT_END_NAMESPACE | ||
|
||
#endif // UREACT_ADAPTOR_JOIN_WITH_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Copyright (C) 2020-2023 Krylov Yaroslav. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
#include "ureact/adaptor/join_with.hpp" | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
#include "catch2_extra.hpp" | ||
#include "ureact/adaptor/collect.hpp" | ||
#include "ureact/events.hpp" | ||
|
||
// Based on https://en.cppreference.com/w/cpp/ranges/join_with_view | ||
TEST_CASE( "ureact::join_with" ) | ||
{ | ||
ureact::context ctx; | ||
|
||
auto src = ureact::make_source<std::string_view>( ctx ); | ||
ureact::events<char> chars_1; | ||
ureact::events<char> chars_2; | ||
|
||
using namespace std::literals; | ||
|
||
SECTION( "Functional syntax" ) | ||
{ | ||
chars_1 = ureact::join_with( src, ' ' ); | ||
chars_2 = ureact::join_with( src, ".."sv ); | ||
} | ||
SECTION( "Piped syntax" ) | ||
{ | ||
chars_1 = src | ureact::join_with( ' ' ); | ||
chars_2 = src | ureact::join_with( ".."sv ); | ||
} | ||
|
||
const auto result_1 = ureact::collect<std::basic_string>( chars_1 ); | ||
const auto result_2 = ureact::collect<std::basic_string>( chars_2 ); | ||
|
||
using namespace std::literals; | ||
for( auto s : { "This"sv, "is"sv, "a"sv, "test."sv } ) | ||
src << s; | ||
|
||
CHECK( result_1.get() == "This is a test."s ); | ||
CHECK( result_2.get() == "This..is..a..test."s ); | ||
} |