annotate win32-mingw/include/capnp/dynamic.h @ 79:91c729825bca pa_catalina

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