Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
Object.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_OBJECT_H
4#define ICE_OBJECT_H
5
6#include "IncomingRequest.h"
7#include "ObjectF.h"
8#include "OutgoingResponse.h"
9
10#include <functional>
11#include <string_view>
12
13namespace Ice
14{
15 /// The base class for servants.
16 /// @remark Object is a stateless polymorphic base class. Its copy constructor, move constructor, copy assignment
17 /// operator and move assignment operator are all deleted to prevent accidental slicing. Derived classes can
18 /// define these constructors and assignment operators to reenable copying, moving and slicing.
19 /// @headerfile Ice/Ice.h
20 class ICE_API Object
21 {
22 public:
23 /// Default constructor.
24 Object() noexcept = default;
25
26 virtual ~Object() = default;
27
28 Object(const Object&) = delete;
29 Object& operator=(const Object&) = delete;
30 Object(Object&&) = delete;
31 Object& operator=(Object&&) = delete;
32
33 /// Dispatches an incoming request and returns the corresponding outgoing response.
34 /// @param request The incoming request.
35 /// @param sendResponse A callback that the implementation calls to return the response. @p sendResponse does
36 /// not throw any exception and any @p sendResponse wrapper must not throw any exception. @p sendResponse can be
37 /// called by the thread that called dispatch (the "dispatch thread") or by another thread. The implementation
38 /// must call @p sendResponse exactly once or throw an exception.
39 /// @remark Calling @p sendResponse can be thought as returning the outgoing response. Just like when you return
40 /// a value from a remote operation, you can only return it once and you don't know if the client receives this
41 /// value. In practice, the Ice-provided @p sendResponse attempts to send the response to the client
42 /// synchronously, but may send it asynchronously. It can also silently fail to send the response back to the
43 /// client. This function is the main building block for the Ice dispatch pipeline. The implementation provided
44 /// by the base class (Object) dispatches incoming requests to the four `Object` operations (`ice_isA`,
45 /// `ice_ping`, `ice_ids` and `ice_id`), and throws OperationNotExistException for all other operations. This
46 /// base implementation is trivial and should be overridden and fully replaced by all derived classes.
47 virtual void dispatch(IncomingRequest& request, std::function<void(OutgoingResponse)> sendResponse);
48
49 /// Tests whether this object supports a specific Slice interface.
50 /// @param typeId The type ID of the Slice interface to test against.
51 /// @param current The Current object of the incoming request.
52 /// @return `true` if this object implements the Slice interface specified by @p typeId or implements a derived
53 /// interface, `false` otherwise.
54 [[nodiscard]] virtual bool ice_isA(std::string typeId, const Current& current) const;
55
56 /// @private
57 void _iceD_ice_isA(IncomingRequest&, std::function<void(OutgoingResponse)>);
58
59 /// Tests whether this object can be reached.
60 /// @param current The Current object of the incoming request.
61 virtual void ice_ping(const Current& current) const;
62
63 /// @private
64 void _iceD_ice_ping(IncomingRequest&, std::function<void(OutgoingResponse)>);
65
66 /// Gets the Slice interfaces supported by this object as a list of type IDs.
67 /// @param current The Current object of the incoming request.
68 /// @return The Slice type IDs of the interfaces supported by this object, in alphabetical order.
69 [[nodiscard]] virtual std::vector<std::string> ice_ids(const Current& current) const;
70
71 /// @private
72 void _iceD_ice_ids(IncomingRequest&, std::function<void(OutgoingResponse)>);
73
74 /// Gets the type ID of the most-derived Slice interface supported by this object.
75 /// @param current The Current object of the incoming request.
76 /// @return The Slice type ID of the most-derived interface.
77 [[nodiscard]] virtual std::string ice_id(const Current& current) const;
78
79 /// @private
80 void _iceD_ice_id(IncomingRequest&, std::function<void(OutgoingResponse)>);
81
82 /// Gets the type ID of the associated Slice interface.
83 /// @return The string `"::Ice::Object"`.
84 static const char* ice_staticId() noexcept;
85 };
86
87 /// Base class for dynamic dispatch servants.
88 /// @remark This class is provided for backward compatibility. You should consider deriving directly from Object
89 /// and overriding the Object::dispatch function.
90 /// @headerfile Ice/Ice.h
91 class ICE_API Blobject : public Object
92 {
93 public:
94 /// Dispatches an incoming request.
95 /// @param inEncaps An encapsulation containing the encoded in-parameters for the operation.
96 /// @param outEncaps An encapsulation containing the encoded result for the operation.
97 /// @param current The Current object of the incoming request.
98 /// @return `true` if the dispatch completes successfully, `false` if the dispatch completes with a user
99 /// exception encoded in @p outEncaps.
100 virtual bool
101 ice_invoke(std::vector<std::byte> inEncaps, std::vector<std::byte>& outEncaps, const Current& current) = 0;
102
103 /// @private
104 void dispatch(IncomingRequest& request, std::function<void(OutgoingResponse)> sendResponse) final;
105 };
106
107 /// Base class for dynamic dispatch servants that uses the array mapping.
108 /// @remark This class is provided for backward compatibility. You should consider deriving directly from Object
109 /// and overriding the Object::dispatch function.
110 /// @headerfile Ice/Ice.h
111 class ICE_API BlobjectArray : public Object
112 {
113 public:
114 /// @copydoc Blobject::ice_invoke
115 virtual bool ice_invoke(
116 std::pair<const std::byte*, const std::byte*> inEncaps,
117 std::vector<std::byte>& outEncaps,
118 const Current& current) = 0;
119
120 /// @private
121 void dispatch(IncomingRequest&, std::function<void(OutgoingResponse)>) final;
122 };
123
124 /// Base class for asynchronous dynamic dispatch servants.
125 /// @remark This class is provided for backward compatibility. You should consider deriving directly from Object
126 /// and overriding the Object::dispatch function.
127 /// @headerfile Ice/Ice.h
128 class ICE_API BlobjectAsync : public Object
129 {
130 public:
131 /// Dispatches an incoming request asynchronously.
132 /// @param inEncaps An encapsulation containing the encoded in-parameters for the operation.
133 /// @param response The response callback. It accepts:
134 /// - `returnValue` `true` if the operation completed successfully, `false` if it completed with a user
135 /// exception encoded in @p outEncaps.
136 /// - `outEncaps` An encapsulation containing the encoded result.
137 /// @param exception The exception callback.
138 /// @param current The Current object of the incoming request.
139 virtual void ice_invokeAsync(
140 std::vector<std::byte> inEncaps,
141 std::function<void(bool, const std::vector<std::byte>&)> response,
142 std::function<void(std::exception_ptr)> exception,
143 const Current& current) = 0;
144
145 /// @private
146 void dispatch(IncomingRequest&, std::function<void(OutgoingResponse)>) final;
147 };
148
149 /// Base class for asynchronous dynamic dispatch servants that uses the array mapping.
150 /// @remark This class is provided for backward compatibility. You should consider deriving directly from Object
151 /// and overriding the Object::dispatch function.
152 /// @headerfile Ice/Ice.h
153 class ICE_API BlobjectArrayAsync : public Object
154 {
155 public:
156 /// @copydoc BlobjectAsync::ice_invokeAsync
157 virtual void ice_invokeAsync(
158 std::pair<const std::byte*, const std::byte*> inEncaps,
159 std::function<void(bool, std::pair<const std::byte*, const std::byte*>)> response,
160 std::function<void(std::exception_ptr)> exception,
161 const Current& current) = 0;
162
163 /// @private
164 void dispatch(IncomingRequest&, std::function<void(OutgoingResponse)>) final;
165 };
166}
167
168#endif
virtual void ice_invokeAsync(std::pair< const std::byte *, const std::byte * > inEncaps, std::function< void(bool, std::pair< const std::byte *, const std::byte * >)> response, std::function< void(std::exception_ptr)> exception, const Current &current)=0
Dispatches an incoming request asynchronously.
Base class for asynchronous dynamic dispatch servants that uses the array mapping.
Definition Object.h:154
virtual bool ice_invoke(std::pair< const std::byte *, const std::byte * > inEncaps, std::vector< std::byte > &outEncaps, const Current &current)=0
Dispatches an incoming request.
Base class for dynamic dispatch servants that uses the array mapping.
Definition Object.h:112
virtual void ice_invokeAsync(std::vector< std::byte > inEncaps, std::function< void(bool, const std::vector< std::byte > &)> response, std::function< void(std::exception_ptr)> exception, const Current &current)=0
Dispatches an incoming request asynchronously.
Base class for asynchronous dynamic dispatch servants.
Definition Object.h:129
virtual bool ice_invoke(std::vector< std::byte > inEncaps, std::vector< std::byte > &outEncaps, const Current &current)=0
Dispatches an incoming request.
Base class for dynamic dispatch servants.
Definition Object.h:92
Represents a request received by a connection.
virtual std::string ice_id(const Current &current) const
Gets the type ID of the most-derived Slice interface supported by this object.
Object() noexcept=default
Default constructor.
virtual void dispatch(IncomingRequest &request, std::function< void(OutgoingResponse)> sendResponse)
Dispatches an incoming request and returns the corresponding outgoing response.
virtual std::vector< std::string > ice_ids(const Current &current) const
Gets the Slice interfaces supported by this object as a list of type IDs.
virtual bool ice_isA(std::string typeId, const Current &current) const
Tests whether this object supports a specific Slice interface.
virtual void ice_ping(const Current &current) const
Tests whether this object can be reached.
static const char * ice_staticId() noexcept
Gets the type ID of the associated Slice interface.
Represents the response to an incoming request.
The Ice RPC framework.
Definition SampleEvent.h:59
Provides information about an incoming request being dispatched.
Definition Current.h:18