Skip to content

Commit 7465a58

Browse files
committed
more usage of PayloadFragment
1 parent a788dea commit 7465a58

File tree

9 files changed

+29
-28
lines changed

9 files changed

+29
-28
lines changed

docs/examples/1_presentation/example_1_presentation_0_hello_raw_pubsub_udp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ TEST_F(Example_1_Presentation_0_HelloRawPubSub_Udp, main)
151151
const TimePoint msg_deadline = arg.approx_now + 1s;
152152
constexpr cetl::span<const char> message{"Hello, World!"};
153153
//
154-
const std::array<const cetl::span<const cetl::byte>, 1> payload_fragments{
155-
cetl::span<const cetl::byte>{reinterpret_cast<const cetl::byte*>(message.data()), // NOLINT
154+
const std::array<const PayloadFragment, 1> payload_fragments{
155+
PayloadFragment{reinterpret_cast<const cetl::byte*>(message.data()), // NOLINT
156156
message.size()}};
157157
EXPECT_THAT(raw_publisher.publish(msg_deadline, payload_fragments), testing::Eq(cetl::nullopt));
158158
});

docs/examples/platform/node_helpers.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ struct NodeHelpers
6969
TxSession& tx_session,
7070
const TxMetadata& metadata)
7171
{
72-
using traits = typename T::_traits_;
72+
using traits = typename T::_traits_;
73+
using PayloadFragment = libcyphal::transport::PayloadFragment;
74+
7375
std::array<std::uint8_t, traits::SerializationBufferSizeBytes> buffer{};
7476

7577
const auto data_size = serialize(value, buffer).value();
7678

7779
// NOLINTNEXTLINE
78-
const cetl::span<const cetl::byte> fragment{reinterpret_cast<cetl::byte*>(buffer.data()), data_size};
79-
const std::array<const cetl::span<const cetl::byte>, 1> payload{fragment};
80+
const PayloadFragment fragment{reinterpret_cast<cetl::byte*>(buffer.data()), data_size};
81+
const std::array<const PayloadFragment, 1> payload{fragment};
8082

8183
return tx_session.send(metadata, payload);
8284
}

include/libcyphal/presentation/common_helpers.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ static auto tryPerformOnSerialized(const Message& message,
100100
return result_size.error();
101101
}
102102

103-
const cetl::span<const cetl::byte> data_span{buffer.data(), result_size.value()};
104-
const std::array<const cetl::span<const cetl::byte>, 1> fragments{data_span};
103+
const transport::PayloadFragment data_span{buffer.data(), result_size.value()};
104+
const std::array<const transport::PayloadFragment, 1> fragments{data_span};
105105

106106
return std::forward<Action>(action)(fragments);
107107
}
@@ -135,8 +135,8 @@ static auto tryPerformOnSerialized(const Message& message,
135135
return result_size.error();
136136
}
137137

138-
const cetl::span<const cetl::byte> data_span{buffer.get(), result_size.value()};
139-
const std::array<const cetl::span<const cetl::byte>, 1> fragments{data_span};
138+
const transport::PayloadFragment data_span{buffer.get(), result_size.value()};
139+
const std::array<const transport::PayloadFragment, 1> fragments{data_span};
140140

141141
return std::forward<Action>(action)(fragments);
142142
}

include/libcyphal/transport/can/can_transport_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ class TransportImpl final : private TransportDelegate, public ICanTransport
512512
return tryHandleTransientFailure<Report>(std::move(*failure), media.index(), canardInstance());
513513
}
514514

