Github   Telegram
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
concurrent::AsyncEventChannel< Args > Class Template Reference

#include <userver/concurrent/async_event_channel.hpp>

+ Inheritance diagram for concurrent::AsyncEventChannel< Args >:
+ Collaboration diagram for concurrent::AsyncEventChannel< Args >:

Public Types

using Function = typename AsyncEventSource< Args... >::Function
 
- Public Types inherited from concurrent::AsyncEventSource< Args... >
using Function = std::function< void(Args... args)>
 

Public Member Functions

 AsyncEventChannel (std::string name)
 The primary constructor.
 
template<typename UpdaterFunc >
AsyncEventSubscriberScope DoUpdateAndListen (FunctionId id, std::string_view name, Function &&func, UpdaterFunc &&updater)
 For use in UpdateAndListen of specific event channels.
 
template<typename Class , typename UpdaterFunc >
AsyncEventSubscriberScope DoUpdateAndListen (Class *obj, std::string_view name, void(Class::*func)(Args...), UpdaterFunc &&updater)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void SendEvent (Args... args) const
 Send the next event and wait until all the listeners process it.
 
const std::string & Name () const noexcept
 
- Public Member Functions inherited from concurrent::AsyncEventSource< Args... >
AsyncEventSubscriberScope AddListener (Class *obj, std::string_view name, void(Class::*func)(Args...))
 Subscribes to updates from this event source.
 
AsyncEventSubscriberScope AddListener (FunctionId id, std::string_view name, Function &&func)
 A type-erased version of AddListener.
 
void RemoveListener (FunctionId, UnsubscribingKind) noexcept override=0
 Used by AsyncEventSubscriberScope to cancel an event subscription.
 

Additional Inherited Members

- Protected Member Functions inherited from concurrent::AsyncEventSource< Args... >
virtual AsyncEventSubscriberScope DoAddListener (FunctionId id, std::string_view name, Function &&func)=0
 

Detailed Description

template<typename... Args>
class concurrent::AsyncEventChannel< Args >

AsyncEventChannel is an in-process pub-sub with strict FIFO serialization.

Example usage:

enum class WeatherKind { kSunny, kRainy };
class WeatherStorage final {
public:
explicit WeatherStorage(WeatherKind value)
: value_(value), channel_("weather") {}
WeatherKind Get() const { return value_.load(); }
concurrent::AsyncEventSource<WeatherKind>& GetSource() { return channel_; }
template <typename Class>
Class* obj, std::string_view name, void (Class::*func)(WeatherKind)) {
return channel_.DoUpdateAndListen(obj, name, func,
[&] { (obj->*func)(Get()); });
}
void Set(WeatherKind value) {
value_.store(value);
channel_.SendEvent(value);
}
private:
std::atomic<WeatherKind> value_;
};
enum class CoatKind { kJacket, kRaincoat };
class CoatStorage final {
public:
explicit CoatStorage(WeatherStorage& weather_storage) {
weather_subscriber_ = weather_storage.UpdateAndListen(
this, "coats", &CoatStorage::OnWeatherUpdate);
}
~CoatStorage() { weather_subscriber_.Unsubscribe(); }
CoatKind Get() const { return value_.load(); }
private:
void OnWeatherUpdate(WeatherKind weather) {
value_.store(ComputeCoat(weather));
}
static CoatKind ComputeCoat(WeatherKind weather);
std::atomic<CoatKind> value_{};
};
UTEST(AsyncEventChannel, UpdateAndListenSample) {
WeatherStorage weather_storage(WeatherKind::kSunny);
CoatStorage coat_storage(weather_storage);
EXPECT_EQ(coat_storage.Get(), CoatKind::kJacket);
weather_storage.Set(WeatherKind::kRainy);
EXPECT_EQ(coat_storage.Get(), CoatKind::kRaincoat);
}

Definition at line 48 of file async_event_channel.hpp.

Member Typedef Documentation

◆ Function

template<typename... Args>
using concurrent::AsyncEventChannel< Args >::Function = typename AsyncEventSource<Args...>::Function

Definition at line 50 of file async_event_channel.hpp.

Constructor & Destructor Documentation

◆ AsyncEventChannel()

template<typename... Args>
concurrent::AsyncEventChannel< Args >::AsyncEventChannel ( std::string  name)
inlineexplicit

The primary constructor.

Parameters
nameused for diagnostic purposes and is also accessible with Name

Definition at line 54 of file async_event_channel.hpp.

Member Function Documentation

◆ DoUpdateAndListen() [1/2]

template<typename... Args>
template<typename Class , typename UpdaterFunc >
AsyncEventSubscriberScope concurrent::AsyncEventChannel< Args >::DoUpdateAndListen ( Class *  obj,
std::string_view  name,
void(Class::*)(Args...)  func,
UpdaterFunc &&  updater 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 74 of file async_event_channel.hpp.

◆ DoUpdateAndListen() [2/2]

template<typename... Args>
template<typename UpdaterFunc >
AsyncEventSubscriberScope concurrent::AsyncEventChannel< Args >::DoUpdateAndListen ( FunctionId  id,
std::string_view  name,
Function &&  func,
UpdaterFunc &&  updater 
)
inline

For use in UpdateAndListen of specific event channels.

Atomically calls updater, which should invoke func with the previously sent event, and subscribes to new events as if using AddListener.

See also
AsyncEventSource::AddListener

Definition at line 63 of file async_event_channel.hpp.

◆ Name()

template<typename... Args>
const std::string & concurrent::AsyncEventChannel< Args >::Name ( ) const
inlinenoexcept
Returns
the name of this event channel

Definition at line 104 of file async_event_channel.hpp.

◆ SendEvent()

template<typename... Args>
void concurrent::AsyncEventChannel< Args >::SendEvent ( Args...  args) const
inline

Send the next event and wait until all the listeners process it.

Definition at line 84 of file async_event_channel.hpp.


The documentation for this class was generated from the following file: