Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
OutgoingResponse.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_OUTGOING_RESPONSE_H
4#define ICE_OUTGOING_RESPONSE_H
5
6#include "Current.h"
7#include "Ice/ReplyStatus.h"
8#include "MarshaledResult.h"
9#include "OutputStream.h"
10
11#include <functional>
12
13namespace Ice
14{
15 /// Represents the response to an incoming request. It's the argument to the `sendResponse` callback accepted by
16 /// Object::dispatch.
17 /// @remark OutgoingResponse is movable but not copyable. `sendResponse` wrappers must move the response to the next
18 /// callback.
19 /// @see Object::dispatch
20 /// @headerfile Ice/Ice.h
21 class ICE_API OutgoingResponse final
22 {
23 public:
24 /// Constructs an OutgoingResponse object.
25 /// @param replyStatus The status of the response.
26 /// @param exceptionId The type ID of the exception, when the response carries an exception other than a user
27 /// exception.
28 /// @param exceptionDetails The full details of the exception, when the response carries an exception other than
29 /// a user exception.
30 /// @param outputStream The output stream that holds the response.
31 /// @param current A reference to the Current object of the request.
34 std::string exceptionId,
35 std::string exceptionDetails,
37 const Current& current) noexcept;
38
39 /// Constructs an OutgoingResponse object with ReplyStatus::Ok.
40 /// @param outputStream The output stream that holds the response.
41 /// @param current A reference to the Current object of the request.
46
47 /// Move constructor.
48 /// @param other The response to move into this new response.
49 OutgoingResponse(OutgoingResponse&& other) noexcept = default;
50
51 /// Move assignment operator.
52 /// @param other The response to move into this response.
53 OutgoingResponse& operator=(OutgoingResponse&& other) noexcept = default;
54
55 OutgoingResponse(const OutgoingResponse&) = delete;
57
58 /// Gets the Current object of this response.
59 /// @return A const reference to the Current object.
60 /// @remark The response only holds onto a reference for this Current object. The caller keeps the Current
61 /// object alive until the call to `sendResponse` completes.
62 [[nodiscard]] const Current& current() const noexcept { return _current.get(); }
63
64 /// Gets the exception ID of the response.
65 /// @return The exception ID of the response. It's empty when #replyStatus is ReplyStatus::Ok or
66 /// ReplyStatus::UserException. Otherwise, this ID is the value returned by LocalException#ice_id. For other
67 /// exceptions, this ID is the value returned by `std::exception::what()`.
68 [[nodiscard]] const std::string& exceptionId() const noexcept { return _exceptionId; }
69
70 /// Gets the full details of the exception marshaled into the response.
71 /// @return The exception details. For Ice exceptions, it's produced by Exception::ice_print. It's empty
72 /// when #replyStatus is ReplyStatus::Ok or ReplyStatus::UserException.
73 [[nodiscard]] const std::string& exceptionDetails() const noexcept { return _exceptionDetails; }
74
75 /// Gets the reply status of the response.
76 /// @return The reply status.
77 [[nodiscard]] ReplyStatus replyStatus() const noexcept { return _replyStatus; }
78
79 /// Gets the output stream buffer of the response.
80 /// @return A reference to the output stream buffer.
81 OutputStream& outputStream() noexcept { return _outputStream; }
82
83 /// Gets the number of bytes in the response.
84 /// @return The number of bytes in the response.
85 [[nodiscard]] std::int32_t size() const noexcept;
86
87 private:
88 std::reference_wrapper<const Current> _current;
89 std::string _exceptionId;
90 std::string _exceptionDetails;
91 OutputStream _outputStream;
92 ReplyStatus _replyStatus;
93 };
94
95 /// Creates an OutgoingResponse object with ReplyStatus::Ok.
96 /// @param marshal A function that writes the payload of the response to the output stream.
97 /// @param current The Current object of the incoming request.
98 /// @param format The class format to use when marshaling the response.
99 /// @return The new response.
101 const std::function<void(OutputStream*)>& marshal,
102 const Current& current,
103 std::optional<FormatType> format = std::nullopt) noexcept;
104
105 /// Creates an OutgoingResponse object with ReplyStatus::Ok and an empty payload.
106 /// @param current A reference to the Current object of the request.
107 /// @return The new response.
108 ICE_API OutgoingResponse makeEmptyOutgoingResponse(const Current& current) noexcept;
109
110 /// Creates an OutgoingResponse object with ReplyStatus::Ok or ReplyStatus::UserException.
111 /// @param ok When `true`, the reply status is ReplyStatus::Ok. When false, the reply status is
112 /// ReplyStatus::UserException.
113 /// @param encapsulation The payload-encapsulation of the response or the user exception. It should be encoded using
114 /// Current::encoding but this function does not verify it.
115 /// @param current A reference to the Current object of the request.
116 /// @return The new response.
118 bool ok,
119 std::pair<const std::byte*, const std::byte*> encapsulation,
120 const Current& current) noexcept;
121
122 /// Creates an OutgoingResponse object with a reply status other than ReplyStatus::Ok.
123 /// @param exception The exception to marshal into the response.
124 /// @param current A reference to the Current object of the request.
125 /// @return The new response.
126 ICE_API OutgoingResponse makeOutgoingResponse(std::exception_ptr exception, const Current& current) noexcept;
127}
128
129#endif
const Current & current() const noexcept
Gets the Current object of this response.
const std::string & exceptionId() const noexcept
Gets the exception ID of the response.
OutgoingResponse & operator=(OutgoingResponse &&other) noexcept=default
Move assignment operator.
OutgoingResponse(OutputStream outputStream, const Current &current) noexcept
Constructs an OutgoingResponse object with ReplyStatus::Ok.
OutgoingResponse(ReplyStatus replyStatus, std::string exceptionId, std::string exceptionDetails, OutputStream outputStream, const Current &current) noexcept
Constructs an OutgoingResponse object.
ReplyStatus replyStatus() const noexcept
Gets the reply status of the response.
const std::string & exceptionDetails() const noexcept
Gets the full details of the exception marshaled into the response.
OutputStream & outputStream() noexcept
Gets the output stream buffer of the response.
std::int32_t size() const noexcept
Gets the number of bytes in the response.
OutgoingResponse(OutgoingResponse &&other) noexcept=default
Move constructor.
Represents the response to an incoming request.
Represents a byte buffer used for marshaling data using the Slice encoding.
OutgoingResponse makeOutgoingResponse(const std::function< void(OutputStream *)> &marshal, const Current &current, std::optional< FormatType > format=std::nullopt) noexcept
Creates an OutgoingResponse object with ReplyStatus::Ok.
OutgoingResponse makeEmptyOutgoingResponse(const Current &current) noexcept
Creates an OutgoingResponse object with ReplyStatus::Ok and an empty payload.
FormatType
Specifies the format for marshaling classes and exceptions with the Slice 1.1 encoding.
Definition Format.h:12
ReplyStatus
Represents the status of a reply.
Definition ReplyStatus.h:28
@ Ok
The dispatch completed successfully.
Definition ReplyStatus.h:30
The Ice RPC framework.
Definition SampleEvent.h:59
Provides information about an incoming request being dispatched.
Definition Current.h:18