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