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