IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::protocol::Success Struct Reference

Represents a success response message from the server. More...

#include <success.hpp>

Public Member Functions

std::string toString () const
 Converts the success message to a human-readable string.
 
bool hasBody () const
 
bool hasInt () const
 
bool hasDouble () const
 
bool hasString () const
 

Static Public Member Functions

static Success from (const Assignment &msg, std::optional< irsol::types::protocol_value_t > overrideValue=std::nullopt)
 Creates a success message from an Assignment.
 
static Success from (const Command &msg)
 Creates a success message from a Command.
 
static Success from (const Inquiry &msg, irsol::types::protocol_value_t result)
 Creates a success message from an Inquiry.
 
static Success asStatus (const std::string &identifier, irsol::types::protocol_value_t value)
 Creates a standalone status message with a value.
 

Public Attributes

std::string identifier
 The identifier associated with the success response.
 
InMessageKind source
 The kind of the incoming message that triggered this response.
 
std::optional< irsol::types::protocol_value_tbody {}
 Optional result or data associated with the response.
 

Private Member Functions

 Success (const std::string &identifier, InMessageKind source, std::optional< irsol::types::protocol_value_t > body=std::nullopt)
 

Detailed Description

Represents a success response message from the server.

A Success message acknowledges the successful processing of an incoming message (such as an irsol::protocol::Assignment, irsol::protocol::Command, or irsol::protocol::Inquiry). It contains the originating identifier, the kind of message that triggered the response, and optionally a result value (e.g., for assignments and inquiries).

Can be stored in a irsol::protocol::OutMessage variant.

Definition at line 38 of file success.hpp.

Constructor & Destructor Documentation

◆ Success()

irsol::protocol::Success::Success ( const std::string &  identifier,
InMessageKind  source,
std::optional< irsol::types::protocol_value_t body = std::nullopt 
)
private

Definition at line 10 of file success.cpp.

15{}
std::string validateIdentifier(const std::string &identifier)
Validate that a string is a valid protocol identifier.
Definition utils.hpp:55
std::string identifier
The identifier associated with the success response.
Definition success.hpp:41
std::optional< irsol::types::protocol_value_t > body
Optional result or data associated with the response.
Definition success.hpp:47
InMessageKind source
The kind of the incoming message that triggered this response.
Definition success.hpp:44

Member Function Documentation

◆ asStatus()

static Success irsol::protocol::Success::asStatus ( const std::string &  identifier,
irsol::types::protocol_value_t  value 
)
inlinestatic

Creates a standalone status message with a value.

Useful in cases where the server responds without having access to the original incoming message (e.g., asynchronous events).

Parameters
identifierThe identifier associated with the result.
valueThe value to return.
Returns
A success response with the given identifier and value.

Definition at line 116 of file success.hpp.

117 {
118 return Success(identifier, InMessageKind::INQUIRY, std::make_optional(value));
119 }
Success(const std::string &identifier, InMessageKind source, std::optional< irsol::types::protocol_value_t > body=std::nullopt)
Definition success.cpp:10
auto value

◆ from() [1/3]

static Success irsol::protocol::Success::from ( const Assignment msg,
std::optional< irsol::types::protocol_value_t overrideValue = std::nullopt 
)
inlinestatic

Creates a success message from an Assignment.

Parameters
msgThe original assignment message.
overrideValueOptional value to return instead of the original. This is sometimes set by the server, when the original value of the Assignment message was not suitable for storage (e.g. due to precision limitations or min/max constraints).
Returns
A success response based on the assignment.

Definition at line 75 of file success.hpp.

78 {
79 return Success(
80 msg.identifier,
82 overrideValue.has_value() ? overrideValue : std::make_optional(msg.value));
83 }
auto msg

◆ from() [2/3]

static Success irsol::protocol::Success::from ( const Command msg)
inlinestatic

Creates a success message from a Command.

Parameters
msgThe original command message.
Returns
A success response based on the command.

Definition at line 90 of file success.hpp.

91 {
92 return Success(msg.identifier, InMessageKind::COMMAND);
93 }

◆ from() [3/3]

static Success irsol::protocol::Success::from ( const Inquiry msg,
irsol::types::protocol_value_t  result 
)
inlinestatic

Creates a success message from an Inquiry.

Parameters
msgThe original inquiry message.
resultThe result of the inquiry.
Returns
A success response with the requested value.

Definition at line 101 of file success.hpp.

102 {
103 return Success(msg.identifier, InMessageKind::INQUIRY, std::make_optional(result));
104 }
auto result

◆ hasBody()

bool irsol::protocol::Success::hasBody ( ) const
Returns
true if the success message contains a result body.

Definition at line 38 of file success.cpp.

39{
40 return body.has_value();
41}

◆ hasDouble()

bool irsol::protocol::Success::hasDouble ( ) const
Returns
true if the result body is of type double.

Definition at line 48 of file success.cpp.

49{
50 return hasBody() && std::holds_alternative<double>(*body);
51}

◆ hasInt()

bool irsol::protocol::Success::hasInt ( ) const
Returns
true if the result body is of type int.

Definition at line 43 of file success.cpp.

44{
45 return hasBody() && std::holds_alternative<int>(*body);
46}

◆ hasString()

bool irsol::protocol::Success::hasString ( ) const
Returns
true if the result body is of type string.

Definition at line 53 of file success.cpp.

54{
55 return hasBody() && std::holds_alternative<std::string>(*body);
56}

◆ toString()

std::string irsol::protocol::Success::toString ( ) const

Converts the success message to a human-readable string.

Returns
A string representation of the success.

Definition at line 18 of file success.cpp.

19{
20 std::ostringstream oss;
21 oss << "Success{"
22 << "identifier: '" << identifier << "', source: " << InMessageKindToString(source);
23 if(hasBody()) {
24 oss << ", body: ";
25 if(hasInt()) {
26 oss << "<int> " << std::get<int>(*body);
27 } else if(hasDouble()) {
28 oss << "<double> " << std::get<double>(*body);
29 } else if(hasString()) {
30 oss << "<string> \"" << std::get<std::string>(*body) << "\"";
31 }
32 }
33 oss << "}";
34 return oss.str();
35}
constexpr const char * InMessageKindToString(InMessageKind kind)
Converts an InMessageKind enum to a human-readable string.
Definition variants.hpp:59
bool hasDouble() const
Definition success.cpp:48
bool hasString() const
Definition success.cpp:53

Member Data Documentation

◆ body

std::optional<irsol::types::protocol_value_t> irsol::protocol::Success::body {}

Optional result or data associated with the response.

Definition at line 47 of file success.hpp.

47{};

◆ identifier

std::string irsol::protocol::Success::identifier

The identifier associated with the success response.

Definition at line 41 of file success.hpp.

◆ source

InMessageKind irsol::protocol::Success::source

The kind of the incoming message that triggered this response.

Definition at line 44 of file success.hpp.


The documentation for this struct was generated from the following files: