16 #include <rapidsmpf/buffer/buffer.hpp>
17 #include <rapidsmpf/buffer/content_description.hpp>
18 #include <rapidsmpf/buffer/resource.hpp>
19 #include <rapidsmpf/error.hpp>
78 std::unique_ptr<T> payload,
86 payload !=
nullptr,
"nullptr not allowed", std::invalid_argument
92 payload_ = std::shared_ptr<T>(std::move(payload));
100 #if defined(__GNUC__) && __GNUC__ == 14
101 #pragma GCC diagnostic push
102 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
109 #if defined(__GNUC__) && __GNUC__ == 14
110 #pragma GCC diagnostic pop
119 return payload_.reset();
127 [[nodiscard]]
bool empty() const noexcept {
128 return !payload_.has_value();
137 return sequence_number_;
146 template <
typename T>
147 [[nodiscard]]
bool holds() const noexcept {
148 return payload_.type() ==
typeid(std::shared_ptr<T>);
160 template <
typename T>
161 [[nodiscard]] T
const&
get()
const {
162 return *get_ptr<T>();
172 template <
typename T>
174 std::shared_ptr<T> ret = get_ptr<T>();
176 return std::move(*ret);
186 return content_description_;
212 [[nodiscard]] constexpr
size_t copy_cost() const noexcept {
233 copy_cb(),
"message doesn't support `copy`", std::invalid_argument
235 return copy_cb()(*
this, reservation);
246 template <
typename T>
247 [[nodiscard]] std::shared_ptr<T> get_ptr()
const {
248 RAPIDSMPF_EXPECTS(!
empty(),
"message is empty", std::invalid_argument);
249 RAPIDSMPF_EXPECTS(holds<T>(),
"wrong message type", std::invalid_argument);
250 return std::any_cast<std::shared_ptr<T>>(payload_);
254 std::uint64_t sequence_number_{0};
256 ContentDescription content_description_;
Description of an object's content.
constexpr std::size_t & content_size(MemoryType mem_type) noexcept
Access (read/write) the size for a specific memory type.
Represents a reservation for future memory allocation.
Type-erased message wrapper around a payload.
constexpr ContentDescription const & content_description() const noexcept
Returns the content description associated with the message.
void reset() noexcept
Reset the message to empty.
Message(Message &&other) noexcept=default
Move construct.
Message & operator=(Message &&other) noexcept=default
Move assign.
std::function< Message(Message const &, MemoryReservation &reservation)> CopyCallback
Callback for performing a deep copy of a message.
Message(std::uint64_t sequence_number, std::unique_ptr< T > payload, ContentDescription content_description, CopyCallback copy_cb=nullptr)
Construct a new message from a unique pointer to its payload.
T const & get() const
Reference to the payload.
Message()=default
Create an empty message.
bool holds() const noexcept
Compare the payload type.
constexpr std::uint64_t sequence_number() const noexcept
Returns the sequence number of this message.
T release()
Extracts the payload and resets the message.
bool empty() const noexcept
Returns true when no payload is stored.
Message copy(MemoryReservation &reservation) const
Perform a deep copy of this message and its payload.
constexpr size_t copy_cost() const noexcept
Returns the total memory size required for a deep copy of the payload.
constexpr CopyCallback const & copy_cb() const noexcept
Returns the copy callback associated with the message.