annotate win64-msvc/include/capnp/capability.h @ 74:2f2b27544483

Rebuild win32 Opus using mingw 5 rather than 7 to avoid runtime incompatibility
author Chris Cannam
date Wed, 30 Jan 2019 10:30:56 +0000
parents 0f2d93caa50c
children
rev   line source
Chris@63 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@63 2 // Licensed under the MIT License:
Chris@63 3 //
Chris@63 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@63 5 // of this software and associated documentation files (the "Software"), to deal
Chris@63 6 // in the Software without restriction, including without limitation the rights
Chris@63 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@63 8 // copies of the Software, and to permit persons to whom the Software is
Chris@63 9 // furnished to do so, subject to the following conditions:
Chris@63 10 //
Chris@63 11 // The above copyright notice and this permission notice shall be included in
Chris@63 12 // all copies or substantial portions of the Software.
Chris@63 13 //
Chris@63 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@63 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@63 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@63 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@63 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@63 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@63 20 // THE SOFTWARE.
Chris@63 21
Chris@63 22 #ifndef CAPNP_CAPABILITY_H_
Chris@63 23 #define CAPNP_CAPABILITY_H_
Chris@63 24
Chris@63 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@63 26 #pragma GCC system_header
Chris@63 27 #endif
Chris@63 28
Chris@63 29 #if CAPNP_LITE
Chris@63 30 #error "RPC APIs, including this header, are not available in lite mode."
Chris@63 31 #endif
Chris@63 32
Chris@63 33 #include <kj/async.h>
Chris@63 34 #include <kj/vector.h>
Chris@63 35 #include "raw-schema.h"
Chris@63 36 #include "any.h"
Chris@63 37 #include "pointer-helpers.h"
Chris@63 38
Chris@63 39 namespace capnp {
Chris@63 40
Chris@63 41 template <typename Results>
Chris@63 42 class Response;
Chris@63 43
Chris@63 44 template <typename T>
Chris@63 45 class RemotePromise: public kj::Promise<Response<T>>, public T::Pipeline {
Chris@63 46 // A Promise which supports pipelined calls. T is typically a struct type. T must declare
Chris@63 47 // an inner "mix-in" type "Pipeline" which implements pipelining; RemotePromise simply
Chris@63 48 // multiply-inherits that type along with Promise<Response<T>>. T::Pipeline must be movable,
Chris@63 49 // but does not need to be copyable (i.e. just like Promise<T>).
Chris@63 50 //
Chris@63 51 // The promise is for an owned pointer so that the RPC system can allocate the MessageReader
Chris@63 52 // itself.
Chris@63 53
Chris@63 54 public:
Chris@63 55 inline RemotePromise(kj::Promise<Response<T>>&& promise, typename T::Pipeline&& pipeline)
Chris@63 56 : kj::Promise<Response<T>>(kj::mv(promise)),
Chris@63 57 T::Pipeline(kj::mv(pipeline)) {}
Chris@63 58 inline RemotePromise(decltype(nullptr))
Chris@63 59 : kj::Promise<Response<T>>(nullptr),
Chris@63 60 T::Pipeline(nullptr) {}
Chris@63 61 KJ_DISALLOW_COPY(RemotePromise);
Chris@63 62 RemotePromise(RemotePromise&& other) = default;
Chris@63 63 RemotePromise& operator=(RemotePromise&& other) = default;
Chris@63 64 };
Chris@63 65
Chris@63 66 class LocalClient;
Chris@63 67 namespace _ { // private
Chris@63 68 extern const RawSchema NULL_INTERFACE_SCHEMA; // defined in schema.c++
Chris@63 69 class CapabilityServerSetBase;
Chris@63 70 } // namespace _ (private)
Chris@63 71
Chris@63 72 struct Capability {
Chris@63 73 // A capability without type-safe methods. Typed capability clients wrap `Client` and typed
Chris@63 74 // capability servers subclass `Server` to dispatch to the regular, typed methods.
Chris@63 75
Chris@63 76 class Client;
Chris@63 77 class Server;
Chris@63 78
Chris@63 79 struct _capnpPrivate {
Chris@63 80 struct IsInterface;
Chris@63 81 static constexpr uint64_t typeId = 0x3;
Chris@63 82 static constexpr Kind kind = Kind::INTERFACE;
Chris@63 83 static constexpr _::RawSchema const* schema = &_::NULL_INTERFACE_SCHEMA;
Chris@63 84
Chris@63 85 static const _::RawBrandedSchema* brand() {
Chris@63 86 return &_::NULL_INTERFACE_SCHEMA.defaultBrand;
Chris@63 87 }
Chris@63 88 };
Chris@63 89 };
Chris@63 90
Chris@63 91 // =======================================================================================
Chris@63 92 // Capability clients
Chris@63 93
Chris@63 94 class RequestHook;
Chris@63 95 class ResponseHook;
Chris@63 96 class PipelineHook;
Chris@63 97 class ClientHook;
Chris@63 98
Chris@63 99 template <typename Params, typename Results>
Chris@63 100 class Request: public Params::Builder {
Chris@63 101 // A call that hasn't been sent yet. This class extends a Builder for the call's "Params"
Chris@63 102 // structure with a method send() that actually sends it.
Chris@63 103 //
Chris@63 104 // Given a Cap'n Proto method `foo(a :A, b :B): C`, the generated client interface will have
Chris@63 105 // a method `Request<FooParams, C> fooRequest()` (as well as a convenience method
Chris@63 106 // `RemotePromise<C> foo(A::Reader a, B::Reader b)`).
Chris@63 107
Chris@63 108 public:
Chris@63 109 inline Request(typename Params::Builder builder, kj::Own<RequestHook>&& hook)
Chris@63 110 : Params::Builder(builder), hook(kj::mv(hook)) {}
Chris@63 111 inline Request(decltype(nullptr)): Params::Builder(nullptr) {}
Chris@63 112
Chris@63 113 RemotePromise<Results> send() KJ_WARN_UNUSED_RESULT;
Chris@63 114 // Send the call and return a promise for the results.
Chris@63 115
Chris@63 116 private:
Chris@63 117 kj::Own<RequestHook> hook;
Chris@63 118
Chris@63 119 friend class Capability::Client;
Chris@63 120 friend struct DynamicCapability;
Chris@63 121 template <typename, typename>
Chris@63 122 friend class CallContext;
Chris@63 123 friend class RequestHook;
Chris@63 124 };
Chris@63 125
Chris@63 126 template <typename Results>
Chris@63 127 class Response: public Results::Reader {
Chris@63 128 // A completed call. This class extends a Reader for the call's answer structure. The Response
Chris@63 129 // is move-only -- once it goes out-of-scope, the underlying message will be freed.
Chris@63 130
Chris@63 131 public:
Chris@63 132 inline Response(typename Results::Reader reader, kj::Own<ResponseHook>&& hook)
Chris@63 133 : Results::Reader(reader), hook(kj::mv(hook)) {}
Chris@63 134
Chris@63 135 private:
Chris@63 136 kj::Own<ResponseHook> hook;
Chris@63 137
Chris@63 138 template <typename, typename>
Chris@63 139 friend class Request;
Chris@63 140 friend class ResponseHook;
Chris@63 141 };
Chris@63 142
Chris@63 143 class Capability::Client {
Chris@63 144 // Base type for capability clients.
Chris@63 145
Chris@63 146 public:
Chris@63 147 typedef Capability Reads;
Chris@63 148 typedef Capability Calls;
Chris@63 149
Chris@63 150 Client(decltype(nullptr));
Chris@63 151 // If you need to declare a Client before you have anything to assign to it (perhaps because
Chris@63 152 // the assignment is going to occur in an if/else scope), you can start by initializing it to
Chris@63 153 // `nullptr`. The resulting client is not meant to be called and throws exceptions from all
Chris@63 154 // methods.
Chris@63 155
Chris@63 156 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, Capability::Server*>()>>
Chris@63 157 Client(kj::Own<T>&& server);
Chris@63 158 // Make a client capability that wraps the given server capability. The server's methods will
Chris@63 159 // only be executed in the given EventLoop, regardless of what thread calls the client's methods.
Chris@63 160
Chris@63 161 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, Client*>()>>
Chris@63 162 Client(kj::Promise<T>&& promise);
Chris@63 163 // Make a client from a promise for a future client. The resulting client queues calls until the
Chris@63 164 // promise resolves.
Chris@63 165
Chris@63 166 Client(kj::Exception&& exception);
Chris@63 167 // Make a broken client that throws the given exception from all calls.
Chris@63 168
Chris@63 169 Client(Client& other);
Chris@63 170 Client& operator=(Client& other);
Chris@63 171 // Copies by reference counting. Warning: This refcounting is not thread-safe. All copies of
Chris@63 172 // the client must remain in one thread.
Chris@63 173
Chris@63 174 Client(Client&&) = default;
Chris@63 175 Client& operator=(Client&&) = default;
Chris@63 176 // Move constructor avoids reference counting.
Chris@63 177
Chris@63 178 explicit Client(kj::Own<ClientHook>&& hook);
Chris@63 179 // For use by the RPC implementation: Wrap a ClientHook.
Chris@63 180
Chris@63 181 template <typename T>
Chris@63 182 typename T::Client castAs();
Chris@63 183 // Reinterpret the capability as implementing the given interface. Note that no error will occur
Chris@63 184 // here if the capability does not actually implement this interface, but later method calls will
Chris@63 185 // fail. It's up to the application to decide how indicate that additional interfaces are
Chris@63 186 // supported.
Chris@63 187 //
Chris@63 188 // TODO(perf): GCC 4.8 / Clang 3.3: rvalue-qualified version for better performance.
Chris@63 189
Chris@63 190 template <typename T>
Chris@63 191 typename T::Client castAs(InterfaceSchema schema);
Chris@63 192 // Dynamic version. `T` must be `DynamicCapability`, and you must `#include <capnp/dynamic.h>`.
Chris@63 193
Chris@63 194 kj::Promise<void> whenResolved();
Chris@63 195 // If the capability is actually only a promise, the returned promise resolves once the
Chris@63 196 // capability itself has resolved to its final destination (or propagates the exception if
Chris@63 197 // the capability promise is rejected). This is mainly useful for error-checking in the case
Chris@63 198 // where no calls are being made. There is no reason to wait for this before making calls; if
Chris@63 199 // the capability does not resolve, the call results will propagate the error.
Chris@63 200
Chris@63 201 Request<AnyPointer, AnyPointer> typelessRequest(
Chris@63 202 uint64_t interfaceId, uint16_t methodId,
Chris@63 203 kj::Maybe<MessageSize> sizeHint);
Chris@63 204 // Make a request without knowing the types of the params or results. You specify the type ID
Chris@63 205 // and method number manually.
Chris@63 206
Chris@63 207 // TODO(someday): method(s) for Join
Chris@63 208
Chris@63 209 protected:
Chris@63 210 Client() = default;
Chris@63 211
Chris@63 212 template <typename Params, typename Results>
Chris@63 213 Request<Params, Results> newCall(uint64_t interfaceId, uint16_t methodId,
Chris@63 214 kj::Maybe<MessageSize> sizeHint);
Chris@63 215
Chris@63 216 private:
Chris@63 217 kj::Own<ClientHook> hook;
Chris@63 218
Chris@63 219 static kj::Own<ClientHook> makeLocalClient(kj::Own<Capability::Server>&& server);
Chris@63 220
Chris@63 221 template <typename, Kind>
Chris@63 222 friend struct _::PointerHelpers;
Chris@63 223 friend struct DynamicCapability;
Chris@63 224 friend class Orphanage;
Chris@63 225 friend struct DynamicStruct;
Chris@63 226 friend struct DynamicList;
Chris@63 227 template <typename, Kind>
Chris@63 228 friend struct List;
Chris@63 229 friend class _::CapabilityServerSetBase;
Chris@63 230 friend class ClientHook;
Chris@63 231 };
Chris@63 232
Chris@63 233 // =======================================================================================
Chris@63 234 // Capability servers
Chris@63 235
Chris@63 236 class CallContextHook;
Chris@63 237
Chris@63 238 template <typename Params, typename Results>
Chris@63 239 class CallContext: public kj::DisallowConstCopy {
Chris@63 240 // Wrapper around CallContextHook with a specific return type.
Chris@63 241 //
Chris@63 242 // Methods of this class may only be called from within the server's event loop, not from other
Chris@63 243 // threads.
Chris@63 244 //
Chris@63 245 // The CallContext becomes invalid as soon as the call reports completion.
Chris@63 246
Chris@63 247 public:
Chris@63 248 explicit CallContext(CallContextHook& hook);
Chris@63 249
Chris@63 250 typename Params::Reader getParams();
Chris@63 251 // Get the params payload.
Chris@63 252
Chris@63 253 void releaseParams();
Chris@63 254 // Release the params payload. getParams() will throw an exception after this is called.
Chris@63 255 // Releasing the params may allow the RPC system to free up buffer space to handle other
Chris@63 256 // requests. Long-running asynchronous methods should try to call this as early as is
Chris@63 257 // convenient.
Chris@63 258
Chris@63 259 typename Results::Builder getResults(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@63 260 typename Results::Builder initResults(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@63 261 void setResults(typename Results::Reader value);
Chris@63 262 void adoptResults(Orphan<Results>&& value);
Chris@63 263 Orphanage getResultsOrphanage(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@63 264 // Manipulate the results payload. The "Return" message (part of the RPC protocol) will
Chris@63 265 // typically be allocated the first time one of these is called. Some RPC systems may
Chris@63 266 // allocate these messages in a limited space (such as a shared memory segment), therefore the
Chris@63 267 // application should delay calling these as long as is convenient to do so (but don't delay
Chris@63 268 // if doing so would require extra copies later).
Chris@63 269 //
Chris@63 270 // `sizeHint` indicates a guess at the message size. This will usually be used to decide how
Chris@63 271 // much space to allocate for the first message segment (don't worry: only space that is actually
Chris@63 272 // used will be sent on the wire). If omitted, the system decides. The message root pointer
Chris@63 273 // should not be included in the size. So, if you are simply going to copy some existing message
Chris@63 274 // directly into the results, just call `.totalSize()` and pass that in.
Chris@63 275
Chris@63 276 template <typename SubParams>
Chris@63 277 kj::Promise<void> tailCall(Request<SubParams, Results>&& tailRequest);
Chris@63 278 // Resolve the call by making a tail call. `tailRequest` is a request that has been filled in
Chris@63 279 // but not yet sent. The context will send the call, then fill in the results with the result
Chris@63 280 // of the call. If tailCall() is used, {get,init,set,adopt}Results (above) *must not* be called.
Chris@63 281 //
Chris@63 282 // The RPC implementation may be able to optimize a tail call to another machine such that the
Chris@63 283 // results never actually pass through this machine. Even if no such optimization is possible,
Chris@63 284 // `tailCall()` may allow pipelined calls to be forwarded optimistically to the new call site.
Chris@63 285 //
Chris@63 286 // In general, this should be the last thing a method implementation calls, and the promise
Chris@63 287 // returned from `tailCall()` should then be returned by the method implementation.
Chris@63 288
Chris@63 289 void allowCancellation();
Chris@63 290 // Indicate that it is OK for the RPC system to discard its Promise for this call's result if
Chris@63 291 // the caller cancels the call, thereby transitively canceling any asynchronous operations the
Chris@63 292 // call implementation was performing. This is not done by default because it could represent a
Chris@63 293 // security risk: applications must be carefully written to ensure that they do not end up in
Chris@63 294 // a bad state if an operation is canceled at an arbitrary point. However, for long-running
Chris@63 295 // method calls that hold significant resources, prompt cancellation is often useful.
Chris@63 296 //
Chris@63 297 // Keep in mind that asynchronous cancellation cannot occur while the method is synchronously
Chris@63 298 // executing on a local thread. The method must perform an asynchronous operation or call
Chris@63 299 // `EventLoop::current().evalLater()` to yield control.
Chris@63 300 //
Chris@63 301 // Note: You might think that we should offer `onCancel()` and/or `isCanceled()` methods that
Chris@63 302 // provide notification when the caller cancels the request without forcefully killing off the
Chris@63 303 // promise chain. Unfortunately, this composes poorly with promise forking: the canceled
Chris@63 304 // path may be just one branch of a fork of the result promise. The other branches still want
Chris@63 305 // the call to continue. Promise forking is used within the Cap'n Proto implementation -- in
Chris@63 306 // particular each pipelined call forks the result promise. So, if a caller made a pipelined
Chris@63 307 // call and then dropped the original object, the call should not be canceled, but it would be
Chris@63 308 // excessively complicated for the framework to avoid notififying of cancellation as long as
Chris@63 309 // pipelined calls still exist.
Chris@63 310
Chris@63 311 private:
Chris@63 312 CallContextHook* hook;
Chris@63 313
Chris@63 314 friend class Capability::Server;
Chris@63 315 friend struct DynamicCapability;
Chris@63 316 };
Chris@63 317
Chris@63 318 class Capability::Server {
Chris@63 319 // Objects implementing a Cap'n Proto interface must subclass this. Typically, such objects
Chris@63 320 // will instead subclass a typed Server interface which will take care of implementing
Chris@63 321 // dispatchCall().
Chris@63 322
Chris@63 323 public:
Chris@63 324 typedef Capability Serves;
Chris@63 325
Chris@63 326 virtual kj::Promise<void> dispatchCall(uint64_t interfaceId, uint16_t methodId,
Chris@63 327 CallContext<AnyPointer, AnyPointer> context) = 0;
Chris@63 328 // Call the given method. `params` is the input struct, and should be released as soon as it
Chris@63 329 // is no longer needed. `context` may be used to allocate the output struct and deal with
Chris@63 330 // cancellation.
Chris@63 331
Chris@63 332 // TODO(someday): Method which can optionally be overridden to implement Join when the object is
Chris@63 333 // a proxy.
Chris@63 334
Chris@63 335 protected:
Chris@63 336 inline Capability::Client thisCap();
Chris@63 337 // Get a capability pointing to this object, much like the `this` keyword.
Chris@63 338 //
Chris@63 339 // The effect of this method is undefined if:
Chris@63 340 // - No capability client has been created pointing to this object. (This is always the case in
Chris@63 341 // the server's constructor.)
Chris@63 342 // - The capability client pointing at this object has been destroyed. (This is always the case
Chris@63 343 // in the server's destructor.)
Chris@63 344 // - Multiple capability clients have been created around the same server (possible if the server
Chris@63 345 // is refcounted, which is not recommended since the client itself provides refcounting).
Chris@63 346
Chris@63 347 template <typename Params, typename Results>
Chris@63 348 CallContext<Params, Results> internalGetTypedContext(
Chris@63 349 CallContext<AnyPointer, AnyPointer> typeless);
Chris@63 350 kj::Promise<void> internalUnimplemented(const char* actualInterfaceName,
Chris@63 351 uint64_t requestedTypeId);
Chris@63 352 kj::Promise<void> internalUnimplemented(const char* interfaceName,
Chris@63 353 uint64_t typeId, uint16_t methodId);
Chris@63 354 kj::Promise<void> internalUnimplemented(const char* interfaceName, const char* methodName,
Chris@63 355 uint64_t typeId, uint16_t methodId);
Chris@63 356
Chris@63 357 private:
Chris@63 358 ClientHook* thisHook = nullptr;
Chris@63 359 friend class LocalClient;
Chris@63 360 };
Chris@63 361
Chris@63 362 // =======================================================================================
Chris@63 363
Chris@63 364 class ReaderCapabilityTable: private _::CapTableReader {
Chris@63 365 // Class which imbues Readers with the ability to read capabilities.
Chris@63 366 //
Chris@63 367 // In Cap'n Proto format, the encoding of a capability pointer is simply an integer index into
Chris@63 368 // an external table. Since these pointers fundamentally point outside the message, a
Chris@63 369 // MessageReader by default has no idea what they point at, and therefore reading capabilities
Chris@63 370 // from such a reader will throw exceptions.
Chris@63 371 //
Chris@63 372 // In order to be able to read capabilities, you must first attach a capability table, using
Chris@63 373 // this class. By "imbuing" a Reader, you get a new Reader which will interpret capability
Chris@63 374 // pointers by treating them as indexes into the ReaderCapabilityTable.
Chris@63 375 //
Chris@63 376 // Note that when using Cap'n Proto's RPC system, this is handled automatically.
Chris@63 377
Chris@63 378 public:
Chris@63 379 explicit ReaderCapabilityTable(kj::Array<kj::Maybe<kj::Own<ClientHook>>> table);
Chris@63 380 KJ_DISALLOW_COPY(ReaderCapabilityTable);
Chris@63 381
Chris@63 382 template <typename T>
Chris@63 383 T imbue(T reader);
Chris@63 384 // Return a reader equivalent to `reader` except that when reading capability-valued fields,
Chris@63 385 // the capabilities are looked up in this table.
Chris@63 386
Chris@63 387 private:
Chris@63 388 kj::Array<kj::Maybe<kj::Own<ClientHook>>> table;
Chris@63 389
Chris@63 390 kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
Chris@63 391 };
Chris@63 392
Chris@63 393 class BuilderCapabilityTable: private _::CapTableBuilder {
Chris@63 394 // Class which imbues Builders with the ability to read and write capabilities.
Chris@63 395 //
Chris@63 396 // This is much like ReaderCapabilityTable, except for builders. The table starts out empty,
Chris@63 397 // but capabilities can be added to it over time.
Chris@63 398
Chris@63 399 public:
Chris@63 400 BuilderCapabilityTable();
Chris@63 401 KJ_DISALLOW_COPY(BuilderCapabilityTable);
Chris@63 402
Chris@63 403 inline kj::ArrayPtr<kj::Maybe<kj::Own<ClientHook>>> getTable() { return table; }
Chris@63 404
Chris@63 405 template <typename T>
Chris@63 406 T imbue(T builder);
Chris@63 407 // Return a builder equivalent to `builder` except that when reading capability-valued fields,
Chris@63 408 // the capabilities are looked up in this table.
Chris@63 409
Chris@63 410 private:
Chris@63 411 kj::Vector<kj::Maybe<kj::Own<ClientHook>>> table;
Chris@63 412
Chris@63 413 kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
Chris@63 414 uint injectCap(kj::Own<ClientHook>&& cap) override;
Chris@63 415 void dropCap(uint index) override;
Chris@63 416 };
Chris@63 417
Chris@63 418 // =======================================================================================
Chris@63 419
Chris@63 420 namespace _ { // private
Chris@63 421
Chris@63 422 class CapabilityServerSetBase {
Chris@63 423 public:
Chris@63 424 Capability::Client addInternal(kj::Own<Capability::Server>&& server, void* ptr);
Chris@63 425 kj::Promise<void*> getLocalServerInternal(Capability::Client& client);
Chris@63 426 };
Chris@63 427
Chris@63 428 } // namespace _ (private)
Chris@63 429
Chris@63 430 template <typename T>
Chris@63 431 class CapabilityServerSet: private _::CapabilityServerSetBase {
Chris@63 432 // Allows a server to recognize its own capabilities when passed back to it, and obtain the
Chris@63 433 // underlying Server objects associated with them.
Chris@63 434 //
Chris@63 435 // All objects in the set must have the same interface type T. The objects may implement various
Chris@63 436 // interfaces derived from T (and in fact T can be `capnp::Capability` to accept all objects),
Chris@63 437 // but note that if you compile with RTTI disabled then you will not be able to down-cast through
Chris@63 438 // virtual inheritance, and all inheritance between server interfaces is virtual. So, with RTTI
Chris@63 439 // disabled, you will likely need to set T to be the most-derived Cap'n Proto interface type,
Chris@63 440 // and you server class will need to be directly derived from that, so that you can use
Chris@63 441 // static_cast (or kj::downcast) to cast to it after calling getLocalServer(). (If you compile
Chris@63 442 // with RTTI, then you can freely dynamic_cast and ignore this issue!)
Chris@63 443
Chris@63 444 public:
Chris@63 445 CapabilityServerSet() = default;
Chris@63 446 KJ_DISALLOW_COPY(CapabilityServerSet);
Chris@63 447
Chris@63 448 typename T::Client add(kj::Own<typename T::Server>&& server);
Chris@63 449 // Create a new capability Client for the given Server and also add this server to the set.
Chris@63 450
Chris@63 451 kj::Promise<kj::Maybe<typename T::Server&>> getLocalServer(typename T::Client& client);
Chris@63 452 // Given a Client pointing to a server previously passed to add(), return the corresponding
Chris@63 453 // Server. This returns a promise because if the input client is itself a promise, this must
Chris@63 454 // wait for it to resolve. Keep in mind that the server will be deleted when all clients are
Chris@63 455 // gone, so the caller should make sure to keep the client alive (hence why this method only
Chris@63 456 // accepts an lvalue input).
Chris@63 457 };
Chris@63 458
Chris@63 459 // =======================================================================================
Chris@63 460 // Hook interfaces which must be implemented by the RPC system. Applications never call these
Chris@63 461 // directly; the RPC system implements them and the types defined earlier in this file wrap them.
Chris@63 462
Chris@63 463 class RequestHook {
Chris@63 464 // Hook interface implemented by RPC system representing a request being built.
Chris@63 465
Chris@63 466 public:
Chris@63 467 virtual RemotePromise<AnyPointer> send() = 0;
Chris@63 468 // Send the call and return a promise for the result.
Chris@63 469
Chris@63 470 virtual const void* getBrand() = 0;
Chris@63 471 // Returns a void* that identifies who made this request. This can be used by an RPC adapter to
Chris@63 472 // discover when tail call is going to be sent over its own connection and therefore can be
Chris@63 473 // optimized into a remote tail call.
Chris@63 474
Chris@63 475 template <typename T, typename U>
Chris@63 476 inline static kj::Own<RequestHook> from(Request<T, U>&& request) {
Chris@63 477 return kj::mv(request.hook);
Chris@63 478 }
Chris@63 479 };
Chris@63 480
Chris@63 481 class ResponseHook {
Chris@63 482 // Hook interface implemented by RPC system representing a response.
Chris@63 483 //
Chris@63 484 // At present this class has no methods. It exists only for garbage collection -- when the
Chris@63 485 // ResponseHook is destroyed, the results can be freed.
Chris@63 486
Chris@63 487 public:
Chris@63 488 virtual ~ResponseHook() noexcept(false);
Chris@63 489 // Just here to make sure the type is dynamic.
Chris@63 490
Chris@63 491 template <typename T>
Chris@63 492 inline static kj::Own<ResponseHook> from(Response<T>&& response) {
Chris@63 493 return kj::mv(response.hook);
Chris@63 494 }
Chris@63 495 };
Chris@63 496
Chris@63 497 // class PipelineHook is declared in any.h because it is needed there.
Chris@63 498
Chris@63 499 class ClientHook {
Chris@63 500 public:
Chris@63 501 ClientHook();
Chris@63 502
Chris@63 503 virtual Request<AnyPointer, AnyPointer> newCall(
Chris@63 504 uint64_t interfaceId, uint16_t methodId, kj::Maybe<MessageSize> sizeHint) = 0;
Chris@63 505 // Start a new call, allowing the client to allocate request/response objects as it sees fit.
Chris@63 506 // This version is used when calls are made from application code in the local process.
Chris@63 507
Chris@63 508 struct VoidPromiseAndPipeline {
Chris@63 509 kj::Promise<void> promise;
Chris@63 510 kj::Own<PipelineHook> pipeline;
Chris@63 511 };
Chris@63 512
Chris@63 513 virtual VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId,
Chris@63 514 kj::Own<CallContextHook>&& context) = 0;
Chris@63 515 // Call the object, but the caller controls allocation of the request/response objects. If the
Chris@63 516 // callee insists on allocating these objects itself, it must make a copy. This version is used
Chris@63 517 // when calls come in over the network via an RPC system. Note that even if the returned
Chris@63 518 // `Promise<void>` is discarded, the call may continue executing if any pipelined calls are
Chris@63 519 // waiting for it.
Chris@63 520 //
Chris@63 521 // Since the caller of this method chooses the CallContext implementation, it is the caller's
Chris@63 522 // responsibility to ensure that the returned promise is not canceled unless allowed via
Chris@63 523 // the context's `allowCancellation()`.
Chris@63 524 //
Chris@63 525 // The call must not begin synchronously; the callee must arrange for the call to begin in a
Chris@63 526 // later turn of the event loop. Otherwise, application code may call back and affect the
Chris@63 527 // callee's state in an unexpected way.
Chris@63 528
Chris@63 529 virtual kj::Maybe<ClientHook&> getResolved() = 0;
Chris@63 530 // If this ClientHook is a promise that has already resolved, returns the inner, resolved version
Chris@63 531 // of the capability. The caller may permanently replace this client with the resolved one if
Chris@63 532 // desired. Returns null if the client isn't a promise or hasn't resolved yet -- use
Chris@63 533 // `whenMoreResolved()` to distinguish between them.
Chris@63 534
Chris@63 535 virtual kj::Maybe<kj::Promise<kj::Own<ClientHook>>> whenMoreResolved() = 0;
Chris@63 536 // If this client is a settled reference (not a promise), return nullptr. Otherwise, return a
Chris@63 537 // promise that eventually resolves to a new client that is closer to being the final, settled
Chris@63 538 // client (i.e. the value eventually returned by `getResolved()`). Calling this repeatedly
Chris@63 539 // should eventually produce a settled client.
Chris@63 540
Chris@63 541 kj::Promise<void> whenResolved();
Chris@63 542 // Repeatedly calls whenMoreResolved() until it returns nullptr.
Chris@63 543
Chris@63 544 virtual kj::Own<ClientHook> addRef() = 0;
Chris@63 545 // Return a new reference to the same capability.
Chris@63 546
Chris@63 547 virtual const void* getBrand() = 0;
Chris@63 548 // Returns a void* that identifies who made this client. This can be used by an RPC adapter to
Chris@63 549 // discover when a capability it needs to marshal is one that it created in the first place, and
Chris@63 550 // therefore it can transfer the capability without proxying.
Chris@63 551
Chris@63 552 static const uint NULL_CAPABILITY_BRAND;
Chris@63 553 // Value is irrelevant; used for pointer.
Chris@63 554
Chris@63 555 inline bool isNull() { return getBrand() == &NULL_CAPABILITY_BRAND; }
Chris@63 556 // Returns true if the capability was created as a result of assigning a Client to null or by
Chris@63 557 // reading a null pointer out of a Cap'n Proto message.
Chris@63 558
Chris@63 559 virtual void* getLocalServer(_::CapabilityServerSetBase& capServerSet);
Chris@63 560 // If this is a local capability created through `capServerSet`, return the underlying Server.
Chris@63 561 // Otherwise, return nullptr. Default implementation (which everyone except LocalClient should
Chris@63 562 // use) always returns nullptr.
Chris@63 563
Chris@63 564 static kj::Own<ClientHook> from(Capability::Client client) { return kj::mv(client.hook); }
Chris@63 565 };
Chris@63 566
Chris@63 567 class CallContextHook {
Chris@63 568 // Hook interface implemented by RPC system to manage a call on the server side. See
Chris@63 569 // CallContext<T>.
Chris@63 570
Chris@63 571 public:
Chris@63 572 virtual AnyPointer::Reader getParams() = 0;
Chris@63 573 virtual void releaseParams() = 0;
Chris@63 574 virtual AnyPointer::Builder getResults(kj::Maybe<MessageSize> sizeHint) = 0;
Chris@63 575 virtual kj::Promise<void> tailCall(kj::Own<RequestHook>&& request) = 0;
Chris@63 576 virtual void allowCancellation() = 0;
Chris@63 577
Chris@63 578 virtual kj::Promise<AnyPointer::Pipeline> onTailCall() = 0;
Chris@63 579 // If `tailCall()` is called, resolves to the PipelineHook from the tail call. An
Chris@63 580 // implementation of `ClientHook::call()` is allowed to call this at most once.
Chris@63 581
Chris@63 582 virtual ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own<RequestHook>&& request) = 0;
Chris@63 583 // Call this when you would otherwise call onTailCall() immediately followed by tailCall().
Chris@63 584 // Implementations of tailCall() should typically call directTailCall() and then fulfill the
Chris@63 585 // promise fulfiller for onTailCall() with the returned pipeline.
Chris@63 586
Chris@63 587 virtual kj::Own<CallContextHook> addRef() = 0;
Chris@63 588 };
Chris@63 589
Chris@63 590 kj::Own<ClientHook> newLocalPromiseClient(kj::Promise<kj::Own<ClientHook>>&& promise);
Chris@63 591 // Returns a ClientHook that queues up calls until `promise` resolves, then forwards them to
Chris@63 592 // the new client. This hook's `getResolved()` and `whenMoreResolved()` methods will reflect the
Chris@63 593 // redirection to the eventual replacement client.
Chris@63 594
Chris@63 595 kj::Own<PipelineHook> newLocalPromisePipeline(kj::Promise<kj::Own<PipelineHook>>&& promise);
Chris@63 596 // Returns a PipelineHook that queues up calls until `promise` resolves, then forwards them to
Chris@63 597 // the new pipeline.
Chris@63 598
Chris@63 599 kj::Own<ClientHook> newBrokenCap(kj::StringPtr reason);
Chris@63 600 kj::Own<ClientHook> newBrokenCap(kj::Exception&& reason);
Chris@63 601 // Helper function that creates a capability which simply throws exceptions when called.
Chris@63 602
Chris@63 603 kj::Own<PipelineHook> newBrokenPipeline(kj::Exception&& reason);
Chris@63 604 // Helper function that creates a pipeline which simply throws exceptions when called.
Chris@63 605
Chris@63 606 Request<AnyPointer, AnyPointer> newBrokenRequest(
Chris@63 607 kj::Exception&& reason, kj::Maybe<MessageSize> sizeHint);
Chris@63 608 // Helper function that creates a Request object that simply throws exceptions when sent.
Chris@63 609
Chris@63 610 // =======================================================================================
Chris@63 611 // Extend PointerHelpers for interfaces
Chris@63 612
Chris@63 613 namespace _ { // private
Chris@63 614
Chris@63 615 template <typename T>
Chris@63 616 struct PointerHelpers<T, Kind::INTERFACE> {
Chris@63 617 static inline typename T::Client get(PointerReader reader) {
Chris@63 618 return typename T::Client(reader.getCapability());
Chris@63 619 }
Chris@63 620 static inline typename T::Client get(PointerBuilder builder) {
Chris@63 621 return typename T::Client(builder.getCapability());
Chris@63 622 }
Chris@63 623 static inline void set(PointerBuilder builder, typename T::Client&& value) {
Chris@63 624 builder.setCapability(kj::mv(value.Capability::Client::hook));
Chris@63 625 }
Chris@63 626 static inline void set(PointerBuilder builder, typename T::Client& value) {
Chris@63 627 builder.setCapability(value.Capability::Client::hook->addRef());
Chris@63 628 }
Chris@63 629 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
Chris@63 630 builder.adopt(kj::mv(value.builder));
Chris@63 631 }
Chris@63 632 static inline Orphan<T> disown(PointerBuilder builder) {
Chris@63 633 return Orphan<T>(builder.disown());
Chris@63 634 }
Chris@63 635 };
Chris@63 636
Chris@63 637 } // namespace _ (private)
Chris@63 638
Chris@63 639 // =======================================================================================
Chris@63 640 // Extend List for interfaces
Chris@63 641
Chris@63 642 template <typename T>
Chris@63 643 struct List<T, Kind::INTERFACE> {
Chris@63 644 List() = delete;
Chris@63 645
Chris@63 646 class Reader {
Chris@63 647 public:
Chris@63 648 typedef List<T> Reads;
Chris@63 649
Chris@63 650 Reader() = default;
Chris@63 651 inline explicit Reader(_::ListReader reader): reader(reader) {}
Chris@63 652
Chris@63 653 inline uint size() const { return unbound(reader.size() / ELEMENTS); }
Chris@63 654 inline typename T::Client operator[](uint index) const {
Chris@63 655 KJ_IREQUIRE(index < size());
Chris@63 656 return typename T::Client(reader.getPointerElement(
Chris@63 657 bounded(index) * ELEMENTS).getCapability());
Chris@63 658 }
Chris@63 659
Chris@63 660 typedef _::IndexingIterator<const Reader, typename T::Client> Iterator;
Chris@63 661 inline Iterator begin() const { return Iterator(this, 0); }
Chris@63 662 inline Iterator end() const { return Iterator(this, size()); }
Chris@63 663
Chris@63 664 private:
Chris@63 665 _::ListReader reader;
Chris@63 666 template <typename U, Kind K>
Chris@63 667 friend struct _::PointerHelpers;
Chris@63 668 template <typename U, Kind K>
Chris@63 669 friend struct List;
Chris@63 670 friend class Orphanage;
Chris@63 671 template <typename U, Kind K>
Chris@63 672 friend struct ToDynamic_;
Chris@63 673 };
Chris@63 674
Chris@63 675 class Builder {
Chris@63 676 public:
Chris@63 677 typedef List<T> Builds;
Chris@63 678
Chris@63 679 Builder() = delete;
Chris@63 680 inline Builder(decltype(nullptr)) {}
Chris@63 681 inline explicit Builder(_::ListBuilder builder): builder(builder) {}
Chris@63 682
Chris@63 683 inline operator Reader() const { return Reader(builder.asReader()); }
Chris@63 684 inline Reader asReader() const { return Reader(builder.asReader()); }
Chris@63 685
Chris@63 686 inline uint size() const { return unbound(builder.size() / ELEMENTS); }
Chris@63 687 inline typename T::Client operator[](uint index) {
Chris@63 688 KJ_IREQUIRE(index < size());
Chris@63 689 return typename T::Client(builder.getPointerElement(
Chris@63 690 bounded(index) * ELEMENTS).getCapability());
Chris@63 691 }
Chris@63 692 inline void set(uint index, typename T::Client value) {
Chris@63 693 KJ_IREQUIRE(index < size());
Chris@63 694 builder.getPointerElement(bounded(index) * ELEMENTS).setCapability(kj::mv(value.hook));
Chris@63 695 }
Chris@63 696 inline void adopt(uint index, Orphan<T>&& value) {
Chris@63 697 KJ_IREQUIRE(index < size());
Chris@63 698 builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(value));
Chris@63 699 }
Chris@63 700 inline Orphan<T> disown(uint index) {
Chris@63 701 KJ_IREQUIRE(index < size());
Chris@63 702 return Orphan<T>(builder.getPointerElement(bounded(index) * ELEMENTS).disown());
Chris@63 703 }
Chris@63 704
Chris@63 705 typedef _::IndexingIterator<Builder, typename T::Client> Iterator;
Chris@63 706 inline Iterator begin() { return Iterator(this, 0); }
Chris@63 707 inline Iterator end() { return Iterator(this, size()); }
Chris@63 708
Chris@63 709 private:
Chris@63 710 _::ListBuilder builder;
Chris@63 711 friend class Orphanage;
Chris@63 712 template <typename U, Kind K>
Chris@63 713 friend struct ToDynamic_;
Chris@63 714 };
Chris@63 715
Chris@63 716 private:
Chris@63 717 inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) {
Chris@63 718 return builder.initList(ElementSize::POINTER, bounded(size) * ELEMENTS);
Chris@63 719 }
Chris@63 720 inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) {
Chris@63 721 return builder.getList(ElementSize::POINTER, defaultValue);
Chris@63 722 }
Chris@63 723 inline static _::ListReader getFromPointer(
Chris@63 724 const _::PointerReader& reader, const word* defaultValue) {
Chris@63 725 return reader.getList(ElementSize::POINTER, defaultValue);
Chris@63 726 }
Chris@63 727
Chris@63 728 template <typename U, Kind k>
Chris@63 729 friend struct List;
Chris@63 730 template <typename U, Kind K>
Chris@63 731 friend struct _::PointerHelpers;
Chris@63 732 };
Chris@63 733
Chris@63 734 // =======================================================================================
Chris@63 735 // Inline implementation details
Chris@63 736
Chris@63 737 template <typename Params, typename Results>
Chris@63 738 RemotePromise<Results> Request<Params, Results>::send() {
Chris@63 739 auto typelessPromise = hook->send();
Chris@63 740 hook = nullptr; // prevent reuse
Chris@63 741
Chris@63 742 // Convert the Promise to return the correct response type.
Chris@63 743 // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the
Chris@63 744 // Pipeline part of the RemotePromise.
Chris@63 745 auto typedPromise = kj::implicitCast<kj::Promise<Response<AnyPointer>>&>(typelessPromise)
Chris@63 746 .then([](Response<AnyPointer>&& response) -> Response<Results> {
Chris@63 747 return Response<Results>(response.getAs<Results>(), kj::mv(response.hook));
Chris@63 748 });
Chris@63 749
Chris@63 750 // Wrap the typeless pipeline in a typed wrapper.
Chris@63 751 typename Results::Pipeline typedPipeline(
Chris@63 752 kj::mv(kj::implicitCast<AnyPointer::Pipeline&>(typelessPromise)));
Chris@63 753
Chris@63 754 return RemotePromise<Results>(kj::mv(typedPromise), kj::mv(typedPipeline));
Chris@63 755 }
Chris@63 756
Chris@63 757 inline Capability::Client::Client(kj::Own<ClientHook>&& hook): hook(kj::mv(hook)) {}
Chris@63 758 template <typename T, typename>
Chris@63 759 inline Capability::Client::Client(kj::Own<T>&& server)
Chris@63 760 : hook(makeLocalClient(kj::mv(server))) {}
Chris@63 761 template <typename T, typename>
Chris@63 762 inline Capability::Client::Client(kj::Promise<T>&& promise)
Chris@63 763 : hook(newLocalPromiseClient(promise.then([](T&& t) { return kj::mv(t.hook); }))) {}
Chris@63 764 inline Capability::Client::Client(Client& other): hook(other.hook->addRef()) {}
Chris@63 765 inline Capability::Client& Capability::Client::operator=(Client& other) {
Chris@63 766 hook = other.hook->addRef();
Chris@63 767 return *this;
Chris@63 768 }
Chris@63 769 template <typename T>
Chris@63 770 inline typename T::Client Capability::Client::castAs() {
Chris@63 771 return typename T::Client(hook->addRef());
Chris@63 772 }
Chris@63 773 inline kj::Promise<void> Capability::Client::whenResolved() {
Chris@63 774 return hook->whenResolved();
Chris@63 775 }
Chris@63 776 inline Request<AnyPointer, AnyPointer> Capability::Client::typelessRequest(
Chris@63 777 uint64_t interfaceId, uint16_t methodId,
Chris@63 778 kj::Maybe<MessageSize> sizeHint) {
Chris@63 779 return newCall<AnyPointer, AnyPointer>(interfaceId, methodId, sizeHint);
Chris@63 780 }
Chris@63 781 template <typename Params, typename Results>
Chris@63 782 inline Request<Params, Results> Capability::Client::newCall(
Chris@63 783 uint64_t interfaceId, uint16_t methodId, kj::Maybe<MessageSize> sizeHint) {
Chris@63 784 auto typeless = hook->newCall(interfaceId, methodId, sizeHint);
Chris@63 785 return Request<Params, Results>(typeless.template getAs<Params>(), kj::mv(typeless.hook));
Chris@63 786 }
Chris@63 787
Chris@63 788 template <typename Params, typename Results>
Chris@63 789 inline CallContext<Params, Results>::CallContext(CallContextHook& hook): hook(&hook) {}
Chris@63 790 template <typename Params, typename Results>
Chris@63 791 inline typename Params::Reader CallContext<Params, Results>::getParams() {
Chris@63 792 return hook->getParams().template getAs<Params>();
Chris@63 793 }
Chris@63 794 template <typename Params, typename Results>
Chris@63 795 inline void CallContext<Params, Results>::releaseParams() {
Chris@63 796 hook->releaseParams();
Chris@63 797 }
Chris@63 798 template <typename Params, typename Results>
Chris@63 799 inline typename Results::Builder CallContext<Params, Results>::getResults(
Chris@63 800 kj::Maybe<MessageSize> sizeHint) {
Chris@63 801 // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401
Chris@63 802 return hook->getResults(sizeHint).template getAs<Results>();
Chris@63 803 }
Chris@63 804 template <typename Params, typename Results>
Chris@63 805 inline typename Results::Builder CallContext<Params, Results>::initResults(
Chris@63 806 kj::Maybe<MessageSize> sizeHint) {
Chris@63 807 // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401
Chris@63 808 return hook->getResults(sizeHint).template initAs<Results>();
Chris@63 809 }
Chris@63 810 template <typename Params, typename Results>
Chris@63 811 inline void CallContext<Params, Results>::setResults(typename Results::Reader value) {
Chris@63 812 hook->getResults(value.totalSize()).template setAs<Results>(value);
Chris@63 813 }
Chris@63 814 template <typename Params, typename Results>
Chris@63 815 inline void CallContext<Params, Results>::adoptResults(Orphan<Results>&& value) {
Chris@63 816 hook->getResults(nullptr).adopt(kj::mv(value));
Chris@63 817 }
Chris@63 818 template <typename Params, typename Results>
Chris@63 819 inline Orphanage CallContext<Params, Results>::getResultsOrphanage(
Chris@63 820 kj::Maybe<MessageSize> sizeHint) {
Chris@63 821 return Orphanage::getForMessageContaining(hook->getResults(sizeHint));
Chris@63 822 }
Chris@63 823 template <typename Params, typename Results>
Chris@63 824 template <typename SubParams>
Chris@63 825 inline kj::Promise<void> CallContext<Params, Results>::tailCall(
Chris@63 826 Request<SubParams, Results>&& tailRequest) {
Chris@63 827 return hook->tailCall(kj::mv(tailRequest.hook));
Chris@63 828 }
Chris@63 829 template <typename Params, typename Results>
Chris@63 830 inline void CallContext<Params, Results>::allowCancellation() {
Chris@63 831 hook->allowCancellation();
Chris@63 832 }
Chris@63 833
Chris@63 834 template <typename Params, typename Results>
Chris@63 835 CallContext<Params, Results> Capability::Server::internalGetTypedContext(
Chris@63 836 CallContext<AnyPointer, AnyPointer> typeless) {
Chris@63 837 return CallContext<Params, Results>(*typeless.hook);
Chris@63 838 }
Chris@63 839
Chris@63 840 Capability::Client Capability::Server::thisCap() {
Chris@63 841 return Client(thisHook->addRef());
Chris@63 842 }
Chris@63 843
Chris@63 844 template <typename T>
Chris@63 845 T ReaderCapabilityTable::imbue(T reader) {
Chris@63 846 return T(_::PointerHelpers<FromReader<T>>::getInternalReader(reader).imbue(this));
Chris@63 847 }
Chris@63 848
Chris@63 849 template <typename T>
Chris@63 850 T BuilderCapabilityTable::imbue(T builder) {
Chris@63 851 return T(_::PointerHelpers<FromBuilder<T>>::getInternalBuilder(kj::mv(builder)).imbue(this));
Chris@63 852 }
Chris@63 853
Chris@63 854 template <typename T>
Chris@63 855 typename T::Client CapabilityServerSet<T>::add(kj::Own<typename T::Server>&& server) {
Chris@63 856 void* ptr = reinterpret_cast<void*>(server.get());
Chris@63 857 // Clang insists that `castAs` is a template-dependent member and therefore we need the
Chris@63 858 // `template` keyword here, but AFAICT this is wrong: addImpl() is not a template.
Chris@63 859 return addInternal(kj::mv(server), ptr).template castAs<T>();
Chris@63 860 }
Chris@63 861
Chris@63 862 template <typename T>
Chris@63 863 kj::Promise<kj::Maybe<typename T::Server&>> CapabilityServerSet<T>::getLocalServer(
Chris@63 864 typename T::Client& client) {
Chris@63 865 return getLocalServerInternal(client)
Chris@63 866 .then([](void* server) -> kj::Maybe<typename T::Server&> {
Chris@63 867 if (server == nullptr) {
Chris@63 868 return nullptr;
Chris@63 869 } else {
Chris@63 870 return *reinterpret_cast<typename T::Server*>(server);
Chris@63 871 }
Chris@63 872 });
Chris@63 873 }
Chris@63 874
Chris@63 875 template <typename T>
Chris@63 876 struct Orphanage::GetInnerReader<T, Kind::INTERFACE> {
Chris@63 877 static inline kj::Own<ClientHook> apply(typename T::Client t) {
Chris@63 878 return ClientHook::from(kj::mv(t));
Chris@63 879 }
Chris@63 880 };
Chris@63 881
Chris@63 882 } // namespace capnp
Chris@63 883
Chris@63 884 #endif // CAPNP_CAPABILITY_H_