// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // This file defines classes that can be used to manipulate messages based on schemas that are not // known until runtime. This is also useful for writing generic code that uses schemas to handle // arbitrary types in a generic way. // // Each of the classes defined here has a to() template method which converts an instance back to a // native type. This method will throw an exception if the requested type does not match the // schema. To convert native types to dynamic, use DynamicFactory. // // As always, underlying data is validated lazily, so you have to actually traverse the whole // message if you want to validate all content. #ifndef CAPNP_DYNAMIC_H_ #define CAPNP_DYNAMIC_H_ #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) #pragma GCC system_header #endif #include "schema.h" #include "layout.h" #include "message.h" #include "any.h" #include "capability.h" namespace capnp { class MessageReader; class MessageBuilder; struct DynamicValue { DynamicValue() = delete; enum Type { UNKNOWN, // Means that the value has unknown type and content because it comes from a newer version of // the schema, or from a newer version of Cap'n Proto that has new features that this version // doesn't understand. VOID, BOOL, INT, UINT, FLOAT, TEXT, DATA, LIST, ENUM, STRUCT, CAPABILITY, ANY_POINTER }; class Reader; class Builder; class Pipeline; }; class DynamicEnum; struct DynamicStruct { DynamicStruct() = delete; class Reader; class Builder; class Pipeline; }; struct DynamicList { DynamicList() = delete; class Reader; class Builder; }; struct DynamicCapability { DynamicCapability() = delete; class Client; class Server; }; template <> class Orphan; template struct DynamicTypeFor_; template <> struct DynamicTypeFor_ { typedef DynamicEnum Type; }; template <> struct DynamicTypeFor_ { typedef DynamicStruct Type; }; template <> struct DynamicTypeFor_ { typedef DynamicList Type; }; template <> struct DynamicTypeFor_ { typedef DynamicCapability Type; }; template using DynamicTypeFor = typename DynamicTypeFor_()>::Type; template ReaderFor>> toDynamic(T&& value); template BuilderFor>> toDynamic(T&& value); template DynamicTypeFor> toDynamic(T&& value); template typename DynamicTypeFor>::Client toDynamic(kj::Own&& value); namespace _ { // private template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; } // namespace _ (private) template <> inline constexpr Style style() { return Style::POINTER; } template <> inline constexpr Style style() { return Style::PRIMITIVE; } template <> inline constexpr Style style() { return Style::STRUCT; } template <> inline constexpr Style style() { return Style::POINTER; } template <> inline constexpr Style style() { return Style::CAPABILITY; } // ------------------------------------------------------------------- class DynamicEnum { public: DynamicEnum() = default; inline DynamicEnum(EnumSchema::Enumerant enumerant) : schema(enumerant.getContainingEnum()), value(enumerant.getOrdinal()) {} inline DynamicEnum(EnumSchema schema, uint16_t value) : schema(schema), value(value) {} template () == Kind::ENUM>> inline DynamicEnum(T&& value): DynamicEnum(toDynamic(value)) {} template inline T as() const { return static_cast(asImpl(typeId())); } // Cast to a native enum type. inline EnumSchema getSchema() const { return schema; } kj::Maybe getEnumerant() const; // Get which enumerant this enum value represents. Returns nullptr if the numeric value does not // correspond to any enumerant in the schema -- this can happen if the data was built using a // newer schema that has more values defined. inline uint16_t getRaw() const { return value; } // Returns the raw underlying enum value. private: EnumSchema schema; uint16_t value; uint16_t asImpl(uint64_t requestedTypeId) const; friend struct DynamicStruct; friend struct DynamicList; friend struct DynamicValue; template friend DynamicTypeFor> toDynamic(T&& value); }; // ------------------------------------------------------------------- class DynamicStruct::Reader { public: typedef DynamicStruct Reads; Reader() = default; template >() == Kind::STRUCT>> inline Reader(T&& value): Reader(toDynamic(value)) {} inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } template typename T::Reader as() const; // Convert the dynamic struct to its compiled-in type. inline StructSchema getSchema() const { return schema; } DynamicValue::Reader get(StructSchema::Field field) const; // Read the given field value. bool has(StructSchema::Field field) const; // Tests whether the given field is set to its default value. For pointer values, this does // not actually traverse the value comparing it with the default, but simply returns true if the // pointer is non-null. For members of unions, has() returns false if the union member is not // active, but does not necessarily return true if the member is active (depends on the field's // value). kj::Maybe which() const; // If the struct contains an (unnamed) union, and the currently-active field within that union // is known, this returns that field. Otherwise, it returns null. In other words, this returns // null if there is no union present _or_ if the union's discriminant is set to an unrecognized // value. This could happen in particular when receiving a message from a sender who has a // newer version of the protocol and is using a field of the union that you don't know about yet. DynamicValue::Reader get(kj::StringPtr name) const; bool has(kj::StringPtr name) const; // Shortcuts to access fields by name. These throw exceptions if no such field exists. private: StructSchema schema; _::StructReader reader; inline Reader(StructSchema schema, _::StructReader reader) : schema(schema), reader(reader) {} Reader(StructSchema schema, const _::OrphanBuilder& orphan); bool isSetInUnion(StructSchema::Field field) const; void verifySetInUnion(StructSchema::Field field) const; static DynamicValue::Reader getImpl(_::StructReader reader, StructSchema::Field field); template friend struct _::PointerHelpers; friend class DynamicStruct::Builder; friend struct DynamicList; friend class MessageReader; friend class MessageBuilder; template friend struct ::capnp::ToDynamic_; friend kj::StringTree _::structString( _::StructReader reader, const _::RawBrandedSchema& schema); friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; }; class DynamicStruct::Builder { public: typedef DynamicStruct Builds; Builder() = default; inline Builder(decltype(nullptr)) {} template >() == Kind::STRUCT>> inline Builder(T&& value): Builder(toDynamic(value)) {} inline MessageSize totalSize() const { return asReader().totalSize(); } template typename T::Builder as(); // Cast to a particular struct type. inline StructSchema getSchema() const { return schema; } DynamicValue::Builder get(StructSchema::Field field); // Read the given field value. inline bool has(StructSchema::Field field) { return asReader().has(field); } // Tests whether the given field is set to its default value. For pointer values, this does // not actually traverse the value comparing it with the default, but simply returns true if the // pointer is non-null. For members of unions, has() returns whether the field is currently // active and the union as a whole is non-default -- so, the only time has() will return false // for an active union field is if it is the default active field and it has its default value. kj::Maybe which(); // If the struct contains an (unnamed) union, and the currently-active field within that union // is known, this returns that field. Otherwise, it returns null. In other words, this returns // null if there is no union present _or_ if the union's discriminant is set to an unrecognized // value. This could happen in particular when receiving a message from a sender who has a // newer version of the protocol and is using a field of the union that you don't know about yet. void set(StructSchema::Field field, const DynamicValue::Reader& value); // Set the given field value. DynamicValue::Builder init(StructSchema::Field field); DynamicValue::Builder init(StructSchema::Field field, uint size); // Init a struct, list, or blob field. void adopt(StructSchema::Field field, Orphan&& orphan); Orphan disown(StructSchema::Field field); // Adopt/disown. This works even for non-pointer fields: adopt() becomes equivalent to set() // and disown() becomes like get() followed by clear(). void clear(StructSchema::Field field); // Clear a field, setting it to its default value. For pointer fields, this actually makes the // field null. DynamicValue::Builder get(kj::StringPtr name); bool has(kj::StringPtr name); void set(kj::StringPtr name, const DynamicValue::Reader& value); void set(kj::StringPtr name, std::initializer_list value); DynamicValue::Builder init(kj::StringPtr name); DynamicValue::Builder init(kj::StringPtr name, uint size); void adopt(kj::StringPtr name, Orphan&& orphan); Orphan disown(kj::StringPtr name); void clear(kj::StringPtr name); // Shortcuts to access fields by name. These throw exceptions if no such field exists. Reader asReader() const; private: StructSchema schema; _::StructBuilder builder; inline Builder(StructSchema schema, _::StructBuilder builder) : schema(schema), builder(builder) {} Builder(StructSchema schema, _::OrphanBuilder& orphan); bool isSetInUnion(StructSchema::Field field); void verifySetInUnion(StructSchema::Field field); void setInUnion(StructSchema::Field field); template friend struct _::PointerHelpers; friend struct DynamicList; friend class MessageReader; friend class MessageBuilder; template friend struct ::capnp::ToDynamic_; friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; }; class DynamicStruct::Pipeline { public: typedef DynamicStruct Pipelines; inline Pipeline(decltype(nullptr)): typeless(nullptr) {} template typename T::Pipeline releaseAs(); // Convert the dynamic pipeline to its compiled-in type. inline StructSchema getSchema() { return schema; } DynamicValue::Pipeline get(StructSchema::Field field); // Read the given field value. DynamicValue::Pipeline get(kj::StringPtr name); // Get by string name. private: StructSchema schema; AnyPointer::Pipeline typeless; inline explicit Pipeline(StructSchema schema, AnyPointer::Pipeline&& typeless) : schema(schema), typeless(kj::mv(typeless)) {} friend class Request; }; // ------------------------------------------------------------------- class DynamicList::Reader { public: typedef DynamicList Reads; inline Reader(): reader(ElementSize::VOID) {} template >() == Kind::LIST>> inline Reader(T&& value): Reader(toDynamic(value)) {} template typename T::Reader as() const; // Try to convert to any List, Data, or Text. Throws an exception if the underlying data // can't possibly represent the requested type. inline ListSchema getSchema() const { return schema; } inline uint size() const { return unbound(reader.size() / ELEMENTS); } DynamicValue::Reader operator[](uint index) const; typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: ListSchema schema; _::ListReader reader; Reader(ListSchema schema, _::ListReader reader): schema(schema), reader(reader) {} Reader(ListSchema schema, const _::OrphanBuilder& orphan); template friend struct _::PointerHelpers; friend struct DynamicStruct; friend class DynamicList::Builder; template friend struct ::capnp::ToDynamic_; friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; }; class DynamicList::Builder { public: typedef DynamicList Builds; inline Builder(): builder(ElementSize::VOID) {} inline Builder(decltype(nullptr)): builder(ElementSize::VOID) {} template >() == Kind::LIST>> inline Builder(T&& value): Builder(toDynamic(value)) {} template typename T::Builder as(); // Try to convert to any List, Data, or Text. Throws an exception if the underlying data // can't possibly represent the requested type. inline ListSchema getSchema() const { return schema; } inline uint size() const { return unbound(builder.size() / ELEMENTS); } DynamicValue::Builder operator[](uint index); void set(uint index, const DynamicValue::Reader& value); DynamicValue::Builder init(uint index, uint size); void adopt(uint index, Orphan&& orphan); Orphan disown(uint index); typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } void copyFrom(std::initializer_list value); Reader asReader() const; private: ListSchema schema; _::ListBuilder builder; Builder(ListSchema schema, _::ListBuilder builder): schema(schema), builder(builder) {} Builder(ListSchema schema, _::OrphanBuilder& orphan); template friend struct _::PointerHelpers; friend struct DynamicStruct; template friend struct ::capnp::ToDynamic_; friend class Orphanage; template friend struct _::OrphanGetImpl; friend class Orphan; friend class Orphan; friend class Orphan; }; // ------------------------------------------------------------------- class DynamicCapability::Client: public Capability::Client { public: typedef DynamicCapability Calls; typedef DynamicCapability Reads; Client() = default; template >() == Kind::INTERFACE>> inline Client(T&& client); template ()>> inline Client(kj::Own&& server); template () == Kind::INTERFACE>> typename T::Client as(); template () == Kind::INTERFACE>> typename T::Client releaseAs(); // Convert to any client type. Client upcast(InterfaceSchema requestedSchema); // Upcast to a superclass. Throws an exception if `schema` is not a superclass. inline InterfaceSchema getSchema() { return schema; } Request newRequest( InterfaceSchema::Method method, kj::Maybe sizeHint = nullptr); Request newRequest( kj::StringPtr methodName, kj::Maybe sizeHint = nullptr); private: InterfaceSchema schema; Client(InterfaceSchema schema, kj::Own&& hook) : Capability::Client(kj::mv(hook)), schema(schema) {} template inline Client(InterfaceSchema schema, kj::Own&& server); friend struct Capability; friend struct DynamicStruct; friend struct DynamicList; friend struct DynamicValue; friend class Orphan; friend class Orphan; friend class Orphan; template friend struct _::PointerHelpers; }; class DynamicCapability::Server: public Capability::Server { public: typedef DynamicCapability Serves; Server(InterfaceSchema schema): schema(schema) {} virtual kj::Promise call(InterfaceSchema::Method method, CallContext context) = 0; kj::Promise dispatchCall(uint64_t interfaceId, uint16_t methodId, CallContext context) override final; inline InterfaceSchema getSchema() const { return schema; } private: InterfaceSchema schema; }; template <> class Request: public DynamicStruct::Builder { // Specialization of `Request` for DynamicStruct. public: inline Request(DynamicStruct::Builder builder, kj::Own&& hook, StructSchema resultSchema) : DynamicStruct::Builder(builder), hook(kj::mv(hook)), resultSchema(resultSchema) {} RemotePromise send(); // Send the call and return a promise for the results. private: kj::Own hook; StructSchema resultSchema; friend class Capability::Client; friend struct DynamicCapability; template friend class CallContext; friend class RequestHook; }; template <> class CallContext: public kj::DisallowConstCopy { // Wrapper around CallContextHook with a specific return type. // // Methods of this class may only be called from within the server's event loop, not from other // threads. public: explicit CallContext(CallContextHook& hook, StructSchema paramType, StructSchema resultType); DynamicStruct::Reader getParams(); void releaseParams(); DynamicStruct::Builder getResults(kj::Maybe sizeHint = nullptr); DynamicStruct::Builder initResults(kj::Maybe sizeHint = nullptr); void setResults(DynamicStruct::Reader value); void adoptResults(Orphan&& value); Orphanage getResultsOrphanage(kj::Maybe sizeHint = nullptr); template kj::Promise tailCall(Request&& tailRequest); void allowCancellation(); private: CallContextHook* hook; StructSchema paramType; StructSchema resultType; friend class DynamicCapability::Server; }; // ------------------------------------------------------------------- // Make sure ReaderFor and BuilderFor work for DynamicEnum, DynamicStruct, and // DynamicList, so that we can define DynamicValue::as(). template <> struct ReaderFor_ { typedef DynamicEnum Type; }; template <> struct BuilderFor_ { typedef DynamicEnum Type; }; template <> struct ReaderFor_ { typedef DynamicStruct::Reader Type; }; template <> struct BuilderFor_ { typedef DynamicStruct::Builder Type; }; template <> struct ReaderFor_ { typedef DynamicList::Reader Type; }; template <> struct BuilderFor_ { typedef DynamicList::Builder Type; }; template <> struct ReaderFor_ { typedef DynamicCapability::Client Type; }; template <> struct BuilderFor_ { typedef DynamicCapability::Client Type; }; template <> struct PipelineFor_ { typedef DynamicCapability::Client Type; }; class DynamicValue::Reader { public: typedef DynamicValue Reads; inline Reader(decltype(nullptr) n = nullptr); // UNKNOWN inline Reader(Void value); inline Reader(bool value); inline Reader(char value); inline Reader(signed char value); inline Reader(short value); inline Reader(int value); inline Reader(long value); inline Reader(long long value); inline Reader(unsigned char value); inline Reader(unsigned short value); inline Reader(unsigned int value); inline Reader(unsigned long value); inline Reader(unsigned long long value); inline Reader(float value); inline Reader(double value); inline Reader(const char* value); // Text inline Reader(const Text::Reader& value); inline Reader(const Data::Reader& value); inline Reader(const DynamicList::Reader& value); inline Reader(DynamicEnum value); inline Reader(const DynamicStruct::Reader& value); inline Reader(const AnyPointer::Reader& value); inline Reader(DynamicCapability::Client& value); inline Reader(DynamicCapability::Client&& value); template ()>> inline Reader(kj::Own&& value); Reader(ConstSchema constant); template ()))> inline Reader(T&& value): Reader(toDynamic(kj::mv(value))) {} Reader(const Reader& other); Reader(Reader&& other) noexcept; ~Reader() noexcept(false); Reader& operator=(const Reader& other); Reader& operator=(Reader&& other); // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not // trivially copyable. template inline ReaderFor as() const { return AsImpl::apply(*this); } // Use to interpret the value as some Cap'n Proto type. Allowed types are: // - Void, bool, [u]int{8,16,32,64}_t, float, double, any enum: Returns the raw value. // - Text, Data, AnyPointer, any struct type: Returns the corresponding Reader. // - List for any T listed above: Returns List::Reader. // - DynamicEnum: Returns the corresponding type. // - DynamicStruct, DynamicList: Returns the corresponding Reader. // - Any capability type, including DynamicCapability: Returns the corresponding Client. // - DynamicValue: Returns an identical Reader. Useful to avoid special-casing in generic code. // (TODO(perf): On GCC 4.8 / Clang 3.3, provide rvalue-qualified version that avoids // refcounting.) // // DynamicValue allows various implicit conversions, mostly just to make the interface friendlier. // - Any integer can be converted to any other integer type so long as the actual value is within // the new type's range. // - Floating-point types can be converted to integers as long as no information would be lost // in the conversion. // - Integers can be converted to floating points. This may lose information, but won't throw. // - Float32/Float64 can be converted between each other. Converting Float64 -> Float32 may lose // information, but won't throw. // - Text can be converted to an enum, if the Text matches one of the enumerant names (but not // vice-versa). // - Capabilities can be upcast (cast to a supertype), but not downcast. // // Any other conversion attempt will throw an exception. inline Type getType() const { return type; } // Get the type of this value. private: Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; Text::Reader textValue; Data::Reader dataValue; DynamicList::Reader listValue; DynamicEnum enumValue; DynamicStruct::Reader structValue; AnyPointer::Reader anyPointerValue; mutable DynamicCapability::Client capabilityValue; // Declared mutable because `Client`s normally cannot be const. // Warning: Copy/move constructors assume all these types are trivially copyable except // Capability. }; template ()> struct AsImpl; // Implementation backing the as() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. friend class Orphanage; // to speed up newOrphanCopy(DynamicValue::Reader) }; class DynamicValue::Builder { public: typedef DynamicValue Builds; inline Builder(decltype(nullptr) n = nullptr); // UNKNOWN inline Builder(Void value); inline Builder(bool value); inline Builder(char value); inline Builder(signed char value); inline Builder(short value); inline Builder(int value); inline Builder(long value); inline Builder(long long value); inline Builder(unsigned char value); inline Builder(unsigned short value); inline Builder(unsigned int value); inline Builder(unsigned long value); inline Builder(unsigned long long value); inline Builder(float value); inline Builder(double value); inline Builder(Text::Builder value); inline Builder(Data::Builder value); inline Builder(DynamicList::Builder value); inline Builder(DynamicEnum value); inline Builder(DynamicStruct::Builder value); inline Builder(AnyPointer::Builder value); inline Builder(DynamicCapability::Client& value); inline Builder(DynamicCapability::Client&& value); template ()))> inline Builder(T value): Builder(toDynamic(value)) {} Builder(Builder& other); Builder(Builder&& other) noexcept; ~Builder() noexcept(false); Builder& operator=(Builder& other); Builder& operator=(Builder&& other); // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not // trivially copyable. template inline BuilderFor as() { return AsImpl::apply(*this); } // See DynamicValue::Reader::as(). inline Type getType() { return type; } // Get the type of this value. Reader asReader() const; private: Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; Text::Builder textValue; Data::Builder dataValue; DynamicList::Builder listValue; DynamicEnum enumValue; DynamicStruct::Builder structValue; AnyPointer::Builder anyPointerValue; mutable DynamicCapability::Client capabilityValue; // Declared mutable because `Client`s normally cannot be const. }; template ()> struct AsImpl; // Implementation backing the as() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. friend class Orphan; }; class DynamicValue::Pipeline { public: typedef DynamicValue Pipelines; inline Pipeline(decltype(nullptr) n = nullptr); inline Pipeline(DynamicStruct::Pipeline&& value); inline Pipeline(DynamicCapability::Client&& value); Pipeline(Pipeline&& other) noexcept; Pipeline& operator=(Pipeline&& other); ~Pipeline() noexcept(false); template inline PipelineFor releaseAs() { return AsImpl::apply(*this); } inline Type getType() { return type; } // Get the type of this value. private: Type type; union { DynamicStruct::Pipeline structValue; DynamicCapability::Client capabilityValue; }; template ()> struct AsImpl; // Implementation backing the releaseAs() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. }; kj::StringTree KJ_STRINGIFY(const DynamicValue::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicValue::Builder& value); kj::StringTree KJ_STRINGIFY(DynamicEnum value); kj::StringTree KJ_STRINGIFY(const DynamicStruct::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicStruct::Builder& value); kj::StringTree KJ_STRINGIFY(const DynamicList::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicList::Builder& value); // ------------------------------------------------------------------- // Orphan <-> Dynamic glue template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::STRUCT>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicStruct::Builder get(); DynamicStruct::Reader getReader() const; template Orphan releaseAs(); // Like DynamicStruct::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: StructSchema schema; _::OrphanBuilder builder; inline Orphan(StructSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; friend class MessageBuilder; }; template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::LIST>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicList::Builder get(); DynamicList::Reader getReader() const; template Orphan releaseAs(); // Like DynamicList::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. // TODO(someday): Support truncate(). inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: ListSchema schema; _::OrphanBuilder builder; inline Orphan(ListSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; }; template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::INTERFACE>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicCapability::Client get(); DynamicCapability::Client getReader() const; template Orphan releaseAs(); // Like DynamicCapability::Client::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: InterfaceSchema schema; _::OrphanBuilder builder; inline Orphan(InterfaceSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; }; template <> class Orphan { public: inline Orphan(decltype(nullptr) n = nullptr): type(DynamicValue::UNKNOWN) {} inline Orphan(Void value); inline Orphan(bool value); inline Orphan(char value); inline Orphan(signed char value); inline Orphan(short value); inline Orphan(int value); inline Orphan(long value); inline Orphan(long long value); inline Orphan(unsigned char value); inline Orphan(unsigned short value); inline Orphan(unsigned int value); inline Orphan(unsigned long value); inline Orphan(unsigned long long value); inline Orphan(float value); inline Orphan(double value); inline Orphan(DynamicEnum value); Orphan(Orphan&&) = default; template Orphan(Orphan&&); Orphan(Orphan&&); Orphan(void*) = delete; // So Orphan(bool) doesn't accept pointers. KJ_DISALLOW_COPY(Orphan); Orphan& operator=(Orphan&&) = default; inline DynamicValue::Type getType() { return type; } DynamicValue::Builder get(); DynamicValue::Reader getReader() const; template Orphan releaseAs(); // Like DynamicValue::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. private: DynamicValue::Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; DynamicEnum enumValue; StructSchema structSchema; ListSchema listSchema; InterfaceSchema interfaceSchema; }; _::OrphanBuilder builder; // Only used if `type` is a pointer type. Orphan(DynamicValue::Builder value, _::OrphanBuilder&& builder); Orphan(DynamicValue::Type type, _::OrphanBuilder&& builder) : type(type), builder(kj::mv(builder)) {} Orphan(StructSchema structSchema, _::OrphanBuilder&& builder) : type(DynamicValue::STRUCT), structSchema(structSchema), builder(kj::mv(builder)) {} Orphan(ListSchema listSchema, _::OrphanBuilder&& builder) : type(DynamicValue::LIST), listSchema(listSchema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicStruct; friend struct DynamicList; friend struct AnyPointer; friend class Orphanage; }; template inline Orphan::Orphan(Orphan&& other) : Orphan(other.get(), kj::mv(other.builder)) {} inline Orphan::Orphan(Orphan&& other) : type(DynamicValue::ANY_POINTER), builder(kj::mv(other.builder)) {} template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check type = DynamicValue::UNKNOWN; return Orphan(kj::mv(builder)); } template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> struct Orphanage::GetInnerBuilder { static inline _::StructBuilder apply(DynamicStruct::Builder& t) { return t.builder; } }; template <> struct Orphanage::GetInnerBuilder { static inline _::ListBuilder apply(DynamicList::Builder& t) { return t.builder; } }; template <> inline Orphan Orphanage::newOrphanCopy( DynamicStruct::Reader copyFrom) const { return Orphan( copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.reader)); } template <> inline Orphan Orphanage::newOrphanCopy( DynamicList::Reader copyFrom) const { return Orphan(copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.reader)); } template <> inline Orphan Orphanage::newOrphanCopy( DynamicCapability::Client copyFrom) const { return Orphan( copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.hook->addRef())); } template <> Orphan Orphanage::newOrphanCopy( DynamicValue::Reader copyFrom) const; namespace _ { // private template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicStruct::Reader getDynamic(PointerReader reader, StructSchema schema); static DynamicStruct::Builder getDynamic(PointerBuilder builder, StructSchema schema); static void set(PointerBuilder builder, const DynamicStruct::Reader& value); static DynamicStruct::Builder init(PointerBuilder builder, StructSchema schema); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, StructSchema schema) { return Orphan(schema, builder.disown()); } }; template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicList::Reader getDynamic(PointerReader reader, ListSchema schema); static DynamicList::Builder getDynamic(PointerBuilder builder, ListSchema schema); static void set(PointerBuilder builder, const DynamicList::Reader& value); static DynamicList::Builder init(PointerBuilder builder, ListSchema schema, uint size); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, ListSchema schema) { return Orphan(schema, builder.disown()); } }; template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicCapability::Client getDynamic(PointerReader reader, InterfaceSchema schema); static DynamicCapability::Client getDynamic(PointerBuilder builder, InterfaceSchema schema); static void set(PointerBuilder builder, DynamicCapability::Client& value); static void set(PointerBuilder builder, DynamicCapability::Client&& value); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, InterfaceSchema schema) { return Orphan(schema, builder.disown()); } }; } // namespace _ (private) template inline ReaderFor AnyPointer::Reader::getAs(StructSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline ReaderFor AnyPointer::Reader::getAs(ListSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline ReaderFor AnyPointer::Reader::getAs(InterfaceSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline BuilderFor AnyPointer::Builder::getAs(StructSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::getAs(ListSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::getAs(InterfaceSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::initAs(StructSchema schema) { return _::PointerHelpers::init(builder, schema); } template inline BuilderFor AnyPointer::Builder::initAs(ListSchema schema, uint elementCount) { return _::PointerHelpers::init(builder, schema, elementCount); } template <> inline void AnyPointer::Builder::setAs(DynamicStruct::Reader value) { return _::PointerHelpers::set(builder, value); } template <> inline void AnyPointer::Builder::setAs(DynamicList::Reader value) { return _::PointerHelpers::set(builder, value); } template <> inline void AnyPointer::Builder::setAs(DynamicCapability::Client value) { return _::PointerHelpers::set(builder, kj::mv(value)); } template <> void AnyPointer::Builder::adopt(Orphan&& orphan); template inline Orphan AnyPointer::Builder::disownAs(StructSchema schema) { return _::PointerHelpers::disown(builder, schema); } template inline Orphan AnyPointer::Builder::disownAs(ListSchema schema) { return _::PointerHelpers::disown(builder, schema); } template inline Orphan AnyPointer::Builder::disownAs(InterfaceSchema schema) { return _::PointerHelpers::disown(builder, schema); } // We have to declare the methods below inline because Clang and GCC disagree about how to mangle // their symbol names. template <> inline DynamicStruct::Builder Orphan::getAs(StructSchema schema) { return DynamicStruct::Builder(schema, builder); } template <> inline DynamicStruct::Reader Orphan::getAsReader( StructSchema schema) const { return DynamicStruct::Reader(schema, builder); } template <> inline Orphan Orphan::releaseAs(StructSchema schema) { return Orphan(schema, kj::mv(builder)); } template <> inline DynamicList::Builder Orphan::getAs(ListSchema schema) { return DynamicList::Builder(schema, builder); } template <> inline DynamicList::Reader Orphan::getAsReader(ListSchema schema) const { return DynamicList::Reader(schema, builder); } template <> inline Orphan Orphan::releaseAs(ListSchema schema) { return Orphan(schema, kj::mv(builder)); } template <> inline DynamicCapability::Client Orphan::getAs( InterfaceSchema schema) { return DynamicCapability::Client(schema, builder.asCapability()); } template <> inline DynamicCapability::Client Orphan::getAsReader( InterfaceSchema schema) const { return DynamicCapability::Client(schema, builder.asCapability()); } template <> inline Orphan Orphan::releaseAs( InterfaceSchema schema) { return Orphan(schema, kj::mv(builder)); } // ======================================================================================= // Inline implementation details. template struct ToDynamic_ { static inline DynamicStruct::Reader apply(const typename T::Reader& value) { return DynamicStruct::Reader(Schema::from(), value._reader); } static inline DynamicStruct::Builder apply(typename T::Builder& value) { return DynamicStruct::Builder(Schema::from(), value._builder); } }; template struct ToDynamic_ { static inline DynamicList::Reader apply(const typename T::Reader& value) { return DynamicList::Reader(Schema::from(), value.reader); } static inline DynamicList::Builder apply(typename T::Builder& value) { return DynamicList::Builder(Schema::from(), value.builder); } }; template struct ToDynamic_ { static inline DynamicCapability::Client apply(typename T::Client value) { return DynamicCapability::Client(kj::mv(value)); } static inline DynamicCapability::Client apply(typename T::Client&& value) { return DynamicCapability::Client(kj::mv(value)); } }; template ReaderFor>> toDynamic(T&& value) { return ToDynamic_>::apply(value); } template BuilderFor>> toDynamic(T&& value) { return ToDynamic_>::apply(value); } template DynamicTypeFor> toDynamic(T&& value) { return DynamicEnum(Schema::from>(), static_cast(value)); } template typename DynamicTypeFor>::Client toDynamic(kj::Own&& value) { return typename FromServer::Client(kj::mv(value)); } inline DynamicValue::Reader::Reader(std::nullptr_t n): type(UNKNOWN) {} inline DynamicValue::Builder::Builder(std::nullptr_t n): type(UNKNOWN) {} #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \ inline DynamicValue::Reader::Reader(cppType value) \ : type(typeTag), fieldName##Value(value) {} \ inline DynamicValue::Builder::Builder(cppType value) \ : type(typeTag), fieldName##Value(value) {} \ inline Orphan::Orphan(cppType value) \ : type(DynamicValue::typeTag), fieldName##Value(value) {} CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Void, VOID, void); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(bool, BOOL, bool); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(char, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(signed char, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(short, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(int, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long long, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned char, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned short, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned int, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long long, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(float, FLOAT, float); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(double, FLOAT, float); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicEnum, ENUM, enum); #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \ inline DynamicValue::Reader::Reader(const cppType::Reader& value) \ : type(typeTag), fieldName##Value(value) {} \ inline DynamicValue::Builder::Builder(cppType::Builder value) \ : type(typeTag), fieldName##Value(value) {} CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Text, TEXT, text); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Data, DATA, data); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicList, LIST, list); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicStruct, STRUCT, struct); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(AnyPointer, ANY_POINTER, anyPointer); #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR inline DynamicValue::Reader::Reader(DynamicCapability::Client& value) : type(CAPABILITY), capabilityValue(value) {} inline DynamicValue::Reader::Reader(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} template inline DynamicValue::Reader::Reader(kj::Own&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} inline DynamicValue::Builder::Builder(DynamicCapability::Client& value) : type(CAPABILITY), capabilityValue(value) {} inline DynamicValue::Builder::Builder(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} inline DynamicValue::Reader::Reader(const char* value): Reader(Text::Reader(value)) {} #define CAPNP_DECLARE_TYPE(discrim, typeName) \ template <> \ struct DynamicValue::Reader::AsImpl { \ static ReaderFor apply(const Reader& reader); \ }; \ template <> \ struct DynamicValue::Builder::AsImpl { \ static BuilderFor apply(Builder& builder); \ }; //CAPNP_DECLARE_TYPE(VOID, Void) CAPNP_DECLARE_TYPE(BOOL, bool) CAPNP_DECLARE_TYPE(INT8, int8_t) CAPNP_DECLARE_TYPE(INT16, int16_t) CAPNP_DECLARE_TYPE(INT32, int32_t) CAPNP_DECLARE_TYPE(INT64, int64_t) CAPNP_DECLARE_TYPE(UINT8, uint8_t) CAPNP_DECLARE_TYPE(UINT16, uint16_t) CAPNP_DECLARE_TYPE(UINT32, uint32_t) CAPNP_DECLARE_TYPE(UINT64, uint64_t) CAPNP_DECLARE_TYPE(FLOAT32, float) CAPNP_DECLARE_TYPE(FLOAT64, double) CAPNP_DECLARE_TYPE(TEXT, Text) CAPNP_DECLARE_TYPE(DATA, Data) CAPNP_DECLARE_TYPE(LIST, DynamicList) CAPNP_DECLARE_TYPE(STRUCT, DynamicStruct) CAPNP_DECLARE_TYPE(INTERFACE, DynamicCapability) CAPNP_DECLARE_TYPE(ENUM, DynamicEnum) CAPNP_DECLARE_TYPE(ANY_POINTER, AnyPointer) #undef CAPNP_DECLARE_TYPE // CAPNP_DECLARE_TYPE(Void) causes gcc 4.7 to segfault. If I do it manually and remove the // ReaderFor<> and BuilderFor<> wrappers, it works. template <> struct DynamicValue::Reader::AsImpl { static Void apply(const Reader& reader); }; template <> struct DynamicValue::Builder::AsImpl { static Void apply(Builder& builder); }; template struct DynamicValue::Reader::AsImpl { static T apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static T apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Reader apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Builder apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Reader apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Builder apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Client apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Client apply(Builder& builder) { return builder.as().as(); } }; template <> struct DynamicValue::Reader::AsImpl { static DynamicValue::Reader apply(const Reader& reader) { return reader; } }; template <> struct DynamicValue::Builder::AsImpl { static DynamicValue::Builder apply(Builder& builder) { return builder; } }; inline DynamicValue::Pipeline::Pipeline(std::nullptr_t n): type(UNKNOWN) {} inline DynamicValue::Pipeline::Pipeline(DynamicStruct::Pipeline&& value) : type(STRUCT), structValue(kj::mv(value)) {} inline DynamicValue::Pipeline::Pipeline(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} template struct DynamicValue::Pipeline::AsImpl { static typename T::Pipeline apply(Pipeline& pipeline) { return pipeline.releaseAs().releaseAs(); } }; template struct DynamicValue::Pipeline::AsImpl { static typename T::Client apply(Pipeline& pipeline) { return pipeline.releaseAs().releaseAs(); } }; template <> struct DynamicValue::Pipeline::AsImpl { static PipelineFor apply(Pipeline& pipeline); }; template <> struct DynamicValue::Pipeline::AsImpl { static PipelineFor apply(Pipeline& pipeline); }; // ------------------------------------------------------------------- template typename T::Reader DynamicStruct::Reader::as() const { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Reader::as() can only convert to struct types."); schema.requireUsableAs(); return typename T::Reader(reader); } template typename T::Builder DynamicStruct::Builder::as() { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Builder::as() can only convert to struct types."); schema.requireUsableAs(); return typename T::Builder(builder); } template <> inline DynamicStruct::Reader DynamicStruct::Reader::as() const { return *this; } template <> inline DynamicStruct::Builder DynamicStruct::Builder::as() { return *this; } inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const { return DynamicStruct::Reader(schema, builder.asReader()); } template <> inline AnyStruct::Reader DynamicStruct::Reader::as() const { return AnyStruct::Reader(reader); } template <> inline AnyStruct::Builder DynamicStruct::Builder::as() { return AnyStruct::Builder(builder); } template typename T::Pipeline DynamicStruct::Pipeline::releaseAs() { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Pipeline::releaseAs() can only convert to struct types."); schema.requireUsableAs(); return typename T::Pipeline(kj::mv(typeless)); } // ------------------------------------------------------------------- template typename T::Reader DynamicList::Reader::as() const { static_assert(kind() == Kind::LIST, "DynamicStruct::Reader::as() can only convert to list types."); schema.requireUsableAs(); return typename T::Reader(reader); } template typename T::Builder DynamicList::Builder::as() { static_assert(kind() == Kind::LIST, "DynamicStruct::Builder::as() can only convert to list types."); schema.requireUsableAs(); return typename T::Builder(builder); } template <> inline DynamicList::Reader DynamicList::Reader::as() const { return *this; } template <> inline DynamicList::Builder DynamicList::Builder::as() { return *this; } template <> inline AnyList::Reader DynamicList::Reader::as() const { return AnyList::Reader(reader); } template <> inline AnyList::Builder DynamicList::Builder::as() { return AnyList::Builder(builder); } // ------------------------------------------------------------------- template inline DynamicCapability::Client::Client(T&& client) : Capability::Client(kj::mv(client)), schema(Schema::from>()) {} template inline DynamicCapability::Client::Client(kj::Own&& server) : Client(server->getSchema(), kj::mv(server)) {} template inline DynamicCapability::Client::Client(InterfaceSchema schema, kj::Own&& server) : Capability::Client(kj::mv(server)), schema(schema) {} template typename T::Client DynamicCapability::Client::as() { static_assert(kind() == Kind::INTERFACE, "DynamicCapability::Client::as() can only convert to interface types."); schema.requireUsableAs(); return typename T::Client(hook->addRef()); } template typename T::Client DynamicCapability::Client::releaseAs() { static_assert(kind() == Kind::INTERFACE, "DynamicCapability::Client::as() can only convert to interface types."); schema.requireUsableAs(); return typename T::Client(kj::mv(hook)); } inline CallContext::CallContext( CallContextHook& hook, StructSchema paramType, StructSchema resultType) : hook(&hook), paramType(paramType), resultType(resultType) {} inline DynamicStruct::Reader CallContext::getParams() { return hook->getParams().getAs(paramType); } inline void CallContext::releaseParams() { hook->releaseParams(); } inline DynamicStruct::Builder CallContext::getResults( kj::Maybe sizeHint) { return hook->getResults(sizeHint).getAs(resultType); } inline DynamicStruct::Builder CallContext::initResults( kj::Maybe sizeHint) { return hook->getResults(sizeHint).initAs(resultType); } inline void CallContext::setResults(DynamicStruct::Reader value) { hook->getResults(value.totalSize()).setAs(value); } inline void CallContext::adoptResults(Orphan&& value) { hook->getResults(MessageSize { 0, 0 }).adopt(kj::mv(value)); } inline Orphanage CallContext::getResultsOrphanage( kj::Maybe sizeHint) { return Orphanage::getForMessageContaining(hook->getResults(sizeHint)); } template inline kj::Promise CallContext::tailCall( Request&& tailRequest) { return hook->tailCall(kj::mv(tailRequest.hook)); } inline void CallContext::allowCancellation() { hook->allowCancellation(); } template <> inline DynamicCapability::Client Capability::Client::castAs( InterfaceSchema schema) { return DynamicCapability::Client(schema, hook->addRef()); } // ------------------------------------------------------------------- template ReaderFor ConstSchema::as() const { return DynamicValue::Reader(*this).as(); } } // namespace capnp #endif // CAPNP_DYNAMIC_H_