Thunderbots Project
Loading...
Searching...
No Matches
Observer< T, Clock > Class Template Reference

#include <observer.hpp>

Inheritance diagram for Observer< T, Clock >:

Public Member Functions

 Observer (size_t buffer_size=DEFAULT_BUFFER_SIZE, bool log_buffer_full=true)
 
virtual void receiveValue (T val)
 
virtual double getDataReceivedPerSecond () final
 

Static Public Attributes

static constexpr size_t TIME_BUFFER_SIZE = 5
 

Protected Member Functions

virtual std::optional< T > popMostRecentlyReceivedValue (Duration max_wait_time) final
 
virtual std::optional< T > popLeastRecentlyReceivedValue (Duration max_wait_time) final
 

Static Protected Attributes

static constexpr size_t DEFAULT_BUFFER_SIZE = 1
 

Detailed Description

template<typename T, typename Clock = std::chrono::steady_clock>
class Observer< T, Clock >

This class observes an "Subject<T>". That is, it can be registered with an "Subject<T>" to receive new instances of type T when they are available

Template Parameters
TThe type of object this class is observing
ClockA clock that satisfies the TrivialClock requirements

Constructor & Destructor Documentation

◆ Observer()

template<typename T , typename Clock >
Observer ( size_t  buffer_size = DEFAULT_BUFFER_SIZE,
bool  log_buffer_full = true 
)

Creates a new Observer

Parameters
buffer_sizesize of the buffer
log_buffer_fullwhether or not to log when the buffer is full

Member Function Documentation

◆ getDataReceivedPerSecond()

template<typename T , typename Clock >
double getDataReceivedPerSecond ( )
finalvirtual

Calculate the data received per second using the internal time buffer

Returns
the data received per second

◆ popLeastRecentlyReceivedValue()

template<typename T , typename Clock >
std::optional< T > popLeastRecentlyReceivedValue ( Duration  max_wait_time)
finalprotectedvirtual

Pops the least recently received value and returns it

If no value is available, this will block until:

  • a value becomes available
  • the given amount of time is exceeded
  • the destructor of this class is called
Parameters
max_wait_timeThe maximum duration to wait for a new value before returning
Returns
The value least recently added to the buffer or std::nullopt if none is available

◆ popMostRecentlyReceivedValue()

template<typename T , typename Clock >
std::optional< T > popMostRecentlyReceivedValue ( Duration  max_wait_time)
finalprotectedvirtual

Pops the most recently received value and returns it

If no value is available, this will block until:

  • a value becomes available
  • the given amount of time is exceeded
  • the destructor of this class is called
Parameters
max_wait_timeThe maximum duration to wait for a new value before returning
Returns
The value most recently added to the buffer or std::nullopt if none is available

◆ receiveValue()

template<typename T , typename Clock >
void receiveValue ( val)
virtual

Add the given value to the internal buffer

Parameters
valThe value to add to the internal buffer

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