Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
astarte::device::Data Class Reference

Represents a single Astarte data value. More...

#include <data.hpp>

Public Member Functions

template<DataAllowedType T>
 Data (const T &value)
 Constructs a Data object from a value of any DataAllowedType.
 
template<DataAllowedType T>
auto into () const -> std::conditional_t< std::is_same_v< T, std::string_view >, std::string_view, const T & >
 Converts the Astarte data class to the specified type T.
 
template<DataAllowedType T>
auto try_into () const -> std::optional< T >
 Attempts to convert the Astarte data class to the specified type T safely.
 
auto get_type () const -> Type
 Gets the type of the data contained in this class instance.
 
auto get_raw_data () const -> const std::variant< int32_t, int64_t, double, bool, std::string, std::vector< uint8_t >, std::chrono::system_clock::time_point, std::vector< int32_t >, std::vector< int64_t >, std::vector< double >, std::vector< bool >, std::vector< std::string >, std::vector< std::vector< uint8_t > >, std::vector< std::chrono::system_clock::time_point > > &
 Returns the raw data variant contained in this class instance.
 
auto operator== (const Data &other) const -> bool
 Overloader for the comparison operator ==.
 
auto operator!= (const Data &other) const -> bool
 Overloader for the comparison operator !=.
 

Detailed Description

Represents a single Astarte data value.

The Data class acts as a versatile container for all data types supported by the Astarte platform. It abstracts away the underlying C++ types, providing a unified interface for handling data sent to and received from Astarte.

It uses std::variant internally to store different types, ensuring type safety while offering flexibility. The DataAllowedType concept restricts which C++ types can be stored, mapping them to Astarte's data model.

Constructor & Destructor Documentation

◆ Data()

template<DataAllowedType T>
astarte::device::Data::Data ( const T & value)
inlineexplicit

Constructs a Data object from a value of any DataAllowedType.

The constructor implicitly copies the provided value into the internal storage. This ensures that the Data object manages its own data lifetime independently of the input value.

Template Parameters
TThe type of the value, must satisfy DataAllowedType.
Parameters
[in]valueThe content to initialize the Astarte data instance with.

Member Function Documentation

◆ get_raw_data()

auto astarte::device::Data::get_raw_data ( ) const -> const std::variant< int32_t, int64_t, double, bool, std::string, std::vector< uint8_t >, std::chrono::system_clock::time_point, std::vector< int32_t >, std::vector< int64_t >, std::vector< double >, std::vector< bool >, std::vector< std::string >, std::vector< std::vector< uint8_t > >, std::vector< std::chrono::system_clock::time_point > > &
nodiscard

Returns the raw data variant contained in this class instance.

Returns
A constant reference to the internal variant containing one of the possible data types.

◆ get_type()

auto astarte::device::Data::get_type ( ) const -> Type
nodiscard

Gets the type of the data contained in this class instance.

Returns
The Astarte Type of the content of this class instance.

◆ into()

template<DataAllowedType T>
auto astarte::device::Data::into ( ) const -> std::conditional_t<std::is_same_v<T, std::string_view>, std::string_view, const T&>
inlinenodiscard

Converts the Astarte data class to the specified type T.

This method performs a direct extraction of the stored value using std::get<T>. It is the caller's responsibility to ensure that the Data object currently holds a value of type T.

Template Parameters
TThe target type to convert to, must satisfy DataAllowedType.
Returns
A constant reference to the value contained in the class instance.
Exceptions
std::bad_variant_accessIf the Data object does not contain a T.

◆ operator!=()

auto astarte::device::Data::operator!= ( const Data & other) const -> bool
nodiscard

Overloader for the comparison operator !=.

Parameters
[in]otherThe object to compare to.
Returns
True when different, false otherwise.

◆ operator==()

auto astarte::device::Data::operator== ( const Data & other) const -> bool
nodiscard

Overloader for the comparison operator ==.

Parameters
[in]otherThe object to compare to.
Returns
True when equal, false otherwise.

◆ try_into()

template<DataAllowedType T>
auto astarte::device::Data::try_into ( ) const -> std::optional<T>
inlinenodiscard

Attempts to convert the Astarte data class to the specified type T safely.

This method attempts to extract the stored value as type T. If the Data object currently holds a value of type T, an std::optional containing that value is returned.

Template Parameters
TThe target type to convert to, must satisfy DataAllowedType.
Returns
std::optional containing the value if the types match, or std::nullopt otherwise.

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