ILLIXR: Illinois Extended Reality Testbed
Classes | Public Types | Public Member Functions | List of all members
ILLIXR::switchboard Class Reference

A manager for typesafe, threadsafe, named event-streams (called topics). More...

#include <switchboard.hpp>

Classes

class  event
 Virtual class for event types. More...
 
class  event_wrapper
 Helper class for making event types. More...
 
class  reader
 A handle which can read the latest event on a topic. More...
 
class  writer
 A handle which can publish events to a topic. More...
 

Public Types

template<typename specific_event >
using ptr = std::shared_ptr< specific_event >
 The type of shared pointer returned by switchboard. More...
 

Public Member Functions

 switchboard (const phonebook *pb)
 
template<typename specific_event >
void schedule (plugin_id_t plugin_id, std::string topic_name, std::function< void(ptr< const specific_event > &&, std::size_t)> fn)
 Schedules the callback fn every time an event is published to topic_name. More...
 
template<typename specific_event >
writer< specific_event > get_writer (const std::string &topic_name)
 Gets a handle to publish to the topic topic_name. More...
 
template<typename specific_event >
reader< specific_event > get_reader (const std::string &topic_name)
 Gets a handle to read to the latest value from the topic topic_name. More...
 
template<typename specific_event >
buffered_reader< specific_event > get_buffered_reader (const std::string &topic_name)
 
void stop ()
 Stops calling switchboard callbacks. More...
 

Detailed Description

A manager for typesafe, threadsafe, named event-streams (called topics).

// Get a reader on topic1
switchboard::reader<topic1_type> topic1 = switchboard.get_reader<topic1_type>("topic1");
// Get a writer on topic2
switchboard::writer<topic2_type> topic2 = switchboard.get_writer<topic2_type>("topic2");
while (true) {
// Read topic 1
switchboard::ptr<topic1_type> event1 = topic1.get_rw();
// Write to topic 2 using topic 1 input
topic2.put(topic2.allocate<topic2_type>( do_something(event1->foo) ));
}
// Read topic 3 synchronously
switchboard.schedule<topic3_type>(plugin_id, "topic3", [&](switchboard::ptr<topic3_type> event3, std::size_t it) {
// This is a lambda expression
// https://en.cppreference.com/w/cpp/language/lambda
std::cout << "Got a new event on topic3: " << event3->foo << " for iteration " << it << std::endl;
});
switchboard(const phonebook *pb)
Definition: switchboard.hpp:621

Member Typedef Documentation

◆ ptr

template<typename specific_event >
using ILLIXR::switchboard::ptr = std::shared_ptr<specific_event>

The type of shared pointer returned by switchboard.

TODO: Make this agnostic to the type of ptr Currently, it depends on ptr == shared_ptr

Constructor & Destructor Documentation

◆ switchboard()

ILLIXR::switchboard::switchboard ( const phonebook pb)
inline

If pb is null, then logging is disabled.

Member Function Documentation

◆ get_reader()

template<typename specific_event >
reader<specific_event> ILLIXR::switchboard::get_reader ( const std::string &  topic_name)
inline

Gets a handle to read to the latest value from the topic topic_name.

This is safe to be called from any thread.

Exceptions
Iftopic already exists, and its type does not match the event.

◆ get_writer()

template<typename specific_event >
writer<specific_event> ILLIXR::switchboard::get_writer ( const std::string &  topic_name)
inline

Gets a handle to publish to the topic topic_name.

This is safe to be called from any thread.

Exceptions
Iftopic already exists, and its type does not match the event.

◆ schedule()

template<typename specific_event >
void ILLIXR::switchboard::schedule ( plugin_id_t  plugin_id,
std::string  topic_name,
std::function< void(ptr< const specific_event > &&, std::size_t)>  fn 
)
inline

Schedules the callback fn every time an event is published to topic_name.

Switchboard maintains a threadpool to call fn.

This is safe to be called from any thread.

Exceptions
iftopic already exists and its type does not match the event.

◆ stop()

void ILLIXR::switchboard::stop ( )
inline

Stops calling switchboard callbacks.

This is safe to be called from any thread.

Leave topics in place, so existing reader/writer handles will not crash.


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