Skip to content

Commit

Permalink
iox-eclipse-iceoryx#218 Add error type for recalling chunks and an al…
Browse files Browse the repository at this point in the history
…locate method which can be passed a lambda.

Signed-off-by: Ithier Jeff (CC-AD/EYF1) <[email protected]>
  • Loading branch information
orecham committed Aug 5, 2020
1 parent f9e449b commit a2d384a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ Publisher<T, sender_port_t>::allocate() const noexcept
return iox::cxx::success<chunk_t>(reinterpret_cast<chunk_t>(buf));
}

template<typename T, typename sender_port_t>
cxx::expected<AllocationError>
Publisher<T, sender_port_t>::allocate(cxx::function_ref<void(chunk_t&)> f) const noexcept
{
std::cout << "allocate(cxx::function_ref<chunk_t&>)" << std::endl;
uint8_t* buf = new uint8_t[sizeof (T)];
auto chunk = reinterpret_cast<chunk_t>(buf);
f(chunk);
return cxx::success<>();
};

template<typename T, typename sender_port_t>
inline void
Publisher<T, sender_port_t>::release(chunk_t&& chunk) const noexcept
Expand All @@ -68,7 +79,7 @@ Publisher<T, sender_port_t>::publishCopyOf(const T& val) const noexcept
}

template<typename T, typename sender_port_t>
inline cxx::expected<chunk_t<T>>
inline cxx::expected<ChunkRecallError>
Publisher<T, sender_port_t>::previous() const noexcept
{
std::cout << "previous()" << std::endl;
Expand Down
18 changes: 16 additions & 2 deletions iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>

Expand All @@ -26,6 +27,12 @@ namespace iox
namespace popo
{

enum class ChunkRecallError : uint8_t
{
NO_PREVIOUS_CHUNK,
CHUNK_ALREADY_CLAIMED
};

struct Untyped{};

template<typename T, typename sender_port_t = iox::popo::SenderPort>
Expand Down Expand Up @@ -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<chunk_t, AllocationError> 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<AllocationError> allocate(cxx::function_ref<void(chunk_t&)> 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
Expand Down Expand Up @@ -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<chunk_t> previous() const noexcept;
cxx::expected<ChunkRecallError> previous() const noexcept;

void offer() noexcept;
void stopOffer() noexcept;
Expand Down
15 changes: 15 additions & 0 deletions iceoryx_posh/test/moduletests/test_popo_publisher_experimental.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Position> publisher{};
publisher.allocate([&](Position* chunk){
std::cout << "Lambda called with passed chunk." << std::endl;
});

}

0 comments on commit a2d384a

Please sign in to comment.