HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
tmc51x0::Result< T > Class Template Reference

Result type for operations that return a value. More...

#include <tmc51x0_result.hpp>

Public Member Functions

 Result (T &&value) noexcept
 Construct a successful result with value.
 
 Result (const T &value) noexcept
 Construct a successful result with value (copy)
 
 Result (ErrorCode error) noexcept
 Construct an error result.
 
 operator bool () const noexcept
 Check if result is OK (bool operator for cleaner syntax)
 
bool IsOk () const noexcept
 Check if result is OK.
 
bool IsErr () const noexcept
 Check if result is an error.
 
ErrorCode Error () const noexcept
 Get the error code.
 
const char * ErrorMessage () const noexcept
 Get human-readable error message.
 
T & Value () noexcept
 Get the result value (mutable reference)
 
const T & Value () const noexcept
 Get the result value (const reference)
 
ValueOr (const T &default_value) const noexcept
 Get the result value or a default.
 
template<std::size_t N>
std::enable_if_t< N==0, ErrorCodeget () const &noexcept
 Support structured bindings: auto [err, value] = result;.
 
template<std::size_t N>
std::enable_if_t< N==1, const T & > get () const &noexcept
 
template<std::size_t N>
std::enable_if_t< N==0, ErrorCodeget () &noexcept
 
template<std::size_t N>
std::enable_if_t< N==1, T & > get () &noexcept
 
template<std::size_t N>
std::enable_if_t< N==0, ErrorCodeget () &&noexcept
 
template<std::size_t N>
std::enable_if_t< N==1, T && > get () &&noexcept
 

Private Attributes

ErrorCode error_
 
value_
 

Detailed Description

template<typename T>
class tmc51x0::Result< T >

Result type for operations that return a value.

Template Parameters
TThe value type

Provides explicit error handling with rich error information. Use the bool operator or IsOk()/IsErr() for checking success.

auto result = driver.GetActualPosition();
if (result) { // Bool operator
float pos = result.Value();
} else {
ErrorCode err = result.Error();
}
ErrorCode
Error codes for TMC51x0 operations.
Definition tmc51x0_result.hpp:18

Constructor & Destructor Documentation

◆ Result() [1/3]

template<typename T >
tmc51x0::Result< T >::Result ( T && value)
inlineexplicitnoexcept

Construct a successful result with value.

◆ Result() [2/3]

template<typename T >
tmc51x0::Result< T >::Result ( const T & value)
inlineexplicitnoexcept

Construct a successful result with value (copy)

◆ Result() [3/3]

template<typename T >
tmc51x0::Result< T >::Result ( ErrorCode error)
inlineexplicitnoexcept

Construct an error result.

Member Function Documentation

◆ Error()

template<typename T >
ErrorCode tmc51x0::Result< T >::Error ( ) const
inlinenoexcept

Get the error code.

Returns
Error code (OK if successful)
Here is the caller graph for this function:

◆ ErrorMessage()

template<typename T >
const char * tmc51x0::Result< T >::ErrorMessage ( ) const
inlinenoexcept

Get human-readable error message.

Returns
Error message string
Here is the call graph for this function:

◆ get() [1/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==0, ErrorCode > tmc51x0::Result< T >::get ( ) &&
inlinenoexcept

◆ get() [2/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==1, T && > tmc51x0::Result< T >::get ( ) &&
inlinenoexcept

◆ get() [3/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==0, ErrorCode > tmc51x0::Result< T >::get ( ) &
inlinenoexcept

◆ get() [4/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==1, T & > tmc51x0::Result< T >::get ( ) &
inlinenoexcept

◆ get() [5/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==0, ErrorCode > tmc51x0::Result< T >::get ( ) const &
inlinenoexcept

Support structured bindings: auto [err, value] = result;.

◆ get() [6/6]

template<typename T >
template<std::size_t N>
std::enable_if_t< N==1, const T & > tmc51x0::Result< T >::get ( ) const &
inlinenoexcept

◆ IsErr()

template<typename T >
bool tmc51x0::Result< T >::IsErr ( ) const
inlinenoexcept

Check if result is an error.

Returns
true if operation failed

◆ IsOk()

template<typename T >
bool tmc51x0::Result< T >::IsOk ( ) const
inlinenoexcept

Check if result is OK.

Returns
true if operation succeeded
Here is the caller graph for this function:

◆ operator bool()

template<typename T >
tmc51x0::Result< T >::operator bool ( ) const
inlineexplicitnoexcept

Check if result is OK (bool operator for cleaner syntax)

Returns
true if operation succeeded
if (result.IsOk()) { // success
float pos = result.Value();
}
if (result.IsErr()) { // error
ErrorCode err = result.Error();
}

◆ Value() [1/2]

template<typename T >
const T & tmc51x0::Result< T >::Value ( ) const
inlinenoexcept

Get the result value (const reference)

Warning
Only call if IsOk() returns true
Returns
Const reference to the value

◆ Value() [2/2]

template<typename T >
T & tmc51x0::Result< T >::Value ( )
inlinenoexcept

Get the result value (mutable reference)

Warning
Only call if IsOk() returns true
Returns
Reference to the value

◆ ValueOr()

template<typename T >
T tmc51x0::Result< T >::ValueOr ( const T & default_value) const
inlinenoexcept

Get the result value or a default.

Parameters
default_valueValue to return if result is an error
Returns
The result value if OK, otherwise default_value
Here is the call graph for this function:

Member Data Documentation

◆ error_

template<typename T >
ErrorCode tmc51x0::Result< T >::error_
private

◆ value_

template<typename T >
T tmc51x0::Result< T >::value_
private

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