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

An owned, shareable message value. More...

#include <xronos/sdk/value.hh>

Public Member Functions

constexpr Value () noexcept=default
 Constructs an empty Value that holds nothing.
 Value (const T &value)
 Constructs a Value holding a copy of value.
 Value (const ValueView< T > &view)
 Constructs a Value from a ValueView, retaining the viewed value.
 Value (T &&value)
 Constructs a Value holding a value move-constructed from value.
auto as_shared_ptr () const -> std::shared_ptr< const T >
 Retrieve the held value as a std::shared_ptr.
auto get () const noexcept -> const T *
 Retrieve a pointer to the held value, or nullptr if this Value is empty.
 operator bool () const noexcept
 Check whether this Value holds a value.
auto operator* () const noexcept -> const T &
 Access the held value.
auto operator-> () const noexcept -> const T *
 Access members of the held value.

Static Public Member Functions

static auto from_shared_ptr (std::shared_ptr< const T > pointer) -> Value
 Constructs a Value that shares ownership of an externally managed value instead of copying it.
template<class Deleter>
static auto from_unique_ptr (std::unique_ptr< T, Deleter > pointer) -> Value
 Constructs a Value that takes over exclusive ownership of an externally allocated value, without copying it.
template<class... Args>
static auto make (Args &&... args) -> Value
 Constructs a Value holding a value constructed in place from args.

Friends

auto operator== (const Value &value, std::nullptr_t) noexcept -> bool
 Check whether this Value is empty (value == nullptr is equivalent to !value).

Detailed Description

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

An owned, shareable message value.

The held value remains valid for as long as the Value object itself (or any copy of it) is alive. Copying a Value is cheap: values are immutable while shared, so copies share the underlying storage instead of duplicating it.

Use a Value to keep a value received in a reaction beyond the reaction's execution (by copying the ValueView returned from Trigger::get() into a Value), to share a value between reactors without copying it, or to send the same value to multiple effects Reaction effect classes. without boxing it again.

A default-constructed Value is empty and holds nothing.

Template Parameters
TThe type of the held value.

Constructor & Destructor Documentation

◆ Value()

template<class T>
xronos::sdk::Value< T >::Value ( const ValueView< T > & view)
explicit

Constructs a Value from a ValueView, retaining the viewed value.

This is the way to keep a value received in a reaction beyond the reaction's execution. If view is absent, the constructed Value is empty.

Member Function Documentation

◆ as_shared_ptr()

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

Retrieve the held value as a std::shared_ptr.

The returned pointer keeps the value alive independently of this Value, which makes it suitable for handing values 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 this 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 held value, or nullptr if this Value is empty.

◆ from_shared_ptr()

template<class T>
auto xronos::sdk::Value< T >::from_shared_ptr ( std::shared_ptr< const T > pointer) -> Value
staticnodiscard

Constructs a Value that shares ownership of an externally managed value instead of copying it.

Use this to pass values owned by another framework (for example, messages received from a ROS 2 subscription) into the reactor program without copying the payload. The value must not be modified through any other reference for as long as it is shared – the same contract that sharing a std::shared_ptr<const T> implies everywhere else. To hand over exclusive ownership of a value instead, use from_unique_ptr().

A null pointer yields an empty Value.

Note
Whether the payload is shared or copied is a storage detail of the value type: small trivially copyable types are stored by value and are copied out of the given pointer instead (see as_shared_ptr()).

◆ from_unique_ptr()

template<class T>
template<class Deleter>
auto xronos::sdk::Value< T >::from_unique_ptr ( std::unique_ptr< T, Deleter > pointer) -> Value
staticnodiscard

Constructs a Value that takes over exclusive ownership of an externally allocated value, without copying it.

Unlike from_shared_ptr(), no other reference to the value can remain, so no immutability promise from the caller is needed. Custom deleters are preserved and invoked when the last Value sharing the storage is destroyed (for example, the message deleters used by ROS 2 subscriptions taking std::unique_ptr messages).

A null pointer yields an empty Value.

Note
Small trivially copyable types are stored by value and are copied out of the given pointer instead (see as_shared_ptr()).

◆ get()

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

Retrieve a pointer to the held value, or nullptr if this Value is empty.

The pointer remains valid for as long as any Value sharing the same storage is alive.

◆ make()

template<class T>
template<class... Args>
auto xronos::sdk::Value< T >::make ( Args &&... args) -> Value
staticnodiscard

Constructs a Value holding a value constructed in place from args.

This is analogous to std::make_shared().

◆ operator*()

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

Access the held value.

The behavior is undefined if this Value is empty (asserted in debug builds).

◆ operator->()

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

Access members of the held value.

The behavior is undefined if this Value is empty (asserted in debug builds).


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