IRSOL
C++ code implementing socket server for interacting with Baumer camera.
binary.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "irsol/assert.hpp"
14#include "irsol/logging.hpp"
15#include "irsol/macros.hpp"
16#include "irsol/types.hpp"
17
18#include <array>
19#include <numeric>
20#include <sstream>
21#include <string_view>
22#include <vector>
23
24namespace irsol {
25namespace protocol {
26
35{
42
44 std::string identifier;
45
48
53 std::string toString() const;
54
56 bool hasInt() const;
57
59 bool hasDouble() const;
60
62 bool hasString() const;
63};
64
65namespace internal {
66
73template<std::uint8_t N>
75{
76 static constexpr std::string_view name()
77 {
78 if constexpr(N == 1) {
79 return "BinaryDataBuffer";
80 } else if constexpr(N == 2) {
81 return "BinaryDataBuffer2D";
82 } else if constexpr(N == 3) {
83 return "BinaryDataBuffer3D";
84 } else {
85 IRSOL_STATIC_UNREACHABLE("Unsupported dimensionality for BinaryDataBuffer");
86 }
87 }
88};
89
105template<std::uint8_t NBytes, std::uint8_t N>
107{
108 IRSOL_STATIC_ASSERT((NBytes == 1 || NBytes == 2), "Binary data element byte size must be 1 or 2");
109 IRSOL_STATIC_ASSERT(N >= 1, "Binary data dimensionality must be at least 1");
110
112 static constexpr uint8_t BYTES_PER_ELEMENT = NBytes;
113
115 static constexpr uint8_t DIM = N;
116
126 std::vector<irsol::types::byte_t>&& data,
127 const std::array<uint64_t, N>& shape,
128 std::vector<BinaryDataAttribute>&& attributes = {})
129 : data(std::move(data))
130 , shape(shape)
131 , numElements(std::accumulate(shape.begin(), shape.end(), 1ull, std::multiplies<>{}))
133 , attributes(std::move(attributes))
134 {
135 IRSOL_LOG_TRACE("BinaryData constructed: {}", toString());
137 this->data.size() == this->numBytes,
138 "Data size (%lu) does not match the number of elements (%lu) multiplied by bytes per element "
139 "(%d).",
140 this->data.size(),
141 this->numElements,
142 this->BYTES_PER_ELEMENT);
143 }
144
149 BinaryData(BinaryData&& other) = default;
150
151 // Disable copy semantics.
152 BinaryData(const BinaryData&) = delete;
153 BinaryData& operator=(const BinaryData&) = delete;
154
155 // Disable move assignment to avoid accidental reassignment.
156 BinaryData& operator=(BinaryData&& other) noexcept = delete;
157
159 std::vector<irsol::types::byte_t> data;
160
162 std::array<uint64_t, DIM> shape;
163
165 uint64_t numElements;
166
168 uint64_t numBytes;
169
171 std::vector<BinaryDataAttribute> attributes;
172
177 std::string toString() const
178 {
179 std::stringstream ss;
180 ss << BinaryDataBufferName<N>::name() << "u" << (BYTES_PER_ELEMENT == 1 ? "8" : "16")
181 << "[shape=(" << std::to_string(shape[0]);
182 for(uint8_t i = 1; i < DIM; ++i) {
183 ss << "x" << std::to_string(shape[i]);
184 }
185 ss << ")](" << std::to_string(numBytes) << " bytes)";
186 return ss.str();
187 }
188};
189
190} // namespace internal
191
197
203
209
210} // namespace protocol
211} // namespace irsol
Assertion macros and utilities based on the PPK_ASSERT library.
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
Logging utilities and configuration for the irsol library.
Common portability and diagnostic macros for the irsol library.
#define IRSOL_STATIC_UNREACHABLE(messageLiteral)
Marks unreachable branches in constexpr functions.
Definition macros.hpp:130
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
Represents a single binary data attribute within the protocol.
Definition binary.hpp:35
std::string identifier
Identifier of the binary data attribute.
Definition binary.hpp:44
std::string toString() const
Returns a string representation of the binary data attribute.
Definition binary.cpp:17
irsol::types::protocol_value_t value
Value associated with the attribute.
Definition binary.hpp:47
Helper to get a descriptive name for binary data buffers by dimensionality.
Definition binary.hpp:75
static constexpr std::string_view name()
Definition binary.hpp:76
Represents a binary data object within the protocol.
Definition binary.hpp:107
std::vector< BinaryDataAttribute > attributes
Additional attributes related to the binary data.
Definition binary.hpp:171
std::vector< irsol::types::byte_t > data
Owned binary data bytes.
Definition binary.hpp:159
BinaryData & operator=(const BinaryData &)=delete
std::array< uint64_t, DIM > shape
Shape of the binary data.
Definition binary.hpp:162
static constexpr uint8_t BYTES_PER_ELEMENT
Number of bytes per element.
Definition binary.hpp:112
std::string toString() const
Returns a string summary of the binary data buffer.
Definition binary.hpp:177
BinaryData(std::vector< irsol::types::byte_t > &&data, const std::array< uint64_t, N > &shape, std::vector< BinaryDataAttribute > &&attributes={})
Constructs a BinaryData object.
Definition binary.hpp:125
uint64_t numElements
Total number of elements in the data.
Definition binary.hpp:165
BinaryData & operator=(BinaryData &&other) noexcept=delete
BinaryData(BinaryData &&other)=default
Move constructs a BinaryData object.
IRSOL_STATIC_ASSERT(N >=1, "Binary data dimensionality must be at least 1")
static constexpr uint8_t DIM
Dimensionality of the binary data.
Definition binary.hpp:115
BinaryData(const BinaryData &)=delete
IRSOL_STATIC_ASSERT((NBytes==1||NBytes==2), "Binary data element byte size must be 1 or 2")
uint64_t numBytes
Total number of bytes (numElements * bytes per element).
Definition binary.hpp:168
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...