LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
channelutils.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <util/sll/typegetter.h>
12 #include "channel.h"
13 #include "inparallel.h"
14 #include "task.h"
15 
16 namespace LC::Util
17 {
18  template<typename T>
20  {
21  auto output = std::make_shared<Channel<T>> ();
22  [] (Channel_ptr<T> output, QVector<Channel_ptr<T>> channels) -> Task<void>
23  {
24  co_await InParallel (channels, [] (auto inChan, auto outChan) -> Task<void>
25  {
26  while (auto value = co_await *inChan)
27  outChan->Send (std::move (*value));
28  }, output);
29  output->Close ();
30  } (output, std::move (channels));
31  return output;
32  }
33 
34  template<
35  typename T = void,
36  typename F,
37  typename... Args,
38  typename ItemType =
39  std::conditional_t<
40  !std::is_same_v<T, void>,
41  T,
42  typename ArgType_t<F, 0>::element_type::ItemType_t
43  >
44  >
45  Channel_ptr<ItemType> WithChannel (F&& f, Args&&... args)
46  {
47  auto output = std::make_shared<Channel<ItemType>> ();
48  [] (auto f, Channel_ptr<ItemType> output, auto... args) -> Task<void>
49  {
50  co_await std::invoke (f, output, args...);
51  output->Close ();
52  } (std::forward<F> (f), output, std::forward<Args> (args)...);
53  return output;
54  }
55 
56  template<
57  typename F,
58  typename Conv = std::identity,
59  typename... Args,
60  typename ItemType = RetType_t<F>::ResultType_t,
61  typename ConvertedType = std::invoke_result_t<Conv, ItemType>
62  >
63  Channel_ptr<ConvertedType> ChannelFromSingleResult (F&& f, Conv&& conv, Args&&... args)
64  {
65  auto output = std::make_shared<Channel<ConvertedType>> ();
66  [] (auto f, auto conv, Channel_ptr<ConvertedType> output, auto... args) -> Task<void>
67  {
68  auto res = co_await std::invoke (f, args...);
69  output->Send (conv (std::move (res)));
70  output->Close ();
71  } (std::forward<F> (f), std::forward<Conv> (conv), output, std::forward<Args> (args)...);
72  return output;
73  }
74 }
Channel_ptr< ItemType > WithChannel(F &&f, Args &&... args)
Definition: channelutils.h:45
Task< QVector< T >, Exts... > InParallel(Cont< Task< T, Exts... >> tasks)
Definition: inparallel.h:21
Channel_ptr< T > MergeChannels(QVector< Channel_ptr< T >> channels)
Definition: channelutils.h:19
std::shared_ptr< Channel< T > > Channel_ptr
Definition: channelfwd.h:19
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:1083
Channel_ptr< ConvertedType > ChannelFromSingleResult(F &&f, Conv &&conv, Args &&... args)
Definition: channelutils.h:63
The type of this radio station.