IRSOL
C++ code implementing socket server for interacting with Baumer camera.
parser_result.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include "irsol/macros.hpp"
18#include "irsol/traits.hpp"
19
20#include <sstream>
21#include <string>
22#include <variant>
23
24namespace irsol {
25namespace protocol {
26namespace internal {
60template<typename T, std::enable_if_t<irsol::traits::is_type_in_variant_v<T, InMessage>, int> = 0>
62{
63public:
64 using result_type = T;
65 using error_type = std::string;
66 using message_type = std::variant<result_type, error_type>;
67
72 ParserResult(result_type&& message): _messageOrError(std::move(message)) {}
77 ParserResult(error_type&& error): _messageOrError(std::move(error)) {}
78
79 // Delete copy constructor and move assignment operator and copy assignment operator
80 ParserResult(const ParserResult&) = delete;
81 ParserResult& operator=(ParserResult&&) noexcept = delete;
82 ParserResult& operator=(const ParserResult&) = delete;
87 explicit operator bool() const
88 {
89 return isMessage();
90 }
94 bool isMessage() const
95 {
96 return std::holds_alternative<result_type>(_messageOrError);
97 }
101 bool isError() const
102 {
103 return std::holds_alternative<error_type>(_messageOrError);
104 }
105
111 {
112 return std::get<result_type>(_messageOrError);
113 }
119 {
120 return std::get<error_type>(_messageOrError);
121 }
122
128 std::string toString() const
129 {
130 std::string message_string;
131 std::string message_type;
132 if(isMessage()) {
133 message_string = getMessage().toString();
134 message_type = "Message";
135 } else if(isError()) {
136 message_string = getError();
137 message_type = "Error";
138 } else {
139 IRSOL_ASSERT_FATAL("Both 'isMessage()' and 'isError()' returned false");
140 }
141
142 std::stringstream ss;
143 ss << "ParserResult<" << message_type << ">('" << message_string << "')";
144 return ss.str();
145 }
146
147private:
149};
150} // namespace internal
151} // namespace protocol
152} // namespace irsol
Wrapper for the result of a protocol parsing attempt.
result_type getMessage() const
Returns the contained message.
error_type getError() const
Returns the contained error string.
ParserResult & operator=(ParserResult &&) noexcept=delete
std::variant< result_type, error_type > message_type
ParserResult(result_type &&message)
Constructs a successful parser result.
ParserResult(const ParserResult &)=delete
bool isError() const
Returns true if the result contains an error message.
bool isMessage() const
Returns true if the result contains a parsed message.
std::string toString() const
Returns a human-readable string representation of the result.
ParserResult(error_type &&error)
Constructs a failed parser result with an error string.
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
Common portability and diagnostic macros for the irsol library.
Template metaprogramming traits for type introspection in the irsol library.
Definitions and utilities for incoming and outgoing protocol messages.