xronos-sdk 0.14.0
Xronos C++ SDK
Loading...
Searching...
No Matches
xronos::sdk::ValueView< T > Class Template Reference

A borrowed view of a message value. More...

#include <xronos/sdk/value.hh>

Public Member Functions

constexpr ValueView () noexcept=default
 Constructs an absent view that refers to nothing.
auto as_shared_ptr () const -> std::shared_ptr< const T >
 Retrieve the viewed value as a std::shared_ptr.
auto get () const noexcept -> const T *
 Retrieve a pointer to the viewed value, or nullptr if the view is absent.
 operator bool () const noexcept
 Check whether the view refers to a value.
auto operator* () const noexcept -> const T &
 Access the viewed value.
auto operator-> () const noexcept -> const T *
 Access members of the viewed value.

Friends

auto operator== (const ValueView &view, std::nullptr_t) noexcept -> bool
 Check whether the view is absent (view == nullptr is equivalent to !view).

Detailed Description

template<class T>
class xronos::sdk::ValueView< T >

A borrowed view of a message value.

A ValueView does not own the value it refers to: it forwards to a value stored by the runtime, which remains valid for the duration of the current reaction handler. Reading through a view performs no copy.

Views cannot be copied or moved: a view is a scope-bound borrow, and retaining one beyond the reaction handler it was obtained in would leave it dangling. Initializing a local from a fresh view (auto view = input.get();) works – that is guaranteed copy elision, not a copy. To keep the viewed value, copy the view into a Value :

if (auto view = input_.get()) {
self().last_input_ = Value<Message>{view};
}
An owned, shareable message value.
Definition value.hh:59

A default-constructed (or absent) view refers to nothing; this is what Trigger::get() returns when no event is present.

Template Parameters
TThe type of the viewed value.

Member Function Documentation

◆ as_shared_ptr()

template<class T>
auto xronos::sdk::ValueView< T >::as_shared_ptr ( ) const -> std::shared_ptr< const T >
nodiscard

Retrieve the viewed value as a std::shared_ptr.

Unlike the view itself, the returned pointer keeps the value alive beyond the current reaction handler, which makes it suitable for handing a received value to another framework (for example, publishing to ROS 2). Note that pointer identity is not necessarily carried through: for payload types in shared storage the returned pointer shares ownership with the stored value, while small trivially copyable payloads are stored by value and are copied into a fresh allocation on every call.

Returns
A shared pointer to the viewed value, or nullptr if the view is absent.

◆ get()

template<class T>
auto xronos::sdk::ValueView< T >::get ( ) const -> const T *
nodiscardnoexcept

Retrieve a pointer to the viewed value, or nullptr if the view is absent.

The pointer is only valid for the duration of the current reaction handler.

◆ operator bool()

template<class T>
xronos::sdk::ValueView< T >::operator bool ( ) const
explicitnoexcept

Check whether the view refers to a value.

For a view returned by a trigger, this indicates whether an event with a value is currently present.

◆ operator*()

template<class T>
auto xronos::sdk::ValueView< T >::operator* ( ) const -> const T &
noexcept

Access the viewed value.

The behavior is undefined if the view is absent (asserted in debug builds).

◆ operator->()

template<class T>
auto xronos::sdk::ValueView< T >::operator-> ( ) const -> const T *
noexcept

Access members of the viewed value.

The behavior is undefined if the view is absent (asserted in debug builds).


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