From a2d384afe6f6c279a57542a22b9a1f1f973b8416 Mon Sep 17 00:00:00 2001 From: "Ithier Jeff (CC-AD/EYF1)" Date: Wed, 5 Aug 2020 11:57:23 +0200 Subject: [PATCH] iox-#218 Add error type for recalling chunks and an allocate method which can be passed a lambda. Signed-off-by: Ithier Jeff (CC-AD/EYF1) --- .../experimental/internal/popo/publisher.inl | 13 ++++++++++++- .../experimental/popo/publisher.hpp | 18 ++++++++++++++++-- .../test_popo_publisher_experimental.cpp | 15 +++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/experimental/internal/popo/publisher.inl b/iceoryx_posh/include/iceoryx_posh/experimental/internal/popo/publisher.inl index d2ee19e33e..2cb5040f9d 100644 --- a/iceoryx_posh/include/iceoryx_posh/experimental/internal/popo/publisher.inl +++ b/iceoryx_posh/include/iceoryx_posh/experimental/internal/popo/publisher.inl @@ -46,6 +46,17 @@ Publisher::allocate() const noexcept return iox::cxx::success(reinterpret_cast(buf)); } +template +cxx::expected +Publisher::allocate(cxx::function_ref f) const noexcept +{ + std::cout << "allocate(cxx::function_ref)" << std::endl; + uint8_t* buf = new uint8_t[sizeof (T)]; + auto chunk = reinterpret_cast(buf); + f(chunk); + return cxx::success<>(); +}; + template inline void Publisher::release(chunk_t&& chunk) const noexcept @@ -68,7 +79,7 @@ Publisher::publishCopyOf(const T& val) const noexcept } template -inline cxx::expected> +inline cxx::expected Publisher::previous() const noexcept { std::cout << "previous()" << std::endl; diff --git a/iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp b/iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp index 0cf66d93b4..12ebc8b8ad 100644 --- a/iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp +++ b/iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp @@ -18,6 +18,7 @@ #include "iceoryx_posh/internal/popo/sender_port.hpp" #include "iceoryx_posh/internal/popo/building_blocks/chunk_sender.hpp" #include "iceoryx_utils/cxx/expected.hpp" +#include "iceoryx_utils/cxx/function_ref.hpp" #include @@ -26,6 +27,12 @@ namespace iox namespace popo { +enum class ChunkRecallError : uint8_t +{ + NO_PREVIOUS_CHUNK, + CHUNK_ALREADY_CLAIMED +}; + struct Untyped{}; template @@ -57,11 +64,18 @@ class Publisher // We can't just provide a T* because this memory is undefined and could lead to misuse. We can't initialize the // memory ourselves because then we have no zero copy (or a wasted initliazation? double check...). /// - /// @brief allocate Allocates a chunk of shared memory.Q + /// @brief allocate Allocates a chunk of shared memory. /// @return Pointer to the successfully allocated memory, otherwise an allocation error. /// cxx::expected allocate() const noexcept; + /// + /// @brief allocate Allocate a chunk then execute the provided callable using the allocated chunk. + /// @param f Callable to execute, taking the allocated chunk as its parameter. + /// @return + /// + cxx::expected allocate(cxx::function_ref f) const noexcept; + /// /// @brief release Releases ownership of an unused allocated chunk. /// @details The released chunk will be freed as soon as there are no longer any active references @@ -92,7 +106,7 @@ class Publisher /// @brief previous Reclaims ownership of a previously published chunk if it has not yet been accessed. /// @return The previously published chunk if one exists and is unclaimed, otherwise an error. /// - cxx::expected previous() const noexcept; + cxx::expected previous() const noexcept; void offer() noexcept; void stopOffer() noexcept; diff --git a/iceoryx_posh/test/moduletests/test_popo_publisher_experimental.cpp b/iceoryx_posh/test/moduletests/test_popo_publisher_experimental.cpp index 17362b7522..70acdf2957 100644 --- a/iceoryx_posh/test/moduletests/test_popo_publisher_experimental.cpp +++ b/iceoryx_posh/test/moduletests/test_popo_publisher_experimental.cpp @@ -85,3 +85,18 @@ TEST_F(ExperimentalPublisherTest, BasicCopiedPublish) publisher.publishCopyOf(position); } + +TEST_F(ExperimentalPublisherTest, BasicAllocateThenPublish) +{ + struct Position { + double_t x = 0.0; + double_t y = 0.0; + double_t z = 0.0; + } position; + + iox::popo::Publisher publisher{}; + publisher.allocate([&](Position* chunk){ + std::cout << "Lambda called with passed chunk." << std::endl; + }); + +}