IRSOL
C++ code implementing socket server for interacting with Baumer camera.
serializer.hpp
Go to the documentation of this file.
1
16#pragma once
17
18#include "irsol/macros.hpp"
21#include "irsol/traits.hpp"
22
23#include <string>
24#include <vector>
25
26namespace irsol {
27namespace protocol {
28
46{
47public:
55 {
57 static constexpr irsol::types::byte_t SOH{0x01};
59 static constexpr irsol::types::byte_t STX{0x02};
61 static constexpr irsol::types::byte_t ETX{0x03};
62 };
63
65 static constexpr const char* message_termination = "\n";
66
77
92 template<
93 typename T,
94 std::enable_if_t<irsol::traits::is_type_in_variant_v<T, OutMessage>, int> = 0>
96 {
97 if constexpr(std::is_same_v<T, Success>) {
98 return serializeSuccess(std::move(msg));
99 } else if constexpr(std::is_same_v<T, BinaryDataBuffer>) {
100 return serializeBinaryDataBuffer(std::move(msg));
101 } else if constexpr(std::is_same_v<T, ImageBinaryData>) {
102 return serializeImageBinaryData(std::move(msg));
103 } else if constexpr(std::is_same_v<T, ColorImageBinaryData>) {
104 return serializeColorImageBinaryData(std::move(msg));
105 } else if constexpr(std::is_same_v<T, Error>) {
106 return serializeError(std::move(msg));
107 } else
108 IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, "Serializer::serialize()");
109 }
110
121
131 template<
132 typename T,
133 std::enable_if_t<
135 int> = 0>
136 static std::string serializeValue(T&& value)
137 {
138 if constexpr(std::is_same_v<T, int>) {
139 return std::to_string(value);
140 } else if constexpr(std::is_same_v<T, double>) {
141 return std::to_string(value);
142 } else if constexpr(std::is_same_v<T, std::string>) {
143 return "{" + value + "}";
144 } else
145 IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, "Serializer::serializeValue()");
146 }
147
155
156private:
157 Serializer() = delete;
158
159 // Specialized serialization methods for each OutMessage type:
160
171
183
202
214
225};
226
227} // namespace protocol
228} // namespace irsol
Serializes outgoing protocol messages and primitive values.
static internal::SerializedMessage serialize(T &&msg)
Serialize a specific irsol::protocol::OutMessage type into a serialized protocol message.
static std::string serializeValue(T &&value)
Serialize a specific primitive type to a string.
static std::string serializeBinaryDataAttribute(irsol::protocol::BinaryDataAttribute &&att)
Serialize a irsol::protocol::BinaryDataAttribute to a string.
static internal::SerializedMessage serializeBinaryDataBuffer(BinaryDataBuffer &&msg)
Serializes a irsol::protocol::BinaryDataBuffer message to a binary protocol message.
static internal::SerializedMessage serializeImageBinaryData(ImageBinaryData &&msg)
Serializes a irsol::protocol::ImageBinaryData message to a binary protocol message.
static constexpr const char * message_termination
Message line termination sequence (newline character) used to "close" a serialized message.
static std::string serializeValue(irsol::types::protocol_value_t &&value)
Serialize a protocol primitive value to a string.
static internal::SerializedMessage serializeColorImageBinaryData(ColorImageBinaryData &&msg)
Serializes a irsol::protocol::ColorImageBinaryData message to a binary protocol message.
static internal::SerializedMessage serialize(OutMessage &&msg)
Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.
Serializer()=delete
This class cannot be instantiated.
static internal::SerializedMessage serializeError(Error &&msg)
Serializes a irsol::protocol::Error message to a binary protocol message.
static internal::SerializedMessage serializeSuccess(Success &&msg)
Serializes a irsol::protocol::Success message to a binary protocol message.
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
Common portability and diagnostic macros for the irsol library.
#define IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, funcNameLiteral)
Emits a compile-time error when no template specialization is available.
Definition macros.hpp:173
std::variant< int, double, std::string > protocol_value_t
Variant type representing protocol values that can be one of several types.
Definition types.hpp:150
std::byte byte_t
Alias for a single byte used in serialization or binary data handling.
Definition types.hpp:165
Defines the SerializedMessage structure representing serialized protocol messages.
Represents a single binary data attribute within the protocol.
Definition binary.hpp:35
Represents an error response message from the server.
Definition error.hpp:36
Special byte constants used in the Simple Communication Protocol.
static constexpr irsol::types::byte_t SOH
Start of Header (SOH) byte: 0x01.
static constexpr irsol::types::byte_t STX
Start of Text (STX) byte: 0x02.
static constexpr irsol::types::byte_t ETX
End of Text (ETX) byte: 0x03.
Represents a success response message from the server.
Definition success.hpp:39
Represents a binary data object within the protocol.
Definition binary.hpp:107
Represents a serialized protocol message with header and payload.
Checks whether a type T is one of the types in a std::variant.
Definition traits.hpp:76
auto value
auto msg
Template metaprogramming traits for type introspection in the irsol library.