annotate win64-msvc/include/capnp/dynamic.h @ 63:0f2d93caa50c

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