annotate win32-mingw/include/capnp/dynamic.h @ 50:37d53a7e8262

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