Skip to content

Commit

Permalink
iox-eclipse-iceoryx#218 Attempt to add ability to emplace sample via …
Browse files Browse the repository at this point in the history
…function call.

Signed-off-by: Ithier Jeff (CC-AD/EYF1) <[email protected]>
  • Loading branch information
orecham committed Aug 6, 2020
1 parent d4dc6be commit 19e2d7d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 11 additions & 11 deletions iceoryx_examples/icedelivery/iox_publisher_modern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ int main(int argc, char *argv[])

// OR - if you want to loan a sample and construct the data with arbitrary logic

// publisher.loan()
// .and_then([&](iox::popo::Sample<Position>& sample){
// getVehiclePosition(sample.allocation());
// // OR
// sample.emplace(77, 88, 99);
// // OR
// sample.emplace(getVehiclePosition);
publisher.loan()
.and_then([&](iox::popo::Sample<Position>& sample){
getVehiclePosition(sample.allocation());
// OR
sample.emplace(77.77, 88.88, 99.99);
// OR
sample.emplace(getVehiclePosition);

// // sample.publish()
// // OR
// // publisher.publish(std::move(sample));
// });
// sample.publish()
// OR
publisher.publish(std::move(sample));
});

return 0;
}
21 changes: 16 additions & 5 deletions iceoryx_posh/include/iceoryx_posh/experimental/popo/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,38 @@ class Sample
m_isEmpty = true;
return *this;
}
template <typename... Args>
void emplace(Args&&... args) noexcept
template <typename Arg, typename... Args>
auto emplace(Arg arg, Args... args)
-> typename std::enable_if<!std::is_same<std::decay<Arg>, cxx::function_ref<void(T*)>>::value>::type
{
if(m_samplePtr != nullptr)
{
new (m_samplePtr.get()) T(std::forward<Args>(args)...);
new (m_samplePtr.get()) T(std::forward<Arg>(arg), std::forward<Args>(args)...);
m_isEmpty = false;
}
}
void emplace(cxx::function_ref<void(T*)> f)
void emplace(cxx::function_ref<void(T*)> f) noexcept
{
f(m_samplePtr.get());
m_isEmpty = false;
}
// template <typename... Args>
// void emplace(Args&&... args) noexcept
// {
// if(m_samplePtr != nullptr)
// {
// new (m_samplePtr.get()) T(std::forward<Args>(args)...);
// m_isEmpty = false;
// }
// }
void publish() noexcept
{
m_publisher.publish(std::move(*this));
}
private:
const cxx::unique_ptr<T> m_samplePtr = nullptr;
bool m_isEmpty = true;

cxx::unique_ptr<T> m_samplePtr = nullptr;
Publisher<T>& m_publisher;
};

Expand Down

0 comments on commit 19e2d7d

Please sign in to comment.