iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
response.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_RESPONSE_HPP
14#define IOX2_RESPONSE_HPP
15
17#include "iox2/bb/slice.hpp"
19#include "iox2/payload_info.hpp"
20#include "iox2/service_type.hpp"
21
22#include <type_traits>
23
24namespace iox2 {
27template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
28class Response {
29 using ValueType = typename PayloadInfo<ResponsePayload>::ValueType;
30
31 public:
32 Response(Response&& rhs) noexcept;
33 auto operator=(Response&& rhs) noexcept -> Response&;
34 ~Response() noexcept;
35
36 Response(const Response&) noexcept = delete;
37 auto operator=(const Response&) noexcept -> Response& = delete;
38
40 auto header() const -> ResponseHeader;
41
43 template <typename T = ResponseUserHeader,
44 typename = std::enable_if_t<!std::is_same<void, ResponseUserHeader>::value, T>>
45 auto user_header() const -> const T&;
46
48 template <typename T = ResponsePayload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
49 auto payload() const -> const T&;
50
52 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
53 auto payload() const -> bb::ImmutableSlice<ValueType>;
54
57 auto origin() const -> UniqueServerId;
58
59 private:
60 template <ServiceType, typename, typename, typename, typename>
61 friend class PendingResponse;
62
63 explicit Response(iox2_response_h handle) noexcept;
64
65 void drop();
66
67 iox2_response_h m_handle = nullptr;
68};
69
70template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
71inline Response<Service, ResponsePayload, ResponseUserHeader>::Response(Response&& rhs) noexcept {
72 *this = std::move(rhs);
73}
74
75template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
77 if (this != &rhs) {
78 drop();
79 m_handle = rhs.m_handle;
80 rhs.m_handle = nullptr;
81 }
82
83 return *this;
84}
85
86template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
90
91template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
93 iox2_response_header_h header_handle = nullptr;
94 iox2_response_header(&m_handle, nullptr, &header_handle);
95 return ResponseHeader { header_handle };
96}
97
98template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
99template <typename T, typename>
101 const void* ptr = nullptr;
102 iox2_response_user_header(&m_handle, &ptr);
103 return *static_cast<const T*>(ptr);
104}
105
106template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
107template <typename T, typename>
109 const void* ptr = nullptr;
110 iox2_response_payload(&m_handle, &ptr, nullptr);
111 return *static_cast<const T*>(ptr);
112}
113
114template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
115template <typename T, typename>
116inline auto Response<Service, ResponsePayload, ResponseUserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
117 const void* ptr = nullptr;
118 size_t number_of_elements = 0;
119 iox2_response_payload(&m_handle, &ptr, &number_of_elements);
120
121 // for the custom payload marker, the slice length is the
122 // runtime payload byte size
123 auto length = number_of_elements;
124 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
125 length = iox2_response_payload_number_of_bytes(&m_handle);
126 }
127
128 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
129}
130
131template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
133 return header().server_port_id();
134}
135
136template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
137inline Response<Service, ResponsePayload, ResponseUserHeader>::Response(iox2_response_h handle) noexcept
138 : m_handle(handle) {
139}
140
141template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
142inline void Response<Service, ResponsePayload, ResponseUserHeader>::drop() {
143 if (m_handle != nullptr) {
144 iox2_response_drop(m_handle);
145 m_handle = nullptr;
146 }
147}
148} // namespace iox2
149
150#endif
Response header used by [MessagingPattern::RequestResponse].
auto payload() const -> const T &
Returns a reference to the payload of the response.
Definition response.hpp:108
auto operator=(Response &&rhs) noexcept -> Response &
Definition response.hpp:76
auto operator=(const Response &) noexcept -> Response &=delete
auto header() const -> ResponseHeader
Returns a reference to the [ResponseHeader].
Definition response.hpp:92
Response(Response &&rhs) noexcept
Definition response.hpp:71
~Response() noexcept
Definition response.hpp:87
auto user_header() const -> const T &
Returns a reference to the user header of the response.
Definition response.hpp:100
auto origin() const -> UniqueServerId
Definition response.hpp:132
Response(const Response &) noexcept=delete
The system-wide unique id of a [Server].