annotate win64-msvc/include/capnp/dynamic.h @ 47:d93140aac40b

Current Capnp libs and headers from git
author Chris Cannam
date Thu, 20 Oct 2016 18:15:38 +0100
parents
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 // This file defines classes that can be used to manipulate messages based on schemas that are not
Chris@47 23 // known until runtime. This is also useful for writing generic code that uses schemas to handle
Chris@47 24 // arbitrary types in a generic way.
Chris@47 25 //
Chris@47 26 // Each of the classes defined here has a to() template method which converts an instance back to a
Chris@47 27 // native type. This method will throw an exception if the requested type does not match the
Chris@47 28 // schema. To convert native types to dynamic, use DynamicFactory.
Chris@47 29 //
Chris@47 30 // As always, underlying data is validated lazily, so you have to actually traverse the whole
Chris@47 31 // message if you want to validate all content.
Chris@47 32
Chris@47 33 #ifndef CAPNP_DYNAMIC_H_
Chris@47 34 #define CAPNP_DYNAMIC_H_
Chris@47 35
Chris@47 36 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@47 37 #pragma GCC system_header
Chris@47 38 #endif
Chris@47 39
Chris@47 40 #include "schema.h"
Chris@47 41 #include "layout.h"
Chris@47 42 #include "message.h"
Chris@47 43 #include "any.h"
Chris@47 44 #include "capability.h"
Chris@47 45
Chris@47 46 namespace capnp {
Chris@47 47
Chris@47 48 class MessageReader;
Chris@47 49 class MessageBuilder;
Chris@47 50
Chris@47 51 struct DynamicValue {
Chris@47 52 DynamicValue() = delete;
Chris@47 53
Chris@47 54 enum Type {
Chris@47 55 UNKNOWN,
Chris@47 56 // Means that the value has unknown type and content because it comes from a newer version of
Chris@47 57 // the schema, or from a newer version of Cap'n Proto that has new features that this version
Chris@47 58 // doesn't understand.
Chris@47 59
Chris@47 60 VOID,
Chris@47 61 BOOL,
Chris@47 62 INT,
Chris@47 63 UINT,
Chris@47 64 FLOAT,
Chris@47 65 TEXT,
Chris@47 66 DATA,
Chris@47 67 LIST,
Chris@47 68 ENUM,
Chris@47 69 STRUCT,
Chris@47 70 CAPABILITY,
Chris@47 71 ANY_POINTER
Chris@47 72 };
Chris@47 73
Chris@47 74 class Reader;
Chris@47 75 class Builder;
Chris@47 76 class Pipeline;
Chris@47 77 };
Chris@47 78 class DynamicEnum;
Chris@47 79 struct DynamicStruct {
Chris@47 80 DynamicStruct() = delete;
Chris@47 81 class Reader;
Chris@47 82 class Builder;
Chris@47 83 class Pipeline;
Chris@47 84 };
Chris@47 85 struct DynamicList {
Chris@47 86 DynamicList() = delete;
Chris@47 87 class Reader;
Chris@47 88 class Builder;
Chris@47 89 };
Chris@47 90 struct DynamicCapability {
Chris@47 91 DynamicCapability() = delete;
Chris@47 92 class Client;
Chris@47 93 class Server;
Chris@47 94 };
Chris@47 95 template <> class Orphan<DynamicValue>;
Chris@47 96
Chris@47 97 template <Kind k> struct DynamicTypeFor_;
Chris@47 98 template <> struct DynamicTypeFor_<Kind::ENUM> { typedef DynamicEnum Type; };
Chris@47 99 template <> struct DynamicTypeFor_<Kind::STRUCT> { typedef DynamicStruct Type; };
Chris@47 100 template <> struct DynamicTypeFor_<Kind::LIST> { typedef DynamicList Type; };
Chris@47 101 template <> struct DynamicTypeFor_<Kind::INTERFACE> { typedef DynamicCapability Type; };
Chris@47 102
Chris@47 103 template <typename T>
Chris@47 104 using DynamicTypeFor = typename DynamicTypeFor_<kind<T>()>::Type;
Chris@47 105
Chris@47 106 template <typename T>
Chris@47 107 ReaderFor<DynamicTypeFor<FromReader<T>>> toDynamic(T&& value);
Chris@47 108 template <typename T>
Chris@47 109 BuilderFor<DynamicTypeFor<FromBuilder<T>>> toDynamic(T&& value);
Chris@47 110 template <typename T>
Chris@47 111 DynamicTypeFor<TypeIfEnum<T>> toDynamic(T&& value);
Chris@47 112 template <typename T>
Chris@47 113 typename DynamicTypeFor<FromServer<T>>::Client toDynamic(kj::Own<T>&& value);
Chris@47 114
Chris@47 115 namespace _ { // private
Chris@47 116
Chris@47 117 template <> struct Kind_<DynamicValue > { static constexpr Kind kind = Kind::OTHER; };
Chris@47 118 template <> struct Kind_<DynamicEnum > { static constexpr Kind kind = Kind::OTHER; };
Chris@47 119 template <> struct Kind_<DynamicStruct > { static constexpr Kind kind = Kind::OTHER; };
Chris@47 120 template <> struct Kind_<DynamicList > { static constexpr Kind kind = Kind::OTHER; };
Chris@47 121 template <> struct Kind_<DynamicCapability> { static constexpr Kind kind = Kind::OTHER; };
Chris@47 122
Chris@47 123 } // namespace _ (private)
Chris@47 124
Chris@47 125 template <> inline constexpr Style style<DynamicValue >() { return Style::POINTER; }
Chris@47 126 template <> inline constexpr Style style<DynamicEnum >() { return Style::PRIMITIVE; }
Chris@47 127 template <> inline constexpr Style style<DynamicStruct >() { return Style::STRUCT; }
Chris@47 128 template <> inline constexpr Style style<DynamicList >() { return Style::POINTER; }
Chris@47 129 template <> inline constexpr Style style<DynamicCapability>() { return Style::CAPABILITY; }
Chris@47 130
Chris@47 131 // -------------------------------------------------------------------
Chris@47 132
Chris@47 133 class DynamicEnum {
Chris@47 134 public:
Chris@47 135 DynamicEnum() = default;
Chris@47 136 inline DynamicEnum(EnumSchema::Enumerant enumerant)
Chris@47 137 : schema(enumerant.getContainingEnum()), value(enumerant.getOrdinal()) {}
Chris@47 138 inline DynamicEnum(EnumSchema schema, uint16_t value)
Chris@47 139 : schema(schema), value(value) {}
Chris@47 140
Chris@47 141 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::ENUM>>
Chris@47 142 inline DynamicEnum(T&& value): DynamicEnum(toDynamic(value)) {}
Chris@47 143
Chris@47 144 template <typename T>
Chris@47 145 inline T as() const { return static_cast<T>(asImpl(typeId<T>())); }
Chris@47 146 // Cast to a native enum type.
Chris@47 147
Chris@47 148 inline EnumSchema getSchema() const { return schema; }
Chris@47 149
Chris@47 150 kj::Maybe<EnumSchema::Enumerant> getEnumerant() const;
Chris@47 151 // Get which enumerant this enum value represents. Returns nullptr if the numeric value does not
Chris@47 152 // correspond to any enumerant in the schema -- this can happen if the data was built using a
Chris@47 153 // newer schema that has more values defined.
Chris@47 154
Chris@47 155 inline uint16_t getRaw() const { return value; }
Chris@47 156 // Returns the raw underlying enum value.
Chris@47 157
Chris@47 158 private:
Chris@47 159 EnumSchema schema;
Chris@47 160 uint16_t value;
Chris@47 161
Chris@47 162 uint16_t asImpl(uint64_t requestedTypeId) const;
Chris@47 163
Chris@47 164 friend struct DynamicStruct;
Chris@47 165 friend struct DynamicList;
Chris@47 166 friend struct DynamicValue;
Chris@47 167 template <typename T>
Chris@47 168 friend DynamicTypeFor<TypeIfEnum<T>> toDynamic(T&& value);
Chris@47 169 };
Chris@47 170
Chris@47 171 // -------------------------------------------------------------------
Chris@47 172
Chris@47 173 class DynamicStruct::Reader {
Chris@47 174 public:
Chris@47 175 typedef DynamicStruct Reads;
Chris@47 176
Chris@47 177 Reader() = default;
Chris@47 178
Chris@47 179 template <typename T, typename = kj::EnableIf<kind<FromReader<T>>() == Kind::STRUCT>>
Chris@47 180 inline Reader(T&& value): Reader(toDynamic(value)) {}
Chris@47 181
Chris@47 182 inline MessageSize totalSize() const { return reader.totalSize().asPublic(); }
Chris@47 183
Chris@47 184 template <typename T>
Chris@47 185 typename T::Reader as() const;
Chris@47 186 // Convert the dynamic struct to its compiled-in type.
Chris@47 187
Chris@47 188 inline StructSchema getSchema() const { return schema; }
Chris@47 189
Chris@47 190 DynamicValue::Reader get(StructSchema::Field field) const;
Chris@47 191 // Read the given field value.
Chris@47 192
Chris@47 193 bool has(StructSchema::Field field) const;
Chris@47 194 // Tests whether the given field is set to its default value. For pointer values, this does
Chris@47 195 // not actually traverse the value comparing it with the default, but simply returns true if the
Chris@47 196 // pointer is non-null. For members of unions, has() returns false if the union member is not
Chris@47 197 // active, but does not necessarily return true if the member is active (depends on the field's
Chris@47 198 // value).
Chris@47 199
Chris@47 200 kj::Maybe<StructSchema::Field> which() const;
Chris@47 201 // If the struct contains an (unnamed) union, and the currently-active field within that union
Chris@47 202 // is known, this returns that field. Otherwise, it returns null. In other words, this returns
Chris@47 203 // null if there is no union present _or_ if the union's discriminant is set to an unrecognized
Chris@47 204 // value. This could happen in particular when receiving a message from a sender who has a
Chris@47 205 // newer version of the protocol and is using a field of the union that you don't know about yet.
Chris@47 206
Chris@47 207 DynamicValue::Reader get(kj::StringPtr name) const;
Chris@47 208 bool has(kj::StringPtr name) const;
Chris@47 209 // Shortcuts to access fields by name. These throw exceptions if no such field exists.
Chris@47 210
Chris@47 211 private:
Chris@47 212 StructSchema schema;
Chris@47 213 _::StructReader reader;
Chris@47 214
Chris@47 215 inline Reader(StructSchema schema, _::StructReader reader)
Chris@47 216 : schema(schema), reader(reader) {}
Chris@47 217
Chris@47 218 bool isSetInUnion(StructSchema::Field field) const;
Chris@47 219 void verifySetInUnion(StructSchema::Field field) const;
Chris@47 220 static DynamicValue::Reader getImpl(_::StructReader reader, StructSchema::Field field);
Chris@47 221
Chris@47 222 template <typename T, Kind K>
Chris@47 223 friend struct _::PointerHelpers;
Chris@47 224 friend class DynamicStruct::Builder;
Chris@47 225 friend struct DynamicList;
Chris@47 226 friend class MessageReader;
Chris@47 227 friend class MessageBuilder;
Chris@47 228 template <typename T, ::capnp::Kind k>
Chris@47 229 friend struct ::capnp::ToDynamic_;
Chris@47 230 friend kj::StringTree _::structString(
Chris@47 231 _::StructReader reader, const _::RawBrandedSchema& schema);
Chris@47 232 friend class Orphanage;
Chris@47 233 friend class Orphan<DynamicStruct>;
Chris@47 234 friend class Orphan<DynamicValue>;
Chris@47 235 friend class Orphan<AnyPointer>;
Chris@47 236 };
Chris@47 237
Chris@47 238 class DynamicStruct::Builder {
Chris@47 239 public:
Chris@47 240 typedef DynamicStruct Builds;
Chris@47 241
Chris@47 242 Builder() = default;
Chris@47 243 inline Builder(decltype(nullptr)) {}
Chris@47 244
Chris@47 245 template <typename T, typename = kj::EnableIf<kind<FromBuilder<T>>() == Kind::STRUCT>>
Chris@47 246 inline Builder(T&& value): Builder(toDynamic(value)) {}
Chris@47 247
Chris@47 248 inline MessageSize totalSize() const { return asReader().totalSize(); }
Chris@47 249
Chris@47 250 template <typename T>
Chris@47 251 typename T::Builder as();
Chris@47 252 // Cast to a particular struct type.
Chris@47 253
Chris@47 254 inline StructSchema getSchema() const { return schema; }
Chris@47 255
Chris@47 256 DynamicValue::Builder get(StructSchema::Field field);
Chris@47 257 // Read the given field value.
Chris@47 258
Chris@47 259 inline bool has(StructSchema::Field field) { return asReader().has(field); }
Chris@47 260 // Tests whether the given field is set to its default value. For pointer values, this does
Chris@47 261 // not actually traverse the value comparing it with the default, but simply returns true if the
Chris@47 262 // pointer is non-null. For members of unions, has() returns whether the field is currently
Chris@47 263 // active and the union as a whole is non-default -- so, the only time has() will return false
Chris@47 264 // for an active union field is if it is the default active field and it has its default value.
Chris@47 265
Chris@47 266 kj::Maybe<StructSchema::Field> which();
Chris@47 267 // If the struct contains an (unnamed) union, and the currently-active field within that union
Chris@47 268 // is known, this returns that field. Otherwise, it returns null. In other words, this returns
Chris@47 269 // null if there is no union present _or_ if the union's discriminant is set to an unrecognized
Chris@47 270 // value. This could happen in particular when receiving a message from a sender who has a
Chris@47 271 // newer version of the protocol and is using a field of the union that you don't know about yet.
Chris@47 272
Chris@47 273 void set(StructSchema::Field field, const DynamicValue::Reader& value);
Chris@47 274 // Set the given field value.
Chris@47 275
Chris@47 276 DynamicValue::Builder init(StructSchema::Field field);
Chris@47 277 DynamicValue::Builder init(StructSchema::Field field, uint size);
Chris@47 278 // Init a struct, list, or blob field.
Chris@47 279
Chris@47 280 void adopt(StructSchema::Field field, Orphan<DynamicValue>&& orphan);
Chris@47 281 Orphan<DynamicValue> disown(StructSchema::Field field);
Chris@47 282 // Adopt/disown. This works even for non-pointer fields: adopt() becomes equivalent to set()
Chris@47 283 // and disown() becomes like get() followed by clear().
Chris@47 284
Chris@47 285 void clear(StructSchema::Field field);
Chris@47 286 // Clear a field, setting it to its default value. For pointer fields, this actually makes the
Chris@47 287 // field null.
Chris@47 288
Chris@47 289 DynamicValue::Builder get(kj::StringPtr name);
Chris@47 290 bool has(kj::StringPtr name);
Chris@47 291 void set(kj::StringPtr name, const DynamicValue::Reader& value);
Chris@47 292 void set(kj::StringPtr name, std::initializer_list<DynamicValue::Reader> value);
Chris@47 293 DynamicValue::Builder init(kj::StringPtr name);
Chris@47 294 DynamicValue::Builder init(kj::StringPtr name, uint size);
Chris@47 295 void adopt(kj::StringPtr name, Orphan<DynamicValue>&& orphan);
Chris@47 296 Orphan<DynamicValue> disown(kj::StringPtr name);
Chris@47 297 void clear(kj::StringPtr name);
Chris@47 298 // Shortcuts to access fields by name. These throw exceptions if no such field exists.
Chris@47 299
Chris@47 300 Reader asReader() const;
Chris@47 301
Chris@47 302 private:
Chris@47 303 StructSchema schema;
Chris@47 304 _::StructBuilder builder;
Chris@47 305
Chris@47 306 inline Builder(StructSchema schema, _::StructBuilder builder)
Chris@47 307 : schema(schema), builder(builder) {}
Chris@47 308
Chris@47 309 bool isSetInUnion(StructSchema::Field field);
Chris@47 310 void verifySetInUnion(StructSchema::Field field);
Chris@47 311 void setInUnion(StructSchema::Field field);
Chris@47 312
Chris@47 313 template <typename T, Kind k>
Chris@47 314 friend struct _::PointerHelpers;
Chris@47 315 friend struct DynamicList;
Chris@47 316 friend class MessageReader;
Chris@47 317 friend class MessageBuilder;
Chris@47 318 template <typename T, ::capnp::Kind k>
Chris@47 319 friend struct ::capnp::ToDynamic_;
Chris@47 320 friend class Orphanage;
Chris@47 321 friend class Orphan<DynamicStruct>;
Chris@47 322 friend class Orphan<DynamicValue>;
Chris@47 323 friend class Orphan<AnyPointer>;
Chris@47 324 };
Chris@47 325
Chris@47 326 class DynamicStruct::Pipeline {
Chris@47 327 public:
Chris@47 328 typedef DynamicStruct Pipelines;
Chris@47 329
Chris@47 330 inline Pipeline(decltype(nullptr)): typeless(nullptr) {}
Chris@47 331
Chris@47 332 template <typename T>
Chris@47 333 typename T::Pipeline releaseAs();
Chris@47 334 // Convert the dynamic pipeline to its compiled-in type.
Chris@47 335
Chris@47 336 inline StructSchema getSchema() { return schema; }
Chris@47 337
Chris@47 338 DynamicValue::Pipeline get(StructSchema::Field field);
Chris@47 339 // Read the given field value.
Chris@47 340
Chris@47 341 DynamicValue::Pipeline get(kj::StringPtr name);
Chris@47 342 // Get by string name.
Chris@47 343
Chris@47 344 private:
Chris@47 345 StructSchema schema;
Chris@47 346 AnyPointer::Pipeline typeless;
Chris@47 347
Chris@47 348 inline explicit Pipeline(StructSchema schema, AnyPointer::Pipeline&& typeless)
Chris@47 349 : schema(schema), typeless(kj::mv(typeless)) {}
Chris@47 350
Chris@47 351 friend class Request<DynamicStruct, DynamicStruct>;
Chris@47 352 };
Chris@47 353
Chris@47 354 // -------------------------------------------------------------------
Chris@47 355
Chris@47 356 class DynamicList::Reader {
Chris@47 357 public:
Chris@47 358 typedef DynamicList Reads;
Chris@47 359
Chris@47 360 inline Reader(): reader(ElementSize::VOID) {}
Chris@47 361
Chris@47 362 template <typename T, typename = kj::EnableIf<kind<FromReader<T>>() == Kind::LIST>>
Chris@47 363 inline Reader(T&& value): Reader(toDynamic(value)) {}
Chris@47 364
Chris@47 365 template <typename T>
Chris@47 366 typename T::Reader as() const;
Chris@47 367 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data
Chris@47 368 // can't possibly represent the requested type.
Chris@47 369
Chris@47 370 inline ListSchema getSchema() const { return schema; }
Chris@47 371
Chris@47 372 inline uint size() const { return reader.size() / ELEMENTS; }
Chris@47 373 DynamicValue::Reader operator[](uint index) const;
Chris@47 374
Chris@47 375 typedef _::IndexingIterator<const Reader, DynamicValue::Reader> Iterator;
Chris@47 376 inline Iterator begin() const { return Iterator(this, 0); }
Chris@47 377 inline Iterator end() const { return Iterator(this, size()); }
Chris@47 378
Chris@47 379 private:
Chris@47 380 ListSchema schema;
Chris@47 381 _::ListReader reader;
Chris@47 382
Chris@47 383 Reader(ListSchema schema, _::ListReader reader): schema(schema), reader(reader) {}
Chris@47 384
Chris@47 385 template <typename T, Kind k>
Chris@47 386 friend struct _::PointerHelpers;
Chris@47 387 friend struct DynamicStruct;
Chris@47 388 friend class DynamicList::Builder;
Chris@47 389 template <typename T, ::capnp::Kind k>
Chris@47 390 friend struct ::capnp::ToDynamic_;
Chris@47 391 friend class Orphanage;
Chris@47 392 friend class Orphan<DynamicList>;
Chris@47 393 friend class Orphan<DynamicValue>;
Chris@47 394 friend class Orphan<AnyPointer>;
Chris@47 395 };
Chris@47 396
Chris@47 397 class DynamicList::Builder {
Chris@47 398 public:
Chris@47 399 typedef DynamicList Builds;
Chris@47 400
Chris@47 401 inline Builder(): builder(ElementSize::VOID) {}
Chris@47 402 inline Builder(decltype(nullptr)): builder(ElementSize::VOID) {}
Chris@47 403
Chris@47 404 template <typename T, typename = kj::EnableIf<kind<FromBuilder<T>>() == Kind::LIST>>
Chris@47 405 inline Builder(T&& value): Builder(toDynamic(value)) {}
Chris@47 406
Chris@47 407 template <typename T>
Chris@47 408 typename T::Builder as();
Chris@47 409 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data
Chris@47 410 // can't possibly represent the requested type.
Chris@47 411
Chris@47 412 inline ListSchema getSchema() const { return schema; }
Chris@47 413
Chris@47 414 inline uint size() const { return builder.size() / ELEMENTS; }
Chris@47 415 DynamicValue::Builder operator[](uint index);
Chris@47 416 void set(uint index, const DynamicValue::Reader& value);
Chris@47 417 DynamicValue::Builder init(uint index, uint size);
Chris@47 418 void adopt(uint index, Orphan<DynamicValue>&& orphan);
Chris@47 419 Orphan<DynamicValue> disown(uint index);
Chris@47 420
Chris@47 421 typedef _::IndexingIterator<Builder, DynamicStruct::Builder> Iterator;
Chris@47 422 inline Iterator begin() { return Iterator(this, 0); }
Chris@47 423 inline Iterator end() { return Iterator(this, size()); }
Chris@47 424
Chris@47 425 void copyFrom(std::initializer_list<DynamicValue::Reader> value);
Chris@47 426
Chris@47 427 Reader asReader() const;
Chris@47 428
Chris@47 429 private:
Chris@47 430 ListSchema schema;
Chris@47 431 _::ListBuilder builder;
Chris@47 432
Chris@47 433 Builder(ListSchema schema, _::ListBuilder builder): schema(schema), builder(builder) {}
Chris@47 434
Chris@47 435 template <typename T, Kind k>
Chris@47 436 friend struct _::PointerHelpers;
Chris@47 437 friend struct DynamicStruct;
Chris@47 438 template <typename T, ::capnp::Kind k>
Chris@47 439 friend struct ::capnp::ToDynamic_;
Chris@47 440 friend class Orphanage;
Chris@47 441 template <typename T, Kind k>
Chris@47 442 friend struct _::OrphanGetImpl;
Chris@47 443 friend class Orphan<DynamicList>;
Chris@47 444 friend class Orphan<DynamicValue>;
Chris@47 445 friend class Orphan<AnyPointer>;
Chris@47 446 };
Chris@47 447
Chris@47 448 // -------------------------------------------------------------------
Chris@47 449
Chris@47 450 class DynamicCapability::Client: public Capability::Client {
Chris@47 451 public:
Chris@47 452 typedef DynamicCapability Calls;
Chris@47 453 typedef DynamicCapability Reads;
Chris@47 454
Chris@47 455 Client() = default;
Chris@47 456
Chris@47 457 template <typename T, typename = kj::EnableIf<kind<FromClient<T>>() == Kind::INTERFACE>>
Chris@47 458 inline Client(T&& client);
Chris@47 459
Chris@47 460 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, DynamicCapability::Server*>()>>
Chris@47 461 inline Client(kj::Own<T>&& server);
Chris@47 462
Chris@47 463 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::INTERFACE>>
Chris@47 464 typename T::Client as();
Chris@47 465 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::INTERFACE>>
Chris@47 466 typename T::Client releaseAs();
Chris@47 467 // Convert to any client type.
Chris@47 468
Chris@47 469 Client upcast(InterfaceSchema requestedSchema);
Chris@47 470 // Upcast to a superclass. Throws an exception if `schema` is not a superclass.
Chris@47 471
Chris@47 472 inline InterfaceSchema getSchema() { return schema; }
Chris@47 473
Chris@47 474 Request<DynamicStruct, DynamicStruct> newRequest(
Chris@47 475 InterfaceSchema::Method method, kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@47 476 Request<DynamicStruct, DynamicStruct> newRequest(
Chris@47 477 kj::StringPtr methodName, kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@47 478
Chris@47 479 private:
Chris@47 480 InterfaceSchema schema;
Chris@47 481
Chris@47 482 Client(InterfaceSchema schema, kj::Own<ClientHook>&& hook)
Chris@47 483 : Capability::Client(kj::mv(hook)), schema(schema) {}
Chris@47 484
Chris@47 485 template <typename T>
Chris@47 486 inline Client(InterfaceSchema schema, kj::Own<T>&& server);
Chris@47 487
Chris@47 488 friend struct Capability;
Chris@47 489 friend struct DynamicStruct;
Chris@47 490 friend struct DynamicList;
Chris@47 491 friend struct DynamicValue;
Chris@47 492 friend class Orphan<DynamicCapability>;
Chris@47 493 friend class Orphan<DynamicValue>;
Chris@47 494 friend class Orphan<AnyPointer>;
Chris@47 495 template <typename T, Kind k>
Chris@47 496 friend struct _::PointerHelpers;
Chris@47 497 };
Chris@47 498
Chris@47 499 class DynamicCapability::Server: public Capability::Server {
Chris@47 500 public:
Chris@47 501 typedef DynamicCapability Serves;
Chris@47 502
Chris@47 503 Server(InterfaceSchema schema): schema(schema) {}
Chris@47 504
Chris@47 505 virtual kj::Promise<void> call(InterfaceSchema::Method method,
Chris@47 506 CallContext<DynamicStruct, DynamicStruct> context) = 0;
Chris@47 507
Chris@47 508 kj::Promise<void> dispatchCall(uint64_t interfaceId, uint16_t methodId,
Chris@47 509 CallContext<AnyPointer, AnyPointer> context) override final;
Chris@47 510
Chris@47 511 inline InterfaceSchema getSchema() const { return schema; }
Chris@47 512
Chris@47 513 private:
Chris@47 514 InterfaceSchema schema;
Chris@47 515 };
Chris@47 516
Chris@47 517 template <>
Chris@47 518 class Request<DynamicStruct, DynamicStruct>: public DynamicStruct::Builder {
Chris@47 519 // Specialization of `Request<T, U>` for DynamicStruct.
Chris@47 520
Chris@47 521 public:
Chris@47 522 inline Request(DynamicStruct::Builder builder, kj::Own<RequestHook>&& hook,
Chris@47 523 StructSchema resultSchema)
Chris@47 524 : DynamicStruct::Builder(builder), hook(kj::mv(hook)), resultSchema(resultSchema) {}
Chris@47 525
Chris@47 526 RemotePromise<DynamicStruct> send();
Chris@47 527 // Send the call and return a promise for the results.
Chris@47 528
Chris@47 529 private:
Chris@47 530 kj::Own<RequestHook> hook;
Chris@47 531 StructSchema resultSchema;
Chris@47 532
Chris@47 533 friend class Capability::Client;
Chris@47 534 friend struct DynamicCapability;
Chris@47 535 template <typename, typename>
Chris@47 536 friend class CallContext;
Chris@47 537 friend class RequestHook;
Chris@47 538 };
Chris@47 539
Chris@47 540 template <>
Chris@47 541 class CallContext<DynamicStruct, DynamicStruct>: public kj::DisallowConstCopy {
Chris@47 542 // Wrapper around CallContextHook with a specific return type.
Chris@47 543 //
Chris@47 544 // Methods of this class may only be called from within the server's event loop, not from other
Chris@47 545 // threads.
Chris@47 546
Chris@47 547 public:
Chris@47 548 explicit CallContext(CallContextHook& hook, StructSchema paramType, StructSchema resultType);
Chris@47 549
Chris@47 550 DynamicStruct::Reader getParams();
Chris@47 551 void releaseParams();
Chris@47 552 DynamicStruct::Builder getResults(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@47 553 DynamicStruct::Builder initResults(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@47 554 void setResults(DynamicStruct::Reader value);
Chris@47 555 void adoptResults(Orphan<DynamicStruct>&& value);
Chris@47 556 Orphanage getResultsOrphanage(kj::Maybe<MessageSize> sizeHint = nullptr);
Chris@47 557 template <typename SubParams>
Chris@47 558 kj::Promise<void> tailCall(Request<SubParams, DynamicStruct>&& tailRequest);
Chris@47 559 void allowCancellation();
Chris@47 560
Chris@47 561 private:
Chris@47 562 CallContextHook* hook;
Chris@47 563 StructSchema paramType;
Chris@47 564 StructSchema resultType;
Chris@47 565
Chris@47 566 friend class DynamicCapability::Server;
Chris@47 567 };
Chris@47 568
Chris@47 569 // -------------------------------------------------------------------
Chris@47 570
Chris@47 571 // Make sure ReaderFor<T> and BuilderFor<T> work for DynamicEnum, DynamicStruct, and
Chris@47 572 // DynamicList, so that we can define DynamicValue::as().
Chris@47 573
Chris@47 574 template <> struct ReaderFor_ <DynamicEnum, Kind::OTHER> { typedef DynamicEnum Type; };
Chris@47 575 template <> struct BuilderFor_<DynamicEnum, Kind::OTHER> { typedef DynamicEnum Type; };
Chris@47 576 template <> struct ReaderFor_ <DynamicStruct, Kind::OTHER> { typedef DynamicStruct::Reader Type; };
Chris@47 577 template <> struct BuilderFor_<DynamicStruct, Kind::OTHER> { typedef DynamicStruct::Builder Type; };
Chris@47 578 template <> struct ReaderFor_ <DynamicList, Kind::OTHER> { typedef DynamicList::Reader Type; };
Chris@47 579 template <> struct BuilderFor_<DynamicList, Kind::OTHER> { typedef DynamicList::Builder Type; };
Chris@47 580 template <> struct ReaderFor_ <DynamicCapability, Kind::OTHER> { typedef DynamicCapability::Client Type; };
Chris@47 581 template <> struct BuilderFor_<DynamicCapability, Kind::OTHER> { typedef DynamicCapability::Client Type; };
Chris@47 582 template <> struct PipelineFor_<DynamicCapability, Kind::OTHER> { typedef DynamicCapability::Client Type; };
Chris@47 583
Chris@47 584 class DynamicValue::Reader {
Chris@47 585 public:
Chris@47 586 typedef DynamicValue Reads;
Chris@47 587
Chris@47 588 inline Reader(decltype(nullptr) n = nullptr); // UNKNOWN
Chris@47 589 inline Reader(Void value);
Chris@47 590 inline Reader(bool value);
Chris@47 591 inline Reader(char value);
Chris@47 592 inline Reader(signed char value);
Chris@47 593 inline Reader(short value);
Chris@47 594 inline Reader(int value);
Chris@47 595 inline Reader(long value);
Chris@47 596 inline Reader(long long value);
Chris@47 597 inline Reader(unsigned char value);
Chris@47 598 inline Reader(unsigned short value);
Chris@47 599 inline Reader(unsigned int value);
Chris@47 600 inline Reader(unsigned long value);
Chris@47 601 inline Reader(unsigned long long value);
Chris@47 602 inline Reader(float value);
Chris@47 603 inline Reader(double value);
Chris@47 604 inline Reader(const char* value); // Text
Chris@47 605 inline Reader(const Text::Reader& value);
Chris@47 606 inline Reader(const Data::Reader& value);
Chris@47 607 inline Reader(const DynamicList::Reader& value);
Chris@47 608 inline Reader(DynamicEnum value);
Chris@47 609 inline Reader(const DynamicStruct::Reader& value);
Chris@47 610 inline Reader(const AnyPointer::Reader& value);
Chris@47 611 inline Reader(DynamicCapability::Client& value);
Chris@47 612 inline Reader(DynamicCapability::Client&& value);
Chris@47 613 template <typename T, typename = kj::EnableIf<kj::canConvert<T*, DynamicCapability::Server*>()>>
Chris@47 614 inline Reader(kj::Own<T>&& value);
Chris@47 615 Reader(ConstSchema constant);
Chris@47 616
Chris@47 617 template <typename T, typename = decltype(toDynamic(kj::instance<T>()))>
Chris@47 618 inline Reader(T&& value): Reader(toDynamic(kj::mv(value))) {}
Chris@47 619
Chris@47 620 Reader(const Reader& other);
Chris@47 621 Reader(Reader&& other) noexcept;
Chris@47 622 ~Reader() noexcept(false);
Chris@47 623 Reader& operator=(const Reader& other);
Chris@47 624 Reader& operator=(Reader&& other);
Chris@47 625 // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not
Chris@47 626 // trivially copyable.
Chris@47 627
Chris@47 628 template <typename T>
Chris@47 629 inline ReaderFor<T> as() const { return AsImpl<T>::apply(*this); }
Chris@47 630 // Use to interpret the value as some Cap'n Proto type. Allowed types are:
Chris@47 631 // - Void, bool, [u]int{8,16,32,64}_t, float, double, any enum: Returns the raw value.
Chris@47 632 // - Text, Data, AnyPointer, any struct type: Returns the corresponding Reader.
Chris@47 633 // - List<T> for any T listed above: Returns List<T>::Reader.
Chris@47 634 // - DynamicEnum: Returns the corresponding type.
Chris@47 635 // - DynamicStruct, DynamicList: Returns the corresponding Reader.
Chris@47 636 // - Any capability type, including DynamicCapability: Returns the corresponding Client.
Chris@47 637 // (TODO(perf): On GCC 4.8 / Clang 3.3, provide rvalue-qualified version that avoids
Chris@47 638 // refcounting.)
Chris@47 639 //
Chris@47 640 // DynamicValue allows various implicit conversions, mostly just to make the interface friendlier.
Chris@47 641 // - Any integer can be converted to any other integer type so long as the actual value is within
Chris@47 642 // the new type's range.
Chris@47 643 // - Floating-point types can be converted to integers as long as no information would be lost
Chris@47 644 // in the conversion.
Chris@47 645 // - Integers can be converted to floating points. This may lose information, but won't throw.
Chris@47 646 // - Float32/Float64 can be converted between each other. Converting Float64 -> Float32 may lose
Chris@47 647 // information, but won't throw.
Chris@47 648 // - Text can be converted to an enum, if the Text matches one of the enumerant names (but not
Chris@47 649 // vice-versa).
Chris@47 650 // - Capabilities can be upcast (cast to a supertype), but not downcast.
Chris@47 651 //
Chris@47 652 // Any other conversion attempt will throw an exception.
Chris@47 653
Chris@47 654 inline Type getType() const { return type; }
Chris@47 655 // Get the type of this value.
Chris@47 656
Chris@47 657 private:
Chris@47 658 Type type;
Chris@47 659
Chris@47 660 union {
Chris@47 661 Void voidValue;
Chris@47 662 bool boolValue;
Chris@47 663 int64_t intValue;
Chris@47 664 uint64_t uintValue;
Chris@47 665 double floatValue;
Chris@47 666 Text::Reader textValue;
Chris@47 667 Data::Reader dataValue;
Chris@47 668 DynamicList::Reader listValue;
Chris@47 669 DynamicEnum enumValue;
Chris@47 670 DynamicStruct::Reader structValue;
Chris@47 671 AnyPointer::Reader anyPointerValue;
Chris@47 672
Chris@47 673 mutable DynamicCapability::Client capabilityValue;
Chris@47 674 // Declared mutable because `Client`s normally cannot be const.
Chris@47 675
Chris@47 676 // Warning: Copy/move constructors assume all these types are trivially copyable except
Chris@47 677 // Capability.
Chris@47 678 };
Chris@47 679
Chris@47 680 template <typename T, Kind kind = kind<T>()> struct AsImpl;
Chris@47 681 // Implementation backing the as() method. Needs to be a struct to allow partial
Chris@47 682 // specialization. Has a method apply() which does the work.
Chris@47 683
Chris@47 684 friend class Orphanage; // to speed up newOrphanCopy(DynamicValue::Reader)
Chris@47 685 };
Chris@47 686
Chris@47 687 class DynamicValue::Builder {
Chris@47 688 public:
Chris@47 689 typedef DynamicValue Builds;
Chris@47 690
Chris@47 691 inline Builder(decltype(nullptr) n = nullptr); // UNKNOWN
Chris@47 692 inline Builder(Void value);
Chris@47 693 inline Builder(bool value);
Chris@47 694 inline Builder(char value);
Chris@47 695 inline Builder(signed char value);
Chris@47 696 inline Builder(short value);
Chris@47 697 inline Builder(int value);
Chris@47 698 inline Builder(long value);
Chris@47 699 inline Builder(long long value);
Chris@47 700 inline Builder(unsigned char value);
Chris@47 701 inline Builder(unsigned short value);
Chris@47 702 inline Builder(unsigned int value);
Chris@47 703 inline Builder(unsigned long value);
Chris@47 704 inline Builder(unsigned long long value);
Chris@47 705 inline Builder(float value);
Chris@47 706 inline Builder(double value);
Chris@47 707 inline Builder(Text::Builder value);
Chris@47 708 inline Builder(Data::Builder value);
Chris@47 709 inline Builder(DynamicList::Builder value);
Chris@47 710 inline Builder(DynamicEnum value);
Chris@47 711 inline Builder(DynamicStruct::Builder value);
Chris@47 712 inline Builder(AnyPointer::Builder value);
Chris@47 713 inline Builder(DynamicCapability::Client& value);
Chris@47 714 inline Builder(DynamicCapability::Client&& value);
Chris@47 715
Chris@47 716 template <typename T, typename = decltype(toDynamic(kj::instance<T>()))>
Chris@47 717 inline Builder(T value): Builder(toDynamic(value)) {}
Chris@47 718
Chris@47 719 Builder(Builder& other);
Chris@47 720 Builder(Builder&& other) noexcept;
Chris@47 721 ~Builder() noexcept(false);
Chris@47 722 Builder& operator=(Builder& other);
Chris@47 723 Builder& operator=(Builder&& other);
Chris@47 724 // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not
Chris@47 725 // trivially copyable.
Chris@47 726
Chris@47 727 template <typename T>
Chris@47 728 inline BuilderFor<T> as() { return AsImpl<T>::apply(*this); }
Chris@47 729 // See DynamicValue::Reader::as().
Chris@47 730
Chris@47 731 inline Type getType() { return type; }
Chris@47 732 // Get the type of this value.
Chris@47 733
Chris@47 734 Reader asReader() const;
Chris@47 735
Chris@47 736 private:
Chris@47 737 Type type;
Chris@47 738
Chris@47 739 union {
Chris@47 740 Void voidValue;
Chris@47 741 bool boolValue;
Chris@47 742 int64_t intValue;
Chris@47 743 uint64_t uintValue;
Chris@47 744 double floatValue;
Chris@47 745 Text::Builder textValue;
Chris@47 746 Data::Builder dataValue;
Chris@47 747 DynamicList::Builder listValue;
Chris@47 748 DynamicEnum enumValue;
Chris@47 749 DynamicStruct::Builder structValue;
Chris@47 750 AnyPointer::Builder anyPointerValue;
Chris@47 751
Chris@47 752 mutable DynamicCapability::Client capabilityValue;
Chris@47 753 // Declared mutable because `Client`s normally cannot be const.
Chris@47 754 };
Chris@47 755
Chris@47 756 template <typename T, Kind kind = kind<T>()> struct AsImpl;
Chris@47 757 // Implementation backing the as() method. Needs to be a struct to allow partial
Chris@47 758 // specialization. Has a method apply() which does the work.
Chris@47 759
Chris@47 760 friend class Orphan<DynamicValue>;
Chris@47 761 };
Chris@47 762
Chris@47 763 class DynamicValue::Pipeline {
Chris@47 764 public:
Chris@47 765 typedef DynamicValue Pipelines;
Chris@47 766
Chris@47 767 inline Pipeline(decltype(nullptr) n = nullptr);
Chris@47 768 inline Pipeline(DynamicStruct::Pipeline&& value);
Chris@47 769 inline Pipeline(DynamicCapability::Client&& value);
Chris@47 770
Chris@47 771 Pipeline(Pipeline&& other) noexcept;
Chris@47 772 Pipeline& operator=(Pipeline&& other);
Chris@47 773 ~Pipeline() noexcept(false);
Chris@47 774
Chris@47 775 template <typename T>
Chris@47 776 inline PipelineFor<T> releaseAs() { return AsImpl<T>::apply(*this); }
Chris@47 777
Chris@47 778 inline Type getType() { return type; }
Chris@47 779 // Get the type of this value.
Chris@47 780
Chris@47 781 private:
Chris@47 782 Type type;
Chris@47 783 union {
Chris@47 784 DynamicStruct::Pipeline structValue;
Chris@47 785 DynamicCapability::Client capabilityValue;
Chris@47 786 };
Chris@47 787
Chris@47 788 template <typename T, Kind kind = kind<T>()> struct AsImpl;
Chris@47 789 // Implementation backing the releaseAs() method. Needs to be a struct to allow partial
Chris@47 790 // specialization. Has a method apply() which does the work.
Chris@47 791 };
Chris@47 792
Chris@47 793 kj::StringTree KJ_STRINGIFY(const DynamicValue::Reader& value);
Chris@47 794 kj::StringTree KJ_STRINGIFY(const DynamicValue::Builder& value);
Chris@47 795 kj::StringTree KJ_STRINGIFY(DynamicEnum value);
Chris@47 796 kj::StringTree KJ_STRINGIFY(const DynamicStruct::Reader& value);
Chris@47 797 kj::StringTree KJ_STRINGIFY(const DynamicStruct::Builder& value);
Chris@47 798 kj::StringTree KJ_STRINGIFY(const DynamicList::Reader& value);
Chris@47 799 kj::StringTree KJ_STRINGIFY(const DynamicList::Builder& value);
Chris@47 800
Chris@47 801 // -------------------------------------------------------------------
Chris@47 802 // Orphan <-> Dynamic glue
Chris@47 803
Chris@47 804 template <>
Chris@47 805 class Orphan<DynamicStruct> {
Chris@47 806 public:
Chris@47 807 Orphan() = default;
Chris@47 808 KJ_DISALLOW_COPY(Orphan);
Chris@47 809 Orphan(Orphan&&) = default;
Chris@47 810 Orphan& operator=(Orphan&&) = default;
Chris@47 811
Chris@47 812 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::STRUCT>>
Chris@47 813 inline Orphan(Orphan<T>&& other): schema(Schema::from<T>()), builder(kj::mv(other.builder)) {}
Chris@47 814
Chris@47 815 DynamicStruct::Builder get();
Chris@47 816 DynamicStruct::Reader getReader() const;
Chris@47 817
Chris@47 818 template <typename T>
Chris@47 819 Orphan<T> releaseAs();
Chris@47 820 // Like DynamicStruct::Builder::as(), but coerces the Orphan type. Since Orphans are move-only,
Chris@47 821 // the original Orphan<DynamicStruct> is no longer valid after this call; ownership is
Chris@47 822 // transferred to the returned Orphan<T>.
Chris@47 823
Chris@47 824 inline bool operator==(decltype(nullptr)) const { return builder == nullptr; }
Chris@47 825 inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; }
Chris@47 826
Chris@47 827 private:
Chris@47 828 StructSchema schema;
Chris@47 829 _::OrphanBuilder builder;
Chris@47 830
Chris@47 831 inline Orphan(StructSchema schema, _::OrphanBuilder&& builder)
Chris@47 832 : schema(schema), builder(kj::mv(builder)) {}
Chris@47 833
Chris@47 834 template <typename, Kind>
Chris@47 835 friend struct _::PointerHelpers;
Chris@47 836 friend struct DynamicList;
Chris@47 837 friend class Orphanage;
Chris@47 838 friend class Orphan<DynamicValue>;
Chris@47 839 friend class Orphan<AnyPointer>;
Chris@47 840 friend class MessageBuilder;
Chris@47 841 };
Chris@47 842
Chris@47 843 template <>
Chris@47 844 class Orphan<DynamicList> {
Chris@47 845 public:
Chris@47 846 Orphan() = default;
Chris@47 847 KJ_DISALLOW_COPY(Orphan);
Chris@47 848 Orphan(Orphan&&) = default;
Chris@47 849 Orphan& operator=(Orphan&&) = default;
Chris@47 850
Chris@47 851 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::LIST>>
Chris@47 852 inline Orphan(Orphan<T>&& other): schema(Schema::from<T>()), builder(kj::mv(other.builder)) {}
Chris@47 853
Chris@47 854 DynamicList::Builder get();
Chris@47 855 DynamicList::Reader getReader() const;
Chris@47 856
Chris@47 857 template <typename T>
Chris@47 858 Orphan<T> releaseAs();
Chris@47 859 // Like DynamicList::Builder::as(), but coerces the Orphan type. Since Orphans are move-only,
Chris@47 860 // the original Orphan<DynamicStruct> is no longer valid after this call; ownership is
Chris@47 861 // transferred to the returned Orphan<T>.
Chris@47 862
Chris@47 863 // TODO(someday): Support truncate().
Chris@47 864
Chris@47 865 inline bool operator==(decltype(nullptr)) const { return builder == nullptr; }
Chris@47 866 inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; }
Chris@47 867
Chris@47 868 private:
Chris@47 869 ListSchema schema;
Chris@47 870 _::OrphanBuilder builder;
Chris@47 871
Chris@47 872 inline Orphan(ListSchema schema, _::OrphanBuilder&& builder)
Chris@47 873 : schema(schema), builder(kj::mv(builder)) {}
Chris@47 874
Chris@47 875 template <typename, Kind>
Chris@47 876 friend struct _::PointerHelpers;
Chris@47 877 friend struct DynamicList;
Chris@47 878 friend class Orphanage;
Chris@47 879 friend class Orphan<DynamicValue>;
Chris@47 880 friend class Orphan<AnyPointer>;
Chris@47 881 };
Chris@47 882
Chris@47 883 template <>
Chris@47 884 class Orphan<DynamicCapability> {
Chris@47 885 public:
Chris@47 886 Orphan() = default;
Chris@47 887 KJ_DISALLOW_COPY(Orphan);
Chris@47 888 Orphan(Orphan&&) = default;
Chris@47 889 Orphan& operator=(Orphan&&) = default;
Chris@47 890
Chris@47 891 template <typename T, typename = kj::EnableIf<kind<T>() == Kind::INTERFACE>>
Chris@47 892 inline Orphan(Orphan<T>&& other): schema(Schema::from<T>()), builder(kj::mv(other.builder)) {}
Chris@47 893
Chris@47 894 DynamicCapability::Client get();
Chris@47 895 DynamicCapability::Client getReader() const;
Chris@47 896
Chris@47 897 template <typename T>
Chris@47 898 Orphan<T> releaseAs();
Chris@47 899 // Like DynamicCapability::Client::as(), but coerces the Orphan type. Since Orphans are move-only,
Chris@47 900 // the original Orphan<DynamicCapability> is no longer valid after this call; ownership is
Chris@47 901 // transferred to the returned Orphan<T>.
Chris@47 902
Chris@47 903 inline bool operator==(decltype(nullptr)) const { return builder == nullptr; }
Chris@47 904 inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; }
Chris@47 905
Chris@47 906 private:
Chris@47 907 InterfaceSchema schema;
Chris@47 908 _::OrphanBuilder builder;
Chris@47 909
Chris@47 910 inline Orphan(InterfaceSchema schema, _::OrphanBuilder&& builder)
Chris@47 911 : schema(schema), builder(kj::mv(builder)) {}
Chris@47 912
Chris@47 913 template <typename, Kind>
Chris@47 914 friend struct _::PointerHelpers;
Chris@47 915 friend struct DynamicList;
Chris@47 916 friend class Orphanage;
Chris@47 917 friend class Orphan<DynamicValue>;
Chris@47 918 friend class Orphan<AnyPointer>;
Chris@47 919 };
Chris@47 920
Chris@47 921 template <>
Chris@47 922 class Orphan<DynamicValue> {
Chris@47 923 public:
Chris@47 924 inline Orphan(decltype(nullptr) n = nullptr): type(DynamicValue::UNKNOWN) {}
Chris@47 925 inline Orphan(Void value);
Chris@47 926 inline Orphan(bool value);
Chris@47 927 inline Orphan(char value);
Chris@47 928 inline Orphan(signed char value);
Chris@47 929 inline Orphan(short value);
Chris@47 930 inline Orphan(int value);
Chris@47 931 inline Orphan(long value);
Chris@47 932 inline Orphan(long long value);
Chris@47 933 inline Orphan(unsigned char value);
Chris@47 934 inline Orphan(unsigned short value);
Chris@47 935 inline Orphan(unsigned int value);
Chris@47 936 inline Orphan(unsigned long value);
Chris@47 937 inline Orphan(unsigned long long value);
Chris@47 938 inline Orphan(float value);
Chris@47 939 inline Orphan(double value);
Chris@47 940 inline Orphan(DynamicEnum value);
Chris@47 941 Orphan(Orphan&&) = default;
Chris@47 942 template <typename T>
Chris@47 943 Orphan(Orphan<T>&&);
Chris@47 944 Orphan(Orphan<AnyPointer>&&);
Chris@47 945 Orphan(void*) = delete; // So Orphan(bool) doesn't accept pointers.
Chris@47 946 KJ_DISALLOW_COPY(Orphan);
Chris@47 947
Chris@47 948 Orphan& operator=(Orphan&&) = default;
Chris@47 949
Chris@47 950 inline DynamicValue::Type getType() { return type; }
Chris@47 951
Chris@47 952 DynamicValue::Builder get();
Chris@47 953 DynamicValue::Reader getReader() const;
Chris@47 954
Chris@47 955 template <typename T>
Chris@47 956 Orphan<T> releaseAs();
Chris@47 957 // Like DynamicValue::Builder::as(), but coerces the Orphan type. Since Orphans are move-only,
Chris@47 958 // the original Orphan<DynamicStruct> is no longer valid after this call; ownership is
Chris@47 959 // transferred to the returned Orphan<T>.
Chris@47 960
Chris@47 961 private:
Chris@47 962 DynamicValue::Type type;
Chris@47 963 union {
Chris@47 964 Void voidValue;
Chris@47 965 bool boolValue;
Chris@47 966 int64_t intValue;
Chris@47 967 uint64_t uintValue;
Chris@47 968 double floatValue;
Chris@47 969 DynamicEnum enumValue;
Chris@47 970 StructSchema structSchema;
Chris@47 971 ListSchema listSchema;
Chris@47 972 InterfaceSchema interfaceSchema;
Chris@47 973 };
Chris@47 974
Chris@47 975 _::OrphanBuilder builder;
Chris@47 976 // Only used if `type` is a pointer type.
Chris@47 977
Chris@47 978 Orphan(DynamicValue::Builder value, _::OrphanBuilder&& builder);
Chris@47 979 Orphan(DynamicValue::Type type, _::OrphanBuilder&& builder)
Chris@47 980 : type(type), builder(kj::mv(builder)) {}
Chris@47 981 Orphan(StructSchema structSchema, _::OrphanBuilder&& builder)
Chris@47 982 : type(DynamicValue::STRUCT), structSchema(structSchema), builder(kj::mv(builder)) {}
Chris@47 983 Orphan(ListSchema listSchema, _::OrphanBuilder&& builder)
Chris@47 984 : type(DynamicValue::LIST), listSchema(listSchema), builder(kj::mv(builder)) {}
Chris@47 985
Chris@47 986 template <typename, Kind>
Chris@47 987 friend struct _::PointerHelpers;
Chris@47 988 friend struct DynamicStruct;
Chris@47 989 friend struct DynamicList;
Chris@47 990 friend struct AnyPointer;
Chris@47 991 friend class Orphanage;
Chris@47 992 };
Chris@47 993
Chris@47 994 template <typename T>
Chris@47 995 inline Orphan<DynamicValue>::Orphan(Orphan<T>&& other)
Chris@47 996 : Orphan(other.get(), kj::mv(other.builder)) {}
Chris@47 997
Chris@47 998 inline Orphan<DynamicValue>::Orphan(Orphan<AnyPointer>&& other)
Chris@47 999 : type(DynamicValue::ANY_POINTER), builder(kj::mv(other.builder)) {}
Chris@47 1000
Chris@47 1001 template <typename T>
Chris@47 1002 Orphan<T> Orphan<DynamicStruct>::releaseAs() {
Chris@47 1003 get().as<T>(); // type check
Chris@47 1004 return Orphan<T>(kj::mv(builder));
Chris@47 1005 }
Chris@47 1006
Chris@47 1007 template <typename T>
Chris@47 1008 Orphan<T> Orphan<DynamicList>::releaseAs() {
Chris@47 1009 get().as<T>(); // type check
Chris@47 1010 return Orphan<T>(kj::mv(builder));
Chris@47 1011 }
Chris@47 1012
Chris@47 1013 template <typename T>
Chris@47 1014 Orphan<T> Orphan<DynamicCapability>::releaseAs() {
Chris@47 1015 get().as<T>(); // type check
Chris@47 1016 return Orphan<T>(kj::mv(builder));
Chris@47 1017 }
Chris@47 1018
Chris@47 1019 template <typename T>
Chris@47 1020 Orphan<T> Orphan<DynamicValue>::releaseAs() {
Chris@47 1021 get().as<T>(); // type check
Chris@47 1022 type = DynamicValue::UNKNOWN;
Chris@47 1023 return Orphan<T>(kj::mv(builder));
Chris@47 1024 }
Chris@47 1025
Chris@47 1026 template <>
Chris@47 1027 Orphan<AnyPointer> Orphan<DynamicValue>::releaseAs<AnyPointer>();
Chris@47 1028 template <>
Chris@47 1029 Orphan<DynamicStruct> Orphan<DynamicValue>::releaseAs<DynamicStruct>();
Chris@47 1030 template <>
Chris@47 1031 Orphan<DynamicList> Orphan<DynamicValue>::releaseAs<DynamicList>();
Chris@47 1032 template <>
Chris@47 1033 Orphan<DynamicCapability> Orphan<DynamicValue>::releaseAs<DynamicCapability>();
Chris@47 1034
Chris@47 1035 template <>
Chris@47 1036 struct Orphanage::GetInnerBuilder<DynamicStruct, Kind::OTHER> {
Chris@47 1037 static inline _::StructBuilder apply(DynamicStruct::Builder& t) {
Chris@47 1038 return t.builder;
Chris@47 1039 }
Chris@47 1040 };
Chris@47 1041
Chris@47 1042 template <>
Chris@47 1043 struct Orphanage::GetInnerBuilder<DynamicList, Kind::OTHER> {
Chris@47 1044 static inline _::ListBuilder apply(DynamicList::Builder& t) {
Chris@47 1045 return t.builder;
Chris@47 1046 }
Chris@47 1047 };
Chris@47 1048
Chris@47 1049 template <>
Chris@47 1050 inline Orphan<DynamicStruct> Orphanage::newOrphanCopy<DynamicStruct::Reader>(
Chris@47 1051 DynamicStruct::Reader copyFrom) const {
Chris@47 1052 return Orphan<DynamicStruct>(
Chris@47 1053 copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.reader));
Chris@47 1054 }
Chris@47 1055
Chris@47 1056 template <>
Chris@47 1057 inline Orphan<DynamicList> Orphanage::newOrphanCopy<DynamicList::Reader>(
Chris@47 1058 DynamicList::Reader copyFrom) const {
Chris@47 1059 return Orphan<DynamicList>(copyFrom.getSchema(),
Chris@47 1060 _::OrphanBuilder::copy(arena, capTable, copyFrom.reader));
Chris@47 1061 }
Chris@47 1062
Chris@47 1063 template <>
Chris@47 1064 inline Orphan<DynamicCapability> Orphanage::newOrphanCopy<DynamicCapability::Client>(
Chris@47 1065 DynamicCapability::Client copyFrom) const {
Chris@47 1066 return Orphan<DynamicCapability>(
Chris@47 1067 copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.hook->addRef()));
Chris@47 1068 }
Chris@47 1069
Chris@47 1070 template <>
Chris@47 1071 Orphan<DynamicValue> Orphanage::newOrphanCopy<DynamicValue::Reader>(
Chris@47 1072 DynamicValue::Reader copyFrom) const;
Chris@47 1073
Chris@47 1074 namespace _ { // private
Chris@47 1075
Chris@47 1076 template <>
Chris@47 1077 struct PointerHelpers<DynamicStruct, Kind::OTHER> {
Chris@47 1078 // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for
Chris@47 1079 // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
Chris@47 1080 // don't want people to accidentally be able to provide their own default value.
Chris@47 1081 static DynamicStruct::Reader getDynamic(PointerReader reader, StructSchema schema);
Chris@47 1082 static DynamicStruct::Builder getDynamic(PointerBuilder builder, StructSchema schema);
Chris@47 1083 static void set(PointerBuilder builder, const DynamicStruct::Reader& value);
Chris@47 1084 static DynamicStruct::Builder init(PointerBuilder builder, StructSchema schema);
Chris@47 1085 static inline void adopt(PointerBuilder builder, Orphan<DynamicStruct>&& value) {
Chris@47 1086 builder.adopt(kj::mv(value.builder));
Chris@47 1087 }
Chris@47 1088 static inline Orphan<DynamicStruct> disown(PointerBuilder builder, StructSchema schema) {
Chris@47 1089 return Orphan<DynamicStruct>(schema, builder.disown());
Chris@47 1090 }
Chris@47 1091 };
Chris@47 1092
Chris@47 1093 template <>
Chris@47 1094 struct PointerHelpers<DynamicList, Kind::OTHER> {
Chris@47 1095 // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for
Chris@47 1096 // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
Chris@47 1097 // don't want people to accidentally be able to provide their own default value.
Chris@47 1098 static DynamicList::Reader getDynamic(PointerReader reader, ListSchema schema);
Chris@47 1099 static DynamicList::Builder getDynamic(PointerBuilder builder, ListSchema schema);
Chris@47 1100 static void set(PointerBuilder builder, const DynamicList::Reader& value);
Chris@47 1101 static DynamicList::Builder init(PointerBuilder builder, ListSchema schema, uint size);
Chris@47 1102 static inline void adopt(PointerBuilder builder, Orphan<DynamicList>&& value) {
Chris@47 1103 builder.adopt(kj::mv(value.builder));
Chris@47 1104 }
Chris@47 1105 static inline Orphan<DynamicList> disown(PointerBuilder builder, ListSchema schema) {
Chris@47 1106 return Orphan<DynamicList>(schema, builder.disown());
Chris@47 1107 }
Chris@47 1108 };
Chris@47 1109
Chris@47 1110 template <>
Chris@47 1111 struct PointerHelpers<DynamicCapability, Kind::OTHER> {
Chris@47 1112 // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for
Chris@47 1113 // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
Chris@47 1114 // don't want people to accidentally be able to provide their own default value.
Chris@47 1115 static DynamicCapability::Client getDynamic(PointerReader reader, InterfaceSchema schema);
Chris@47 1116 static DynamicCapability::Client getDynamic(PointerBuilder builder, InterfaceSchema schema);
Chris@47 1117 static void set(PointerBuilder builder, DynamicCapability::Client& value);
Chris@47 1118 static void set(PointerBuilder builder, DynamicCapability::Client&& value);
Chris@47 1119 static inline void adopt(PointerBuilder builder, Orphan<DynamicCapability>&& value) {
Chris@47 1120 builder.adopt(kj::mv(value.builder));
Chris@47 1121 }
Chris@47 1122 static inline Orphan<DynamicCapability> disown(PointerBuilder builder, InterfaceSchema schema) {
Chris@47 1123 return Orphan<DynamicCapability>(schema, builder.disown());
Chris@47 1124 }
Chris@47 1125 };
Chris@47 1126
Chris@47 1127 } // namespace _ (private)
Chris@47 1128
Chris@47 1129 template <typename T>
Chris@47 1130 inline ReaderFor<T> AnyPointer::Reader::getAs(StructSchema schema) const {
Chris@47 1131 return _::PointerHelpers<T>::getDynamic(reader, schema);
Chris@47 1132 }
Chris@47 1133 template <typename T>
Chris@47 1134 inline ReaderFor<T> AnyPointer::Reader::getAs(ListSchema schema) const {
Chris@47 1135 return _::PointerHelpers<T>::getDynamic(reader, schema);
Chris@47 1136 }
Chris@47 1137 template <typename T>
Chris@47 1138 inline ReaderFor<T> AnyPointer::Reader::getAs(InterfaceSchema schema) const {
Chris@47 1139 return _::PointerHelpers<T>::getDynamic(reader, schema);
Chris@47 1140 }
Chris@47 1141 template <typename T>
Chris@47 1142 inline BuilderFor<T> AnyPointer::Builder::getAs(StructSchema schema) {
Chris@47 1143 return _::PointerHelpers<T>::getDynamic(builder, schema);
Chris@47 1144 }
Chris@47 1145 template <typename T>
Chris@47 1146 inline BuilderFor<T> AnyPointer::Builder::getAs(ListSchema schema) {
Chris@47 1147 return _::PointerHelpers<T>::getDynamic(builder, schema);
Chris@47 1148 }
Chris@47 1149 template <typename T>
Chris@47 1150 inline BuilderFor<T> AnyPointer::Builder::getAs(InterfaceSchema schema) {
Chris@47 1151 return _::PointerHelpers<T>::getDynamic(builder, schema);
Chris@47 1152 }
Chris@47 1153 template <typename T>
Chris@47 1154 inline BuilderFor<T> AnyPointer::Builder::initAs(StructSchema schema) {
Chris@47 1155 return _::PointerHelpers<T>::init(builder, schema);
Chris@47 1156 }
Chris@47 1157 template <typename T>
Chris@47 1158 inline BuilderFor<T> AnyPointer::Builder::initAs(ListSchema schema, uint elementCount) {
Chris@47 1159 return _::PointerHelpers<T>::init(builder, schema, elementCount);
Chris@47 1160 }
Chris@47 1161 template <>
Chris@47 1162 inline void AnyPointer::Builder::setAs<DynamicStruct>(DynamicStruct::Reader value) {
Chris@47 1163 return _::PointerHelpers<DynamicStruct>::set(builder, value);
Chris@47 1164 }
Chris@47 1165 template <>
Chris@47 1166 inline void AnyPointer::Builder::setAs<DynamicList>(DynamicList::Reader value) {
Chris@47 1167 return _::PointerHelpers<DynamicList>::set(builder, value);
Chris@47 1168 }
Chris@47 1169 template <>
Chris@47 1170 inline void AnyPointer::Builder::setAs<DynamicCapability>(DynamicCapability::Client value) {
Chris@47 1171 return _::PointerHelpers<DynamicCapability>::set(builder, kj::mv(value));
Chris@47 1172 }
Chris@47 1173 template <>
Chris@47 1174 void AnyPointer::Builder::adopt<DynamicValue>(Orphan<DynamicValue>&& orphan);
Chris@47 1175 template <typename T>
Chris@47 1176 inline Orphan<T> AnyPointer::Builder::disownAs(StructSchema schema) {
Chris@47 1177 return _::PointerHelpers<T>::disown(builder, schema);
Chris@47 1178 }
Chris@47 1179 template <typename T>
Chris@47 1180 inline Orphan<T> AnyPointer::Builder::disownAs(ListSchema schema) {
Chris@47 1181 return _::PointerHelpers<T>::disown(builder, schema);
Chris@47 1182 }
Chris@47 1183 template <typename T>
Chris@47 1184 inline Orphan<T> AnyPointer::Builder::disownAs(InterfaceSchema schema) {
Chris@47 1185 return _::PointerHelpers<T>::disown(builder, schema);
Chris@47 1186 }
Chris@47 1187
Chris@47 1188 template <>
Chris@47 1189 DynamicStruct::Builder Orphan<AnyPointer>::getAs<DynamicStruct>(StructSchema schema);
Chris@47 1190 template <>
Chris@47 1191 DynamicList::Builder Orphan<AnyPointer>::getAs<DynamicList>(ListSchema schema);
Chris@47 1192 template <>
Chris@47 1193 DynamicCapability::Client Orphan<AnyPointer>::getAs<DynamicCapability>(InterfaceSchema schema);
Chris@47 1194 template <>
Chris@47 1195 DynamicStruct::Reader Orphan<AnyPointer>::getAsReader<DynamicStruct>(StructSchema schema) const;
Chris@47 1196 template <>
Chris@47 1197 DynamicList::Reader Orphan<AnyPointer>::getAsReader<DynamicList>(ListSchema schema) const;
Chris@47 1198 template <>
Chris@47 1199 DynamicCapability::Client Orphan<AnyPointer>::getAsReader<DynamicCapability>(
Chris@47 1200 InterfaceSchema schema) const;
Chris@47 1201 template <>
Chris@47 1202 Orphan<DynamicStruct> Orphan<AnyPointer>::releaseAs<DynamicStruct>(StructSchema schema);
Chris@47 1203 template <>
Chris@47 1204 Orphan<DynamicList> Orphan<AnyPointer>::releaseAs<DynamicList>(ListSchema schema);
Chris@47 1205 template <>
Chris@47 1206 Orphan<DynamicCapability> Orphan<AnyPointer>::releaseAs<DynamicCapability>(
Chris@47 1207 InterfaceSchema schema);
Chris@47 1208
Chris@47 1209 // =======================================================================================
Chris@47 1210 // Inline implementation details.
Chris@47 1211
Chris@47 1212 template <typename T>
Chris@47 1213 struct ToDynamic_<T, Kind::STRUCT> {
Chris@47 1214 static inline DynamicStruct::Reader apply(const typename T::Reader& value) {
Chris@47 1215 return DynamicStruct::Reader(Schema::from<T>(), value._reader);
Chris@47 1216 }
Chris@47 1217 static inline DynamicStruct::Builder apply(typename T::Builder& value) {
Chris@47 1218 return DynamicStruct::Builder(Schema::from<T>(), value._builder);
Chris@47 1219 }
Chris@47 1220 };
Chris@47 1221
Chris@47 1222 template <typename T>
Chris@47 1223 struct ToDynamic_<T, Kind::LIST> {
Chris@47 1224 static inline DynamicList::Reader apply(const typename T::Reader& value) {
Chris@47 1225 return DynamicList::Reader(Schema::from<T>(), value.reader);
Chris@47 1226 }
Chris@47 1227 static inline DynamicList::Builder apply(typename T::Builder& value) {
Chris@47 1228 return DynamicList::Builder(Schema::from<T>(), value.builder);
Chris@47 1229 }
Chris@47 1230 };
Chris@47 1231
Chris@47 1232 template <typename T>
Chris@47 1233 struct ToDynamic_<T, Kind::INTERFACE> {
Chris@47 1234 static inline DynamicCapability::Client apply(typename T::Client value) {
Chris@47 1235 return DynamicCapability::Client(kj::mv(value));
Chris@47 1236 }
Chris@47 1237 static inline DynamicCapability::Client apply(typename T::Client&& value) {
Chris@47 1238 return DynamicCapability::Client(kj::mv(value));
Chris@47 1239 }
Chris@47 1240 };
Chris@47 1241
Chris@47 1242 template <typename T>
Chris@47 1243 ReaderFor<DynamicTypeFor<FromReader<T>>> toDynamic(T&& value) {
Chris@47 1244 return ToDynamic_<FromReader<T>>::apply(value);
Chris@47 1245 }
Chris@47 1246 template <typename T>
Chris@47 1247 BuilderFor<DynamicTypeFor<FromBuilder<T>>> toDynamic(T&& value) {
Chris@47 1248 return ToDynamic_<FromBuilder<T>>::apply(value);
Chris@47 1249 }
Chris@47 1250 template <typename T>
Chris@47 1251 DynamicTypeFor<TypeIfEnum<T>> toDynamic(T&& value) {
Chris@47 1252 return DynamicEnum(Schema::from<kj::Decay<T>>(), static_cast<uint16_t>(value));
Chris@47 1253 }
Chris@47 1254 template <typename T>
Chris@47 1255 typename DynamicTypeFor<FromServer<T>>::Client toDynamic(kj::Own<T>&& value) {
Chris@47 1256 return typename FromServer<T>::Client(kj::mv(value));
Chris@47 1257 }
Chris@47 1258
Chris@47 1259 inline DynamicValue::Reader::Reader(std::nullptr_t n): type(UNKNOWN) {}
Chris@47 1260 inline DynamicValue::Builder::Builder(std::nullptr_t n): type(UNKNOWN) {}
Chris@47 1261
Chris@47 1262 #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \
Chris@47 1263 inline DynamicValue::Reader::Reader(cppType value) \
Chris@47 1264 : type(typeTag), fieldName##Value(value) {} \
Chris@47 1265 inline DynamicValue::Builder::Builder(cppType value) \
Chris@47 1266 : type(typeTag), fieldName##Value(value) {} \
Chris@47 1267 inline Orphan<DynamicValue>::Orphan(cppType value) \
Chris@47 1268 : type(DynamicValue::typeTag), fieldName##Value(value) {}
Chris@47 1269
Chris@47 1270 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Void, VOID, void);
Chris@47 1271 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(bool, BOOL, bool);
Chris@47 1272 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(char, INT, int);
Chris@47 1273 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(signed char, INT, int);
Chris@47 1274 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(short, INT, int);
Chris@47 1275 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(int, INT, int);
Chris@47 1276 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long, INT, int);
Chris@47 1277 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long long, INT, int);
Chris@47 1278 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned char, UINT, uint);
Chris@47 1279 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned short, UINT, uint);
Chris@47 1280 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned int, UINT, uint);
Chris@47 1281 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long, UINT, uint);
Chris@47 1282 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long long, UINT, uint);
Chris@47 1283 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(float, FLOAT, float);
Chris@47 1284 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(double, FLOAT, float);
Chris@47 1285 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicEnum, ENUM, enum);
Chris@47 1286 #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR
Chris@47 1287
Chris@47 1288 #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \
Chris@47 1289 inline DynamicValue::Reader::Reader(const cppType::Reader& value) \
Chris@47 1290 : type(typeTag), fieldName##Value(value) {} \
Chris@47 1291 inline DynamicValue::Builder::Builder(cppType::Builder value) \
Chris@47 1292 : type(typeTag), fieldName##Value(value) {}
Chris@47 1293
Chris@47 1294 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Text, TEXT, text);
Chris@47 1295 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Data, DATA, data);
Chris@47 1296 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicList, LIST, list);
Chris@47 1297 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicStruct, STRUCT, struct);
Chris@47 1298 CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(AnyPointer, ANY_POINTER, anyPointer);
Chris@47 1299
Chris@47 1300 #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR
Chris@47 1301
Chris@47 1302 inline DynamicValue::Reader::Reader(DynamicCapability::Client& value)
Chris@47 1303 : type(CAPABILITY), capabilityValue(value) {}
Chris@47 1304 inline DynamicValue::Reader::Reader(DynamicCapability::Client&& value)
Chris@47 1305 : type(CAPABILITY), capabilityValue(kj::mv(value)) {}
Chris@47 1306 template <typename T, typename>
Chris@47 1307 inline DynamicValue::Reader::Reader(kj::Own<T>&& value)
Chris@47 1308 : type(CAPABILITY), capabilityValue(kj::mv(value)) {}
Chris@47 1309 inline DynamicValue::Builder::Builder(DynamicCapability::Client& value)
Chris@47 1310 : type(CAPABILITY), capabilityValue(value) {}
Chris@47 1311 inline DynamicValue::Builder::Builder(DynamicCapability::Client&& value)
Chris@47 1312 : type(CAPABILITY), capabilityValue(kj::mv(value)) {}
Chris@47 1313
Chris@47 1314 inline DynamicValue::Reader::Reader(const char* value): Reader(Text::Reader(value)) {}
Chris@47 1315
Chris@47 1316 #define CAPNP_DECLARE_TYPE(discrim, typeName) \
Chris@47 1317 template <> \
Chris@47 1318 struct DynamicValue::Reader::AsImpl<typeName> { \
Chris@47 1319 static ReaderFor<typeName> apply(const Reader& reader); \
Chris@47 1320 }; \
Chris@47 1321 template <> \
Chris@47 1322 struct DynamicValue::Builder::AsImpl<typeName> { \
Chris@47 1323 static BuilderFor<typeName> apply(Builder& builder); \
Chris@47 1324 };
Chris@47 1325
Chris@47 1326 //CAPNP_DECLARE_TYPE(VOID, Void)
Chris@47 1327 CAPNP_DECLARE_TYPE(BOOL, bool)
Chris@47 1328 CAPNP_DECLARE_TYPE(INT8, int8_t)
Chris@47 1329 CAPNP_DECLARE_TYPE(INT16, int16_t)
Chris@47 1330 CAPNP_DECLARE_TYPE(INT32, int32_t)
Chris@47 1331 CAPNP_DECLARE_TYPE(INT64, int64_t)
Chris@47 1332 CAPNP_DECLARE_TYPE(UINT8, uint8_t)
Chris@47 1333 CAPNP_DECLARE_TYPE(UINT16, uint16_t)
Chris@47 1334 CAPNP_DECLARE_TYPE(UINT32, uint32_t)
Chris@47 1335 CAPNP_DECLARE_TYPE(UINT64, uint64_t)
Chris@47 1336 CAPNP_DECLARE_TYPE(FLOAT32, float)
Chris@47 1337 CAPNP_DECLARE_TYPE(FLOAT64, double)
Chris@47 1338
Chris@47 1339 CAPNP_DECLARE_TYPE(TEXT, Text)
Chris@47 1340 CAPNP_DECLARE_TYPE(DATA, Data)
Chris@47 1341 CAPNP_DECLARE_TYPE(LIST, DynamicList)
Chris@47 1342 CAPNP_DECLARE_TYPE(STRUCT, DynamicStruct)
Chris@47 1343 CAPNP_DECLARE_TYPE(INTERFACE, DynamicCapability)
Chris@47 1344 CAPNP_DECLARE_TYPE(ENUM, DynamicEnum)
Chris@47 1345 CAPNP_DECLARE_TYPE(ANY_POINTER, AnyPointer)
Chris@47 1346 #undef CAPNP_DECLARE_TYPE
Chris@47 1347
Chris@47 1348 // CAPNP_DECLARE_TYPE(Void) causes gcc 4.7 to segfault. If I do it manually and remove the
Chris@47 1349 // ReaderFor<> and BuilderFor<> wrappers, it works.
Chris@47 1350 template <>
Chris@47 1351 struct DynamicValue::Reader::AsImpl<Void> {
Chris@47 1352 static Void apply(const Reader& reader);
Chris@47 1353 };
Chris@47 1354 template <>
Chris@47 1355 struct DynamicValue::Builder::AsImpl<Void> {
Chris@47 1356 static Void apply(Builder& builder);
Chris@47 1357 };
Chris@47 1358
Chris@47 1359 template <typename T>
Chris@47 1360 struct DynamicValue::Reader::AsImpl<T, Kind::ENUM> {
Chris@47 1361 static T apply(const Reader& reader) {
Chris@47 1362 return reader.as<DynamicEnum>().as<T>();
Chris@47 1363 }
Chris@47 1364 };
Chris@47 1365 template <typename T>
Chris@47 1366 struct DynamicValue::Builder::AsImpl<T, Kind::ENUM> {
Chris@47 1367 static T apply(Builder& builder) {
Chris@47 1368 return builder.as<DynamicEnum>().as<T>();
Chris@47 1369 }
Chris@47 1370 };
Chris@47 1371
Chris@47 1372 template <typename T>
Chris@47 1373 struct DynamicValue::Reader::AsImpl<T, Kind::STRUCT> {
Chris@47 1374 static typename T::Reader apply(const Reader& reader) {
Chris@47 1375 return reader.as<DynamicStruct>().as<T>();
Chris@47 1376 }
Chris@47 1377 };
Chris@47 1378 template <typename T>
Chris@47 1379 struct DynamicValue::Builder::AsImpl<T, Kind::STRUCT> {
Chris@47 1380 static typename T::Builder apply(Builder& builder) {
Chris@47 1381 return builder.as<DynamicStruct>().as<T>();
Chris@47 1382 }
Chris@47 1383 };
Chris@47 1384
Chris@47 1385 template <typename T>
Chris@47 1386 struct DynamicValue::Reader::AsImpl<T, Kind::LIST> {
Chris@47 1387 static typename T::Reader apply(const Reader& reader) {
Chris@47 1388 return reader.as<DynamicList>().as<T>();
Chris@47 1389 }
Chris@47 1390 };
Chris@47 1391 template <typename T>
Chris@47 1392 struct DynamicValue::Builder::AsImpl<T, Kind::LIST> {
Chris@47 1393 static typename T::Builder apply(Builder& builder) {
Chris@47 1394 return builder.as<DynamicList>().as<T>();
Chris@47 1395 }
Chris@47 1396 };
Chris@47 1397
Chris@47 1398 template <typename T>
Chris@47 1399 struct DynamicValue::Reader::AsImpl<T, Kind::INTERFACE> {
Chris@47 1400 static typename T::Client apply(const Reader& reader) {
Chris@47 1401 return reader.as<DynamicCapability>().as<T>();
Chris@47 1402 }
Chris@47 1403 };
Chris@47 1404 template <typename T>
Chris@47 1405 struct DynamicValue::Builder::AsImpl<T, Kind::INTERFACE> {
Chris@47 1406 static typename T::Client apply(Builder& builder) {
Chris@47 1407 return builder.as<DynamicCapability>().as<T>();
Chris@47 1408 }
Chris@47 1409 };
Chris@47 1410
Chris@47 1411 inline DynamicValue::Pipeline::Pipeline(std::nullptr_t n): type(UNKNOWN) {}
Chris@47 1412 inline DynamicValue::Pipeline::Pipeline(DynamicStruct::Pipeline&& value)
Chris@47 1413 : type(STRUCT), structValue(kj::mv(value)) {}
Chris@47 1414 inline DynamicValue::Pipeline::Pipeline(DynamicCapability::Client&& value)
Chris@47 1415 : type(CAPABILITY), capabilityValue(kj::mv(value)) {}
Chris@47 1416
Chris@47 1417 template <typename T>
Chris@47 1418 struct DynamicValue::Pipeline::AsImpl<T, Kind::STRUCT> {
Chris@47 1419 static typename T::Pipeline apply(Pipeline& pipeline) {
Chris@47 1420 return pipeline.releaseAs<DynamicStruct>().releaseAs<T>();
Chris@47 1421 }
Chris@47 1422 };
Chris@47 1423 template <typename T>
Chris@47 1424 struct DynamicValue::Pipeline::AsImpl<T, Kind::INTERFACE> {
Chris@47 1425 static typename T::Client apply(Pipeline& pipeline) {
Chris@47 1426 return pipeline.releaseAs<DynamicCapability>().releaseAs<T>();
Chris@47 1427 }
Chris@47 1428 };
Chris@47 1429 template <>
Chris@47 1430 struct DynamicValue::Pipeline::AsImpl<DynamicStruct, Kind::OTHER> {
Chris@47 1431 static PipelineFor<DynamicStruct> apply(Pipeline& pipeline);
Chris@47 1432 };
Chris@47 1433 template <>
Chris@47 1434 struct DynamicValue::Pipeline::AsImpl<DynamicCapability, Kind::OTHER> {
Chris@47 1435 static PipelineFor<DynamicCapability> apply(Pipeline& pipeline);
Chris@47 1436 };
Chris@47 1437
Chris@47 1438 // -------------------------------------------------------------------
Chris@47 1439
Chris@47 1440 template <typename T>
Chris@47 1441 typename T::Reader DynamicStruct::Reader::as() const {
Chris@47 1442 static_assert(kind<T>() == Kind::STRUCT,
Chris@47 1443 "DynamicStruct::Reader::as<T>() can only convert to struct types.");
Chris@47 1444 schema.requireUsableAs<T>();
Chris@47 1445 return typename T::Reader(reader);
Chris@47 1446 }
Chris@47 1447
Chris@47 1448 template <typename T>
Chris@47 1449 typename T::Builder DynamicStruct::Builder::as() {
Chris@47 1450 static_assert(kind<T>() == Kind::STRUCT,
Chris@47 1451 "DynamicStruct::Builder::as<T>() can only convert to struct types.");
Chris@47 1452 schema.requireUsableAs<T>();
Chris@47 1453 return typename T::Builder(builder);
Chris@47 1454 }
Chris@47 1455
Chris@47 1456 template <>
Chris@47 1457 inline DynamicStruct::Reader DynamicStruct::Reader::as<DynamicStruct>() const {
Chris@47 1458 return *this;
Chris@47 1459 }
Chris@47 1460 template <>
Chris@47 1461 inline DynamicStruct::Builder DynamicStruct::Builder::as<DynamicStruct>() {
Chris@47 1462 return *this;
Chris@47 1463 }
Chris@47 1464
Chris@47 1465 inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const {
Chris@47 1466 return DynamicStruct::Reader(schema, builder.asReader());
Chris@47 1467 }
Chris@47 1468
Chris@47 1469 template <>
Chris@47 1470 inline AnyStruct::Reader DynamicStruct::Reader::as<AnyStruct>() const {
Chris@47 1471 return AnyStruct::Reader(reader);
Chris@47 1472 }
Chris@47 1473
Chris@47 1474 template <>
Chris@47 1475 inline AnyStruct::Builder DynamicStruct::Builder::as<AnyStruct>() {
Chris@47 1476 return AnyStruct::Builder(builder);
Chris@47 1477 }
Chris@47 1478
Chris@47 1479 template <typename T>
Chris@47 1480 typename T::Pipeline DynamicStruct::Pipeline::releaseAs() {
Chris@47 1481 static_assert(kind<T>() == Kind::STRUCT,
Chris@47 1482 "DynamicStruct::Pipeline::releaseAs<T>() can only convert to struct types.");
Chris@47 1483 schema.requireUsableAs<T>();
Chris@47 1484 return typename T::Pipeline(kj::mv(typeless));
Chris@47 1485 }
Chris@47 1486
Chris@47 1487 // -------------------------------------------------------------------
Chris@47 1488
Chris@47 1489 template <typename T>
Chris@47 1490 typename T::Reader DynamicList::Reader::as() const {
Chris@47 1491 static_assert(kind<T>() == Kind::LIST,
Chris@47 1492 "DynamicStruct::Reader::as<T>() can only convert to list types.");
Chris@47 1493 schema.requireUsableAs<T>();
Chris@47 1494 return typename T::Reader(reader);
Chris@47 1495 }
Chris@47 1496 template <typename T>
Chris@47 1497 typename T::Builder DynamicList::Builder::as() {
Chris@47 1498 static_assert(kind<T>() == Kind::LIST,
Chris@47 1499 "DynamicStruct::Builder::as<T>() can only convert to list types.");
Chris@47 1500 schema.requireUsableAs<T>();
Chris@47 1501 return typename T::Builder(builder);
Chris@47 1502 }
Chris@47 1503
Chris@47 1504 template <>
Chris@47 1505 inline DynamicList::Reader DynamicList::Reader::as<DynamicList>() const {
Chris@47 1506 return *this;
Chris@47 1507 }
Chris@47 1508 template <>
Chris@47 1509 inline DynamicList::Builder DynamicList::Builder::as<DynamicList>() {
Chris@47 1510 return *this;
Chris@47 1511 }
Chris@47 1512
Chris@47 1513 // -------------------------------------------------------------------
Chris@47 1514
Chris@47 1515 template <typename T, typename>
Chris@47 1516 inline DynamicCapability::Client::Client(T&& client)
Chris@47 1517 : Capability::Client(kj::mv(client)), schema(Schema::from<FromClient<T>>()) {}
Chris@47 1518
Chris@47 1519 template <typename T, typename>
Chris@47 1520 inline DynamicCapability::Client::Client(kj::Own<T>&& server)
Chris@47 1521 : Client(server->getSchema(), kj::mv(server)) {}
Chris@47 1522 template <typename T>
Chris@47 1523 inline DynamicCapability::Client::Client(InterfaceSchema schema, kj::Own<T>&& server)
Chris@47 1524 : Capability::Client(kj::mv(server)), schema(schema) {}
Chris@47 1525
Chris@47 1526 template <typename T, typename>
Chris@47 1527 typename T::Client DynamicCapability::Client::as() {
Chris@47 1528 static_assert(kind<T>() == Kind::INTERFACE,
Chris@47 1529 "DynamicCapability::Client::as<T>() can only convert to interface types.");
Chris@47 1530 schema.requireUsableAs<T>();
Chris@47 1531 return typename T::Client(hook->addRef());
Chris@47 1532 }
Chris@47 1533
Chris@47 1534 template <typename T, typename>
Chris@47 1535 typename T::Client DynamicCapability::Client::releaseAs() {
Chris@47 1536 static_assert(kind<T>() == Kind::INTERFACE,
Chris@47 1537 "DynamicCapability::Client::as<T>() can only convert to interface types.");
Chris@47 1538 schema.requireUsableAs<T>();
Chris@47 1539 return typename T::Client(kj::mv(hook));
Chris@47 1540 }
Chris@47 1541
Chris@47 1542 inline CallContext<DynamicStruct, DynamicStruct>::CallContext(
Chris@47 1543 CallContextHook& hook, StructSchema paramType, StructSchema resultType)
Chris@47 1544 : hook(&hook), paramType(paramType), resultType(resultType) {}
Chris@47 1545 inline DynamicStruct::Reader CallContext<DynamicStruct, DynamicStruct>::getParams() {
Chris@47 1546 return hook->getParams().getAs<DynamicStruct>(paramType);
Chris@47 1547 }
Chris@47 1548 inline void CallContext<DynamicStruct, DynamicStruct>::releaseParams() {
Chris@47 1549 hook->releaseParams();
Chris@47 1550 }
Chris@47 1551 inline DynamicStruct::Builder CallContext<DynamicStruct, DynamicStruct>::getResults(
Chris@47 1552 kj::Maybe<MessageSize> sizeHint) {
Chris@47 1553 return hook->getResults(sizeHint).getAs<DynamicStruct>(resultType);
Chris@47 1554 }
Chris@47 1555 inline DynamicStruct::Builder CallContext<DynamicStruct, DynamicStruct>::initResults(
Chris@47 1556 kj::Maybe<MessageSize> sizeHint) {
Chris@47 1557 return hook->getResults(sizeHint).initAs<DynamicStruct>(resultType);
Chris@47 1558 }
Chris@47 1559 inline void CallContext<DynamicStruct, DynamicStruct>::setResults(DynamicStruct::Reader value) {
Chris@47 1560 hook->getResults(value.totalSize()).setAs<DynamicStruct>(value);
Chris@47 1561 }
Chris@47 1562 inline void CallContext<DynamicStruct, DynamicStruct>::adoptResults(Orphan<DynamicStruct>&& value) {
Chris@47 1563 hook->getResults(MessageSize { 0, 0 }).adopt(kj::mv(value));
Chris@47 1564 }
Chris@47 1565 inline Orphanage CallContext<DynamicStruct, DynamicStruct>::getResultsOrphanage(
Chris@47 1566 kj::Maybe<MessageSize> sizeHint) {
Chris@47 1567 return Orphanage::getForMessageContaining(hook->getResults(sizeHint));
Chris@47 1568 }
Chris@47 1569 template <typename SubParams>
Chris@47 1570 inline kj::Promise<void> CallContext<DynamicStruct, DynamicStruct>::tailCall(
Chris@47 1571 Request<SubParams, DynamicStruct>&& tailRequest) {
Chris@47 1572 return hook->tailCall(kj::mv(tailRequest.hook));
Chris@47 1573 }
Chris@47 1574 inline void CallContext<DynamicStruct, DynamicStruct>::allowCancellation() {
Chris@47 1575 hook->allowCancellation();
Chris@47 1576 }
Chris@47 1577
Chris@47 1578 template <>
Chris@47 1579 inline DynamicCapability::Client Capability::Client::castAs<DynamicCapability>(
Chris@47 1580 InterfaceSchema schema) {
Chris@47 1581 return DynamicCapability::Client(schema, hook->addRef());
Chris@47 1582 }
Chris@47 1583
Chris@47 1584 // -------------------------------------------------------------------
Chris@47 1585
Chris@47 1586 template <typename T>
Chris@47 1587 ReaderFor<T> ConstSchema::as() const {
Chris@47 1588 return DynamicValue::Reader(*this).as<T>();
Chris@47 1589 }
Chris@47 1590
Chris@47 1591 } // namespace capnp
Chris@47 1592
Chris@47 1593 #endif // CAPNP_DYNAMIC_H_