515-
CETL_NODISCARD static bool makeMediaArray(MediaArray& media_array,
516-
const std::size_t media_count,
517-
const cetl::span<IMedia*> media_interfaces,
518-
const std::size_t tx_capacity)
515+
CETL_NODISCARD static bool makeMediaArray(MediaArray& media_array,
516+
const std::size_t media_count,
517+
const cetl::span<IMedia*> media_interfaces,
518+
const std::size_t tx_capacity)
519519
{
520520
#if defined(__cpp_exceptions)
521521
try

include/libcyphal/transport/contiguous_payload.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ class ContiguousPayload final
4545
, payload_size_{0}
4646
, allocated_buffer_{nullptr}
4747
{
48-
using Fragment = cetl::span<const cetl::byte>;
49-
5048
// Count fragments skipping empty ones. Also keep tracking of the total payload size
5149
// and pointer to the last non-empty fragment (which will be in use for the optimization).
5250
//
5351
const auto total_non_empty_fragments =
54-
std::count_if(payload_fragments.begin(), payload_fragments.end(), [this](const Fragment frag) {
52+
std::count_if(payload_fragments.begin(), payload_fragments.end(), [this](const PayloadFragment frag) {
5553
if (frag.empty())
5654
{
5755
return false;
@@ -68,9 +66,9 @@ class ContiguousPayload final
6866
if (cetl::byte* const buffer = allocated_buffer_)
6967
{
7068
std::size_t offset = 0;
71-
for (const Fragment frag : payload_fragments)
69+
for (const PayloadFragment frag : payload_fragments)
7270
{
73-
// Next nolint is unavoidable: we need offset from the beginning of the buffer.
71+
// Next nolint is unavoidable: we need to offset from the beginning of the buffer.
7472
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
7573
(void) std::memmove(&buffer[offset], frag.data(), frag.size());
7674
offset += frag.size();

include/libcyphal/transport/media_payload.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef LIBCYPHAL_TRANSPORT_MEDIA_PAYLOAD_HPP_INCLUDED
77
#define LIBCYPHAL_TRANSPORT_MEDIA_PAYLOAD_HPP_INCLUDED
88

9+
#include "types.hpp"
10+
911
#include <cetl/pf17/cetlpf.hpp>
1012
#include <cetl/pf20/cetlpf.hpp>
1113

@@ -125,7 +127,7 @@ class MediaPayload final
125127
///
126128
/// Returns an empty (`{nullptr, 0}`) span if the payload is moved, released or reset.
127129
///
128-
cetl::span<const cetl::byte> getSpan() const noexcept
130+
PayloadFragment getSpan() const noexcept
129131
{
130132
return {ownership_.data, ownership_.size};
131133
}

include/libcyphal/transport/udp/delegate.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ class UdpardMemory final : public ScatteredBuffer::IStorage
138138
cetl::byte* const destination,
139139
const std::size_t length_bytes) const override
140140
{
141-
using FragSpan = const cetl::span<const cetl::byte>;
142-
143141
// TODO: Use `udpardGather` function when it will be available with offset support.
144142

145143
CETL_DEBUG_ASSERT((destination != nullptr) || (length_bytes == 0),
@@ -172,8 +170,9 @@ class UdpardMemory final : public ScatteredBuffer::IStorage
172170
// Next nolint-s are unavoidable: we need offset from the beginning of the buffer.
173171
// No Sonar `cpp:S5356` b/c we integrate here with libcanard raw C buffers.
174172
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
175-
FragSpan frag_span{static_cast<const cetl::byte*>(frag->view.data) + view_offset, // NOSONAR cpp:S5356
176-
std::min(frag->view.size - view_offset, length_bytes - dst_offset)};
173+
const PayloadFragment frag_span{static_cast<const cetl::byte*>(frag->view.data) +
174+
view_offset, // NOSONAR cpp:S5356
175+
std::min(frag->view.size - view_offset, length_bytes - dst_offset)};
177176
CETL_DEBUG_ASSERT(frag_span.size() <= (frag->view.size - view_offset), "");
178177

179178
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)

include/libcyphal/transport/udp/udp_transport_impl.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,6 @@ class TransportImpl final : private TransportDelegate, public IUdpTransport
713713
///
714714
void sendNextFrameToMediaTxSocket(Media& media, ITxSocket& tx_socket)
715715
{
716-
using PayloadFragment = cetl::span<const cetl::byte>;
717-
718716
TimePoint tx_deadline;
719717
while (UdpardTxItem* const tx_item = peekFirstValidTxItem(media.udpard_tx(), tx_deadline))
720718
{

test/unittest/verification_utilities.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef LIBCYPHAL_VERIFICATION_UTILITIES_HPP_INCLUDED
77
#define LIBCYPHAL_VERIFICATION_UTILITIES_HPP_INCLUDED
88

9+
#include <libcyphal/transport/types.hpp>
10+
911
#include <cetl/pf17/cetlpf.hpp>
1012
#include <cetl/pf20/cetlpf.hpp>
1113

@@ -45,20 +47,20 @@ std::array<cetl::byte, N> makeIotaArray(const cetl::byte init)
4547
}
4648

4749
template <std::size_t N>
48-
std::array<cetl::span<const cetl::byte>, 1> makeSpansFrom(const std::array<cetl::byte, N>& payload)
50+
std::array<transport::PayloadFragment, 1> makeSpansFrom(const std::array<cetl::byte, N>& payload)
4951
{
5052
return {payload};
5153
}
5254

5355
template <std::size_t N1, std::size_t N2>
54-
std::array<cetl::span<const cetl::byte>, 2> makeSpansFrom(const std::array<cetl::byte, N1>& payload1,
56+
std::array<transport::PayloadFragment, 2> makeSpansFrom(const std::array<cetl::byte, N1>& payload1,
5557
const std::array<cetl::byte, N2>& payload2)
5658
{
5759
return {payload1, payload2};
5860
}
5961

6062
template <typename T>
61-
static bool tryDeserialize(T& obj, const cetl::span<const cetl::span<const cetl::byte>> fragments)
63+
static bool tryDeserialize(T& obj, const transport::PayloadFragments fragments)
6264
{
6365
std::vector<cetl::byte> bytes;
6466
for (const auto& fragment : fragments)

0 commit comments

Comments
 (0)