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