Skip to content

Commit ccb349b

Browse files
committed
new overloads for build method of UMessageBuilder to avoid copy
1 parent 4375d15 commit ccb349b

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

include/up-cpp/datamodel/builder/UMessage.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ struct UMessageBuilder {
234234
/// @return A built message with no payload populated.
235235
[[nodiscard]] v1::UMessage build() const;
236236

237+
/// @brief Creates a UMessage based on the builder's current state.
238+
///
239+
/// @param A UUri of the method that should be called
240+
///
241+
/// @throws UnexpectedFormat if withPayloadFormat() has been previously
242+
/// called.
243+
///
244+
/// @return A built message with no payload populated.
245+
[[nodiscard]] v1::UMessage build(const v1::UUri&) const;
246+
237247
/// @brief Creates a UMessage with a provided payload based on the
238248
/// builder's current state.
239249
///
@@ -247,6 +257,21 @@ struct UMessageBuilder {
247257
/// @return A built message with the provided payload data embedded.
248258
[[nodiscard]] v1::UMessage build(builder::Payload&&) const;
249259

260+
/// @brief Creates a UMessage with a provided payload based on the
261+
/// builder's current state.
262+
///
263+
/// @param A UUri of the method that should be called
264+
///
265+
/// @param A Payload builder containing a payload to embed in the message.
266+
///
267+
/// @note The contents of the payload builder will be moved.
268+
///
269+
/// @throws UnexpectedFormat if withPayloadFormat() has been previously
270+
/// called and the format in the payload builder does not match.
271+
///
272+
/// @return A built message with the provided payload data embedded.
273+
[[nodiscard]] v1::UMessage build(const v1::UUri&, builder::Payload&&) const;
274+
250275
/// @brief Access the attributes of the message being built.
251276
/// @return A reference to the attributes of the message being built.
252277
[[deprecated(

src/communication/RpcClient.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,13 @@ RpcClient::InvokeHandle RpcClient::invokeMethod(v1::UMessage&& request,
208208
RpcClient::InvokeHandle RpcClient::invokeMethod(
209209
const v1::UUri& method, datamodel::builder::Payload&& payload,
210210
Callback&& callback) {
211-
// makes a local copy of the builder to make this call thread safe
212-
auto local_builder = builder_;
213-
return invokeMethod(
214-
local_builder.withMethod(method).build(std::move(payload)),
215-
std::move(callback));
211+
return invokeMethod(builder_.build(method, std::move(payload)),
212+
std::move(callback));
216213
}
217214

218215
RpcClient::InvokeHandle RpcClient::invokeMethod(const v1::UUri& method,
219216
Callback&& callback) {
220-
// makes a local copy of the builder to make this call thread safe
221-
auto local_builder = builder_;
222-
return invokeMethod(local_builder.withMethod(method).build(),
223-
std::move(callback));
217+
return invokeMethod(builder_.build(method), std::move(callback));
224218
}
225219

226220
RpcClient::InvokeFuture RpcClient::invokeMethod(

src/datamodel/builder/UMessage.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ v1::UMessage UMessageBuilder::build() const {
218218
return message;
219219
}
220220

221+
v1::UMessage UMessageBuilder::build(const v1::UUri& method) const {
222+
v1::UMessage message;
223+
if (expectedPayloadFormat_.has_value()) {
224+
throw UnexpectedFormat(
225+
"Tried to build with no payload when a payload format has been set "
226+
"using withPayloadFormat()");
227+
}
228+
229+
*message.mutable_attributes() = attributes_;
230+
*message.mutable_attributes()->mutable_sink() = method;
231+
*(message.mutable_attributes()->mutable_id()) = uuidBuilder_.build();
232+
233+
return message;
234+
}
235+
221236
v1::UMessage UMessageBuilder::build(builder::Payload&& payload) const {
222237
v1::UMessage message;
223238

@@ -236,6 +251,26 @@ v1::UMessage UMessageBuilder::build(builder::Payload&& payload) const {
236251
return message;
237252
}
238253

254+
v1::UMessage UMessageBuilder::build(const v1::UUri& method,
255+
builder::Payload&& payload) const {
256+
v1::UMessage message;
257+
258+
*message.mutable_attributes() = attributes_;
259+
*message.mutable_attributes()->mutable_sink() = method;
260+
*(message.mutable_attributes()->mutable_id()) = uuidBuilder_.build();
261+
auto [payloadData, payloadFormat] = std::move(payload).buildMove();
262+
if (expectedPayloadFormat_.has_value()) {
263+
if (payloadFormat != expectedPayloadFormat_) {
264+
throw UnexpectedFormat(
265+
"Payload format does not match the expected format");
266+
}
267+
}
268+
*message.mutable_payload() = std::move(payloadData);
269+
message.mutable_attributes()->set_payload_format(payloadFormat);
270+
271+
return message;
272+
}
273+
239274
UMessageBuilder::UMessageBuilder(v1::UMessageType msg_type, v1::UUri&& source,
240275
std::optional<v1::UUri>&& sink,
241276
std::optional<v1::UUID>&& request_id)

0 commit comments

Comments
 (0)