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