-
Notifications
You must be signed in to change notification settings - Fork 497
ScatteredBuffer::IFragmentsObserver #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
2443c94
a788dea
7465a58
497fd2c
76c9a31
6f2f718
c71630c
ea52c13
f3e6f99
862b256
8a4b37c
c0ad95e
9a12b61
33244a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,14 @@ class CanardMemory final : public ScatteredBuffer::IStorage | |
| return bytes_to_copy; | ||
| } | ||
|
|
||
| void observeFragments(ScatteredBuffer::IFragmentsObserver& observer) const override | ||
|
||
| { | ||
| if ((buffer_ != nullptr) && (payload_size_ > 0)) | ||
| { | ||
| observer.onNext({buffer_, payload_size_}); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| // MARK: Data members: | ||
|
|
||
|
|
@@ -471,13 +479,23 @@ class TransportDelegate | |
| // Now we know that we have at least one active port, | ||
| // so we need preallocate temp memory for the total number of active ports. | ||
| // | ||
| filters.reserve(total_active_ports); | ||
| if (filters.capacity() < total_active_ports) | ||
| #if defined(__cpp_exceptions) | ||
| try | ||
| { | ||
| #endif | ||
| filters.reserve(total_active_ports); | ||
| if (filters.capacity() < total_active_ports) | ||
| { | ||
| // This is out of memory situation. | ||
| return false; | ||
| } | ||
|
|
||
| #if defined(__cpp_exceptions) | ||
| } catch (const std::bad_alloc&) | ||
| { | ||
| // This is out of memory situation. | ||
| return false; | ||
| } | ||
|
|
||
| #endif | ||
| // `ports_count` counting is just for the sake of debug verification. | ||
| std::size_t ports_count = 0; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #define LIBCYPHAL_TRANSPORT_SCATTERED_BUFFER_HPP_INCLUDED | ||
|
|
||
| #include "libcyphal/config.hpp" | ||
| #include "types.hpp" | ||
|
|
||
| #include <cetl/pf17/cetlpf.hpp> | ||
| #include <cetl/rtti.hpp> | ||
|
|
@@ -33,6 +34,26 @@ class ScatteredBuffer final | |
| /// | ||
| static constexpr std::size_t StorageVariantFootprint = config::Transport::ScatteredBuffer_StorageVariantFootprint(); | ||
|
|
||
| /// @brief Defines interface for observing internal fragments of the scattered buffer. | ||
| /// | ||
| class IFragmentsObserver | ||
| { | ||
| public: | ||
| IFragmentsObserver(const IFragmentsObserver&) = delete; | ||
| IFragmentsObserver& operator=(const IFragmentsObserver&) = delete; | ||
| IFragmentsObserver& operator=(IFragmentsObserver&&) noexcept = delete; | ||
| IFragmentsObserver(IFragmentsObserver&&) noexcept = delete; | ||
|
|
||
| /// @brief Notifies the observer about the next fragment of the scattered buffer. | ||
| /// | ||
| virtual void onNext(const PayloadFragment fragment) = 0; | ||
|
|
||
| protected: | ||
| IFragmentsObserver() = default; | ||
| ~IFragmentsObserver() = default; | ||
|
|
||
| }; // IFragmentsObserver | ||
|
|
||
| /// @brief Defines storage interface for the scattered buffer. | ||
| /// | ||
| /// @see ScatteredBuffer::ScatteredBuffer(AnyStorage&& any_storage) | ||
|
|
@@ -72,6 +93,12 @@ class ScatteredBuffer final | |
| cetl::byte* const destination, | ||
| const std::size_t length_bytes) const = 0; | ||
|
|
||
| /// @brief Reports the internal fragments of the storage to the specified observer. | ||
| /// | ||
| /// @param observer The observer will be called (by `onNext` method) for each fragment of the storage. | ||
| /// | ||
| virtual void observeFragments(IFragmentsObserver& observer) const = 0; | ||
|
||
|
|
||
| // MARK: RTTI | ||
|
|
||
| static constexpr cetl::type_id _get_type_id_() noexcept | ||
|
|
@@ -201,6 +228,18 @@ class ScatteredBuffer final | |
| return storage_->copy(offset_bytes, static_cast<cetl::byte*>(destination), length_bytes); | ||
| } | ||
|
|
||
| /// @brief Reports the internal fragments of the buffer to the specified observer. | ||
| /// | ||
| /// @param observer The observer will be called (by `onNext` method) for each fragment of the buffer. | ||
| /// | ||
| void observeFragments(IFragmentsObserver& observer) const | ||
| { | ||
| if (const auto* const storage = storage_) | ||
| { | ||
| storage->observeFragments(observer); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| cetl::unbounded_variant<StorageVariantFootprint, false, true> storage_variant_; | ||
| const IStorage* storage_; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
┬─┬ノ(º_ºノ)