STX  1.0.0
List of all members
stx::SpanReport Class Reference

#include <report.h>

Detailed Description

SpanReport is an error-report type containing a reference to an error-string stating the cause of the error from the error type of a Result<T, E>.

When to use SpanReport against FixedReport

TIP: SpanReport is named after std::span as it is only a type-erased reference-view of the error string.

Constructing a SpanReport for your error type

NOTE: character literals (i.e. "A STRING") have static storage duration and live throughout the program, constructing a SpanReport with them just forwards a reference to them, it is safe to do this)

namespace net{
enum class Error {
ConnectionError,
ServerClosed,
ServerUnreachable
};
};
SpanReport operator>>(ReportQuery, net::Error const& err) noexcept {
switch(err){
case net::Error::ConnectionError:
return SpanReport("Connection error occurred");
case net::Error::ServerClosed:
return SpanReport("Server closed unexpectedly");
case net::Error::ServerUnreachable:
return SpanReport("Server was unreachable");
}
}
namespace net{
class Error {
// returns a reference to the error string
std::string const & what() const noexcept;
// returns a string_view to the error string
std::string_view better_what() const noexcept;
// returns a const string_view reference of the error
std::string_view const& best_what() const noexcept;
// `dangerous_what` creates a new string and returns it, totally
// dangerous as a constructor for `SpanReport` because
// `SpanReport` binds a reference to its constructor argument
// which in this case is a temporary and will outlive the
// `SpanReport` if the `SpanReport` is returned from the report
query
// below.
//
std::string dangerous_what() const noexcept;
};
};
SpanReport operator>>(ReportQuery, net::Error const& err) noexcept {
return SpanReport(err.what());
}

Exception-safety

Ensure to not throw exceptions.

Constructor & Destructor Documentation

◆ SpanReport() [1/5]

constexpr stx::SpanReport::SpanReport ( )
inlinenoexcept

◆ SpanReport() [2/5]

constexpr stx::SpanReport::SpanReport ( const char *  str,
size_t  size 
)
inlinenoexcept

size: string size excluding null-terminator

◆ SpanReport() [3/5]

constexpr stx::SpanReport::SpanReport ( std::string_view const &  str)
inlineexplicitnoexcept

◆ SpanReport() [4/5]

constexpr stx::SpanReport::SpanReport ( SpanReport const &  )
defaultnoexcept

◆ SpanReport() [5/5]

constexpr stx::SpanReport::SpanReport ( SpanReport &&  )
defaultnoexcept

◆ ~SpanReport()

stx::SpanReport::~SpanReport ( )
defaultnoexcept

Member Function Documentation

◆ operator=() [1/2]

constexpr SpanReport& stx::SpanReport::operator= ( SpanReport const &  )
defaultnoexcept

◆ operator=() [2/2]

constexpr SpanReport& stx::SpanReport::operator= ( SpanReport &&  )
defaultnoexcept

◆ what()

constexpr std::string_view stx::SpanReport::what ( ) const
inlinenoexcept

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