cannam@147: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@147: // Licensed under the MIT License: cannam@147: // cannam@147: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: // of this software and associated documentation files (the "Software"), to deal cannam@147: // in the Software without restriction, including without limitation the rights cannam@147: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: // copies of the Software, and to permit persons to whom the Software is cannam@147: // furnished to do so, subject to the following conditions: cannam@147: // cannam@147: // The above copyright notice and this permission notice shall be included in cannam@147: // all copies or substantial portions of the Software. cannam@147: // cannam@147: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: // THE SOFTWARE. cannam@147: cannam@147: // This file is included from all generated headers. cannam@147: cannam@147: #ifndef CAPNP_GENERATED_HEADER_SUPPORT_H_ cannam@147: #define CAPNP_GENERATED_HEADER_SUPPORT_H_ cannam@147: cannam@147: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@147: #pragma GCC system_header cannam@147: #endif cannam@147: cannam@147: #include "raw-schema.h" cannam@147: #include "layout.h" cannam@147: #include "list.h" cannam@147: #include "orphan.h" cannam@147: #include "pointer-helpers.h" cannam@147: #include "any.h" cannam@147: #include cannam@147: #include cannam@147: cannam@147: namespace capnp { cannam@147: cannam@147: class MessageBuilder; // So that it can be declared a friend. cannam@147: cannam@147: template cannam@147: struct ToDynamic_; // Defined in dynamic.h, needs to be declared as everyone's friend. cannam@147: cannam@147: struct DynamicStruct; // So that it can be declared a friend. cannam@147: cannam@147: struct Capability; // To declare brandBindingFor() cannam@147: cannam@147: namespace _ { // private cannam@147: cannam@147: #if !CAPNP_LITE cannam@147: cannam@147: template cannam@147: inline const RawSchema& rawSchema() { cannam@147: return *CapnpPrivate::schema; cannam@147: } cannam@147: template ::typeId> cannam@147: inline const RawSchema& rawSchema() { cannam@147: return *schemas::EnumInfo::schema; cannam@147: } cannam@147: cannam@147: template cannam@147: inline const RawBrandedSchema& rawBrandedSchema() { cannam@147: return *CapnpPrivate::brand(); cannam@147: } cannam@147: template ::typeId> cannam@147: inline const RawBrandedSchema& rawBrandedSchema() { cannam@147: return schemas::EnumInfo::schema->defaultBrand; cannam@147: } cannam@147: cannam@147: template cannam@147: struct ChooseBrand; cannam@147: // If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a cannam@147: // specific brand instance. TypeTag is the _capnpPrivate struct for the type in question. cannam@147: cannam@147: template cannam@147: struct ChooseBrand { cannam@147: // All params were AnyPointer. No specific brand needed. cannam@147: static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::schema->defaultBrand; } cannam@147: }; cannam@147: cannam@147: template cannam@147: struct ChooseBrand: public ChooseBrand {}; cannam@147: // The first parameter is AnyPointer, so recurse to check the rest. cannam@147: cannam@147: template cannam@147: struct ChooseBrand { cannam@147: // At least one parameter is not AnyPointer, so use the specificBrand constant. cannam@147: static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::specificBrand; } cannam@147: }; cannam@147: cannam@147: template ()> cannam@147: struct BrandBindingFor_; cannam@147: cannam@147: #define HANDLE_TYPE(Type, which) \ cannam@147: template <> \ cannam@147: struct BrandBindingFor_ { \ cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \ cannam@147: return { which, listDepth, nullptr }; \ cannam@147: } \ cannam@147: } cannam@147: HANDLE_TYPE(Void, 0); cannam@147: HANDLE_TYPE(bool, 1); cannam@147: HANDLE_TYPE(int8_t, 2); cannam@147: HANDLE_TYPE(int16_t, 3); cannam@147: HANDLE_TYPE(int32_t, 4); cannam@147: HANDLE_TYPE(int64_t, 5); cannam@147: HANDLE_TYPE(uint8_t, 6); cannam@147: HANDLE_TYPE(uint16_t, 7); cannam@147: HANDLE_TYPE(uint32_t, 8); cannam@147: HANDLE_TYPE(uint64_t, 9); cannam@147: HANDLE_TYPE(float, 10); cannam@147: HANDLE_TYPE(double, 11); cannam@147: #undef HANDLE_TYPE cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 12, listDepth, nullptr }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 13, listDepth, nullptr }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: struct BrandBindingFor_, Kind::LIST> { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return BrandBindingFor_::get(listDepth + 1); cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 15, listDepth, nullptr }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 16, listDepth, T::_capnpPrivate::brand() }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 17, listDepth, T::_capnpPrivate::brand() }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 18, listDepth, 0, 0 }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 18, listDepth, 0, 1 }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 18, listDepth, 0, 2 }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct BrandBindingFor_ { cannam@147: static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { cannam@147: return { 18, listDepth, 0, 3 }; cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: constexpr RawBrandedSchema::Binding brandBindingFor() { cannam@147: return BrandBindingFor_::get(0); cannam@147: } cannam@147: cannam@147: kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema); cannam@147: kj::String enumString(uint16_t value, const RawBrandedSchema& schema); cannam@147: // Declared here so that we can declare inline stringify methods on generated types. cannam@147: // Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in. cannam@147: cannam@147: template cannam@147: inline kj::StringTree structString(StructReader reader) { cannam@147: return structString(reader, rawBrandedSchema()); cannam@147: } cannam@147: template cannam@147: inline kj::String enumString(T value) { cannam@147: return enumString(static_cast(value), rawBrandedSchema()); cannam@147: } cannam@147: cannam@147: #endif // !CAPNP_LITE cannam@147: cannam@147: // TODO(cleanup): Unify ConstStruct and ConstList. cannam@147: template cannam@147: class ConstStruct { cannam@147: public: cannam@147: ConstStruct() = delete; cannam@147: KJ_DISALLOW_COPY(ConstStruct); cannam@147: inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {} cannam@147: cannam@147: inline typename T::Reader get() const { cannam@147: return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs(); cannam@147: } cannam@147: cannam@147: inline operator typename T::Reader() const { return get(); } cannam@147: inline typename T::Reader operator*() const { return get(); } cannam@147: inline TemporaryPointer operator->() const { return get(); } cannam@147: cannam@147: private: cannam@147: const word* ptr; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ConstList { cannam@147: public: cannam@147: ConstList() = delete; cannam@147: KJ_DISALLOW_COPY(ConstList); cannam@147: inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {} cannam@147: cannam@147: inline typename List::Reader get() const { cannam@147: return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs>(); cannam@147: } cannam@147: cannam@147: inline operator typename List::Reader() const { return get(); } cannam@147: inline typename List::Reader operator*() const { return get(); } cannam@147: inline TemporaryPointer::Reader> operator->() const { return get(); } cannam@147: cannam@147: private: cannam@147: const word* ptr; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ConstText { cannam@147: public: cannam@147: ConstText() = delete; cannam@147: KJ_DISALLOW_COPY(ConstText); cannam@147: inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {} cannam@147: cannam@147: inline Text::Reader get() const { cannam@147: return Text::Reader(reinterpret_cast(ptr), size); cannam@147: } cannam@147: cannam@147: inline operator Text::Reader() const { return get(); } cannam@147: inline Text::Reader operator*() const { return get(); } cannam@147: inline TemporaryPointer operator->() const { return get(); } cannam@147: cannam@147: inline kj::StringPtr toString() const { cannam@147: return get(); cannam@147: } cannam@147: cannam@147: private: cannam@147: const word* ptr; cannam@147: }; cannam@147: cannam@147: template cannam@147: inline kj::StringPtr KJ_STRINGIFY(const ConstText& s) { cannam@147: return s.get(); cannam@147: } cannam@147: cannam@147: template cannam@147: class ConstData { cannam@147: public: cannam@147: ConstData() = delete; cannam@147: KJ_DISALLOW_COPY(ConstData); cannam@147: inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {} cannam@147: cannam@147: inline Data::Reader get() const { cannam@147: return Data::Reader(reinterpret_cast(ptr), size); cannam@147: } cannam@147: cannam@147: inline operator Data::Reader() const { return get(); } cannam@147: inline Data::Reader operator*() const { return get(); } cannam@147: inline TemporaryPointer operator->() const { return get(); } cannam@147: cannam@147: private: cannam@147: const word* ptr; cannam@147: }; cannam@147: cannam@147: template cannam@147: inline auto KJ_STRINGIFY(const ConstData& s) -> decltype(kj::toCharSequence(s.get())) { cannam@147: return kj::toCharSequence(s.get()); cannam@147: } cannam@147: cannam@147: } // namespace _ (private) cannam@147: cannam@147: template cannam@147: inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; } cannam@147: template ::typeId> cannam@147: inline constexpr uint64_t typeId() { return id; } cannam@147: // typeId() returns the type ID as defined in the schema. Works with structs, enums, and cannam@147: // interfaces. cannam@147: cannam@147: template cannam@147: inline constexpr uint sizeInWords() { cannam@147: // Return the size, in words, of a Struct type, if allocated free-standing (not in a list). cannam@147: // May be useful for pre-computing space needed in order to precisely allocate messages. cannam@147: cannam@147: return unbound((upgradeBound(_::structSize().data) + cannam@147: _::structSize().pointers * WORDS_PER_POINTER) / WORDS); cannam@147: } cannam@147: cannam@147: } // namespace capnp cannam@147: cannam@147: #if _MSC_VER cannam@147: // MSVC doesn't understand floating-point constexpr yet. cannam@147: // cannam@147: // TODO(msvc): Remove this hack when MSVC is fixed. cannam@147: #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) cannam@147: #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value cannam@147: #else cannam@147: #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value cannam@147: #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) cannam@147: #endif cannam@147: cannam@147: #if _MSC_VER cannam@147: // TODO(msvc): A little hack to allow MSVC to use C++14 return type deduction in cases where the cannam@147: // explicit type exposes bugs in the compiler. cannam@147: #define CAPNP_AUTO_IF_MSVC(...) auto cannam@147: #else cannam@147: #define CAPNP_AUTO_IF_MSVC(...) __VA_ARGS__ cannam@147: #endif cannam@147: cannam@147: #if CAPNP_LITE cannam@147: cannam@147: #define CAPNP_DECLARE_SCHEMA(id) \ cannam@147: extern ::capnp::word const* const bp_##id cannam@147: cannam@147: #define CAPNP_DECLARE_ENUM(type, id) \ cannam@147: inline ::kj::String KJ_STRINGIFY(type##_##id value) { \ cannam@147: return ::kj::str(static_cast(value)); \ cannam@147: } \ cannam@147: template <> struct EnumInfo { \ cannam@147: struct IsEnum; \ cannam@147: static constexpr uint64_t typeId = 0x##id; \ cannam@147: static inline ::capnp::word const* encodedSchema() { return bp_##id; } \ cannam@147: } cannam@147: cannam@147: #if _MSC_VER cannam@147: // TODO(msvc): MSVC dosen't expect constexprs to have definitions. cannam@147: #define CAPNP_DEFINE_ENUM(type, id) cannam@147: #else cannam@147: #define CAPNP_DEFINE_ENUM(type, id) \ cannam@147: constexpr uint64_t EnumInfo::typeId cannam@147: #endif cannam@147: cannam@147: #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \ cannam@147: struct IsStruct; \ cannam@147: static constexpr uint64_t typeId = 0x##id; \ cannam@147: static constexpr uint16_t dataWordSize = dataWordSize_; \ cannam@147: static constexpr uint16_t pointerCount = pointerCount_; \ cannam@147: static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } cannam@147: cannam@147: #else // CAPNP_LITE cannam@147: cannam@147: #define CAPNP_DECLARE_SCHEMA(id) \ cannam@147: extern ::capnp::word const* const bp_##id; \ cannam@147: extern const ::capnp::_::RawSchema s_##id cannam@147: cannam@147: #define CAPNP_DECLARE_ENUM(type, id) \ cannam@147: inline ::kj::String KJ_STRINGIFY(type##_##id value) { \ cannam@147: return ::capnp::_::enumString(value); \ cannam@147: } \ cannam@147: template <> struct EnumInfo { \ cannam@147: struct IsEnum; \ cannam@147: static constexpr uint64_t typeId = 0x##id; \ cannam@147: static inline ::capnp::word const* encodedSchema() { return bp_##id; } \ cannam@147: static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \ cannam@147: } cannam@147: #define CAPNP_DEFINE_ENUM(type, id) \ cannam@147: constexpr uint64_t EnumInfo::typeId; \ cannam@147: constexpr ::capnp::_::RawSchema const* EnumInfo::schema cannam@147: cannam@147: #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \ cannam@147: struct IsStruct; \ cannam@147: static constexpr uint64_t typeId = 0x##id; \ cannam@147: static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \ cannam@147: static constexpr uint16_t dataWordSize = dataWordSize_; \ cannam@147: static constexpr uint16_t pointerCount = pointerCount_; \ cannam@147: static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \ cannam@147: static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id; cannam@147: cannam@147: #define CAPNP_DECLARE_INTERFACE_HEADER(id) \ cannam@147: struct IsInterface; \ cannam@147: static constexpr uint64_t typeId = 0x##id; \ cannam@147: static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \ cannam@147: static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \ cannam@147: static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id; cannam@147: cannam@147: #endif // CAPNP_LITE, else cannam@147: cannam@147: #endif // CAPNP_GENERATED_HEADER_SUPPORT_H_