cannam@132: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@132: // Licensed under the MIT License:
cannam@132: //
cannam@132: // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@132: // of this software and associated documentation files (the "Software"), to deal
cannam@132: // in the Software without restriction, including without limitation the rights
cannam@132: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@132: // copies of the Software, and to permit persons to whom the Software is
cannam@132: // furnished to do so, subject to the following conditions:
cannam@132: //
cannam@132: // The above copyright notice and this permission notice shall be included in
cannam@132: // all copies or substantial portions of the Software.
cannam@132: //
cannam@132: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@132: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@132: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@132: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@132: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@132: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@132: // THE SOFTWARE.
cannam@132: 
cannam@132: // This file is included form all generated headers.
cannam@132: 
cannam@132: #ifndef CAPNP_GENERATED_HEADER_SUPPORT_H_
cannam@132: #define CAPNP_GENERATED_HEADER_SUPPORT_H_
cannam@132: 
cannam@132: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@132: #pragma GCC system_header
cannam@132: #endif
cannam@132: 
cannam@132: #include "layout.h"
cannam@132: #include "list.h"
cannam@132: #include "orphan.h"
cannam@132: #include "pointer-helpers.h"
cannam@132: #include "any.h"
cannam@132: #include <kj/string.h>
cannam@132: #include <kj/string-tree.h>
cannam@132: 
cannam@132: namespace capnp {
cannam@132: 
cannam@132: class MessageBuilder;  // So that it can be declared a friend.
cannam@132: 
cannam@132: template <typename T, Kind k = CAPNP_KIND(T)>
cannam@132: struct ToDynamic_;   // Defined in dynamic.h, needs to be declared as everyone's friend.
cannam@132: 
cannam@132: struct DynamicStruct;  // So that it can be declared a friend.
cannam@132: 
cannam@132: struct Capability;  // To declare brandBindingFor<Capability>()
cannam@132: 
cannam@132: namespace _ {  // private
cannam@132: 
cannam@132: #if !CAPNP_LITE
cannam@132: 
cannam@132: struct RawSchema;
cannam@132: 
cannam@132: struct RawBrandedSchema {
cannam@132:   // Represents a combination of a schema and bindings for its generic parameters.
cannam@132:   //
cannam@132:   // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for
cannam@132:   // every _instance_ of a generic type -- or, at least, every instance that is actually used. For
cannam@132:   // generated-code types, we use template magic to initialize these.
cannam@132: 
cannam@132:   const RawSchema* generic;
cannam@132:   // Generic type which we're branding.
cannam@132: 
cannam@132:   struct Binding {
cannam@132:     uint8_t which;       // Numeric value of one of schema::Type::Which.
cannam@132: 
cannam@132:     bool isImplicitParameter;
cannam@132:     // For AnyPointer, true if it's an implicit method parameter.
cannam@132: 
cannam@132:     uint16_t listDepth;  // Number of times to wrap the base type in List().
cannam@132: 
cannam@132:     uint16_t paramIndex;
cannam@132:     // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter
cannam@132:     // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric
cannam@132:     // value of one of schema::Type::AnyPointer::Unconstrained::Which.
cannam@132: 
cannam@132:     union {
cannam@132:       const RawBrandedSchema* schema;  // for struct, enum, interface
cannam@132:       uint64_t scopeId;                // for AnyPointer, if it's a type parameter
cannam@132:     };
cannam@132: 
cannam@132:     Binding() = default;
cannam@132:     inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema)
cannam@132:         : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0),
cannam@132:           schema(schema) {}
cannam@132:     inline constexpr Binding(uint8_t which, uint16_t listDepth,
cannam@132:                              uint64_t scopeId, uint16_t paramIndex)
cannam@132:         : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex),
cannam@132:           scopeId(scopeId) {}
cannam@132:     inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex)
cannam@132:         : which(which), isImplicitParameter(true), listDepth(listDepth),
cannam@132:           paramIndex(implicitParamIndex), scopeId(0) {}
cannam@132:   };
cannam@132: 
cannam@132:   struct Scope {
cannam@132:     uint64_t typeId;
cannam@132:     // Type ID whose parameters are being bound.
cannam@132: 
cannam@132:     const Binding* bindings;
cannam@132:     uint bindingCount;
cannam@132:     // Bindings for those parameters.
cannam@132: 
cannam@132:     bool isUnbound;
cannam@132:     // This scope is unbound, in the sense of SchemaLoader::getUnbound().
cannam@132:   };
cannam@132: 
cannam@132:   const Scope* scopes;
cannam@132:   // Array of enclosing scopes for which generic variables have been bound, sorted by type ID.
cannam@132: 
cannam@132:   struct Dependency {
cannam@132:     uint location;
cannam@132:     const RawBrandedSchema* schema;
cannam@132:   };
cannam@132: 
cannam@132:   const Dependency* dependencies;
cannam@132:   // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that
cannam@132:   // are branded are included in this map; if a dependency is missing, use its `defaultBrand`.
cannam@132: 
cannam@132:   uint32_t scopeCount;
cannam@132:   uint32_t dependencyCount;
cannam@132: 
cannam@132:   enum class DepKind {
cannam@132:     // Component of a Dependency::location. Specifies what sort of dependency this is.
cannam@132: 
cannam@132:     INVALID,
cannam@132:     // Mostly defined to ensure that zero is not a valid location.
cannam@132: 
cannam@132:     FIELD,
cannam@132:     // Binding needed for a field's type. The index is the field index (NOT ordinal!).
cannam@132: 
cannam@132:     METHOD_PARAMS,
cannam@132:     // Bindings needed for a method's params type. The index is the method number.
cannam@132: 
cannam@132:     METHOD_RESULTS,
cannam@132:     // Bindings needed for a method's results type. The index is the method ordinal.
cannam@132: 
cannam@132:     SUPERCLASS,
cannam@132:     // Bindings needed for a superclass type. The index is the superclass's index in the
cannam@132:     // "extends" list.
cannam@132: 
cannam@132:     CONST_TYPE
cannam@132:     // Bindings needed for the type of a constant. The index is zero.
cannam@132:   };
cannam@132: 
cannam@132:   static inline uint makeDepLocation(DepKind kind, uint index) {
cannam@132:     // Make a number representing the location of a particular dependency within its parent
cannam@132:     // schema.
cannam@132: 
cannam@132:     return (static_cast<uint>(kind) << 24) | index;
cannam@132:   }
cannam@132: 
cannam@132:   class Initializer {
cannam@132:   public:
cannam@132:     virtual void init(const RawBrandedSchema* generic) const = 0;
cannam@132:   };
cannam@132: 
cannam@132:   const Initializer* lazyInitializer;
cannam@132:   // Lazy initializer, invoked by ensureInitialized().
cannam@132: 
cannam@132:   inline void ensureInitialized() const {
cannam@132:     // Lazy initialization support.  Invoke to ensure that initialization has taken place.  This
cannam@132:     // is required in particular when traversing the dependency list.  RawSchemas for compiled-in
cannam@132:     // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@132: 
cannam@132:     const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@132:     if (i != nullptr) i->init(this);
cannam@132:   }
cannam@132: 
cannam@132:   inline bool isUnbound() const;
cannam@132:   // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case
cannam@132:   // binding lookups need to be handled specially.
cannam@132: };
cannam@132: 
cannam@132: struct RawSchema {
cannam@132:   // The generated code defines a constant RawSchema for every compiled declaration.
cannam@132:   //
cannam@132:   // This is an internal structure which could change in the future.
cannam@132: 
cannam@132:   uint64_t id;
cannam@132: 
cannam@132:   const word* encodedNode;
cannam@132:   // Encoded SchemaNode, readable via readMessageUnchecked<schema::Node>(encodedNode).
cannam@132: 
cannam@132:   uint32_t encodedSize;
cannam@132:   // Size of encodedNode, in words.
cannam@132: 
cannam@132:   const RawSchema* const* dependencies;
cannam@132:   // Pointers to other types on which this one depends, sorted by ID.  The schemas in this table
cannam@132:   // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before
cannam@132:   // using it.
cannam@132:   //
cannam@132:   // TODO(someday):  Make this a hashtable.
cannam@132: 
cannam@132:   const uint16_t* membersByName;
cannam@132:   // Indexes of members sorted by name.  Used to implement name lookup.
cannam@132:   // TODO(someday):  Make this a hashtable.
cannam@132: 
cannam@132:   uint32_t dependencyCount;
cannam@132:   uint32_t memberCount;
cannam@132:   // Sizes of above tables.
cannam@132: 
cannam@132:   const uint16_t* membersByDiscriminant;
cannam@132:   // List of all member indexes ordered by discriminant value.  Those which don't have a
cannam@132:   // discriminant value are listed at the end, in order by ordinal.
cannam@132: 
cannam@132:   const RawSchema* canCastTo;
cannam@132:   // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue
cannam@132:   // with this schema.  This is null for all compiled-in types; it is only set by SchemaLoader on
cannam@132:   // dynamically-loaded types.
cannam@132: 
cannam@132:   class Initializer {
cannam@132:   public:
cannam@132:     virtual void init(const RawSchema* schema) const = 0;
cannam@132:   };
cannam@132: 
cannam@132:   const Initializer* lazyInitializer;
cannam@132:   // Lazy initializer, invoked by ensureInitialized().
cannam@132: 
cannam@132:   inline void ensureInitialized() const {
cannam@132:     // Lazy initialization support.  Invoke to ensure that initialization has taken place.  This
cannam@132:     // is required in particular when traversing the dependency list.  RawSchemas for compiled-in
cannam@132:     // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@132: 
cannam@132:     const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@132:     if (i != nullptr) i->init(this);
cannam@132:   }
cannam@132: 
cannam@132:   RawBrandedSchema defaultBrand;
cannam@132:   // Specifies the brand to use for this schema if no generic parameters have been bound to
cannam@132:   // anything. Generally, in the default brand, all generic parameters are treated as if they were
cannam@132:   // bound to `AnyPointer`.
cannam@132: };
cannam@132: 
cannam@132: inline bool RawBrandedSchema::isUnbound() const {
cannam@132:   // The unbound schema is the only one that has no scopes but is not the default schema.
cannam@132:   return scopeCount == 0 && this != &generic->defaultBrand;
cannam@132: }
cannam@132: 
cannam@132: template <typename T, typename CapnpPrivate = typename T::_capnpPrivate, bool = false>
cannam@132: inline const RawSchema& rawSchema() {
cannam@132:   return *CapnpPrivate::schema;
cannam@132: }
cannam@132: template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@132: inline const RawSchema& rawSchema() {
cannam@132:   return *schemas::EnumInfo<T>::schema;
cannam@132: }
cannam@132: 
cannam@132: template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
cannam@132: inline const RawBrandedSchema& rawBrandedSchema() {
cannam@132:   return *CapnpPrivate::brand;
cannam@132: }
cannam@132: template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@132: inline const RawBrandedSchema& rawBrandedSchema() {
cannam@132:   return schemas::EnumInfo<T>::schema->defaultBrand;
cannam@132: }
cannam@132: 
cannam@132: template <typename TypeTag, typename... Params>
cannam@132: struct ChooseBrand;
cannam@132: // If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a
cannam@132: // specific brand instance. TypeTag is the _capnpPrivate struct for the type in question.
cannam@132: 
cannam@132: template <typename TypeTag>
cannam@132: struct ChooseBrand<TypeTag> {
cannam@132:   // All params were AnyPointer. No specific brand needed.
cannam@132:   static constexpr _::RawBrandedSchema const* brand = &TypeTag::schema->defaultBrand;
cannam@132: };
cannam@132: 
cannam@132: template <typename TypeTag, typename... Rest>
cannam@132: struct ChooseBrand<TypeTag, AnyPointer, Rest...>: public ChooseBrand<TypeTag, Rest...> {};
cannam@132: // The first parameter is AnyPointer, so recurse to check the rest.
cannam@132: 
cannam@132: template <typename TypeTag, typename First, typename... Rest>
cannam@132: struct ChooseBrand<TypeTag, First, Rest...> {
cannam@132:   // At least one parameter is not AnyPointer, so use the specificBrand constant.
cannam@132:   static constexpr _::RawBrandedSchema const* brand = &TypeTag::specificBrand;
cannam@132: };
cannam@132: 
cannam@132: template <typename T, Kind k = kind<T>()>
cannam@132: struct BrandBindingFor_;
cannam@132: 
cannam@132: #define HANDLE_TYPE(Type, which) \
cannam@132:   template <> \
cannam@132:   struct BrandBindingFor_<Type, Kind::PRIMITIVE> { \
cannam@132:     static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \
cannam@132:       return { which, listDepth, nullptr }; \
cannam@132:     } \
cannam@132:   }
cannam@132: HANDLE_TYPE(Void, 0);
cannam@132: HANDLE_TYPE(bool, 1);
cannam@132: HANDLE_TYPE(int8_t, 2);
cannam@132: HANDLE_TYPE(int16_t, 3);
cannam@132: HANDLE_TYPE(int32_t, 4);
cannam@132: HANDLE_TYPE(int64_t, 5);
cannam@132: HANDLE_TYPE(uint8_t, 6);
cannam@132: HANDLE_TYPE(uint16_t, 7);
cannam@132: HANDLE_TYPE(uint32_t, 8);
cannam@132: HANDLE_TYPE(uint64_t, 9);
cannam@132: HANDLE_TYPE(float, 10);
cannam@132: HANDLE_TYPE(double, 11);
cannam@132: #undef HANDLE_TYPE
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<Text, Kind::BLOB> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 12, listDepth, nullptr };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<Data, Kind::BLOB> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 13, listDepth, nullptr };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: struct BrandBindingFor_<List<T>, Kind::LIST> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return BrandBindingFor_<T>::get(listDepth + 1);
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: struct BrandBindingFor_<T, Kind::ENUM> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 15, listDepth, nullptr };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: struct BrandBindingFor_<T, Kind::STRUCT> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 16, listDepth, T::_capnpPrivate::brand };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: struct BrandBindingFor_<T, Kind::INTERFACE> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 17, listDepth, T::_capnpPrivate::brand };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<AnyPointer, Kind::OTHER> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 18, listDepth, 0, 0 };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<AnyStruct, Kind::OTHER> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 18, listDepth, 0, 1 };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<AnyList, Kind::OTHER> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 18, listDepth, 0, 2 };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <>
cannam@132: struct BrandBindingFor_<Capability, Kind::OTHER> {
cannam@132:   static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@132:     return { 18, listDepth, 0, 3 };
cannam@132:   }
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: constexpr RawBrandedSchema::Binding brandBindingFor() {
cannam@132:   return BrandBindingFor_<T>::get(0);
cannam@132: }
cannam@132: 
cannam@132: kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema);
cannam@132: kj::String enumString(uint16_t value, const RawBrandedSchema& schema);
cannam@132: // Declared here so that we can declare inline stringify methods on generated types.
cannam@132: // Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in.
cannam@132: 
cannam@132: template <typename T>
cannam@132: inline kj::StringTree structString(StructReader reader) {
cannam@132:   return structString(reader, rawBrandedSchema<T>());
cannam@132: }
cannam@132: template <typename T>
cannam@132: inline kj::String enumString(T value) {
cannam@132:   return enumString(static_cast<uint16_t>(value), rawBrandedSchema<T>());
cannam@132: }
cannam@132: 
cannam@132: #endif  // !CAPNP_LITE
cannam@132: 
cannam@132: // TODO(cleanup):  Unify ConstStruct and ConstList.
cannam@132: template <typename T>
cannam@132: class ConstStruct {
cannam@132: public:
cannam@132:   ConstStruct() = delete;
cannam@132:   KJ_DISALLOW_COPY(ConstStruct);
cannam@132:   inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {}
cannam@132: 
cannam@132:   inline typename T::Reader get() const {
cannam@132:     return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<T>();
cannam@132:   }
cannam@132: 
cannam@132:   inline operator typename T::Reader() const { return get(); }
cannam@132:   inline typename T::Reader operator*() const { return get(); }
cannam@132:   inline TemporaryPointer<typename T::Reader> operator->() const { return get(); }
cannam@132: 
cannam@132: private:
cannam@132:   const word* ptr;
cannam@132: };
cannam@132: 
cannam@132: template <typename T>
cannam@132: class ConstList {
cannam@132: public:
cannam@132:   ConstList() = delete;
cannam@132:   KJ_DISALLOW_COPY(ConstList);
cannam@132:   inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {}
cannam@132: 
cannam@132:   inline typename List<T>::Reader get() const {
cannam@132:     return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<List<T>>();
cannam@132:   }
cannam@132: 
cannam@132:   inline operator typename List<T>::Reader() const { return get(); }
cannam@132:   inline typename List<T>::Reader operator*() const { return get(); }
cannam@132:   inline TemporaryPointer<typename List<T>::Reader> operator->() const { return get(); }
cannam@132: 
cannam@132: private:
cannam@132:   const word* ptr;
cannam@132: };
cannam@132: 
cannam@132: template <size_t size>
cannam@132: class ConstText {
cannam@132: public:
cannam@132:   ConstText() = delete;
cannam@132:   KJ_DISALLOW_COPY(ConstText);
cannam@132:   inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {}
cannam@132: 
cannam@132:   inline Text::Reader get() const {
cannam@132:     return Text::Reader(reinterpret_cast<const char*>(ptr), size);
cannam@132:   }
cannam@132: 
cannam@132:   inline operator Text::Reader() const { return get(); }
cannam@132:   inline Text::Reader operator*() const { return get(); }
cannam@132:   inline TemporaryPointer<Text::Reader> operator->() const { return get(); }
cannam@132: 
cannam@132:   inline kj::StringPtr toString() const {
cannam@132:     return get();
cannam@132:   }
cannam@132: 
cannam@132: private:
cannam@132:   const word* ptr;
cannam@132: };
cannam@132: 
cannam@132: template <size_t size>
cannam@132: inline kj::StringPtr KJ_STRINGIFY(const ConstText<size>& s) {
cannam@132:   return s.get();
cannam@132: }
cannam@132: 
cannam@132: template <size_t size>
cannam@132: class ConstData {
cannam@132: public:
cannam@132:   ConstData() = delete;
cannam@132:   KJ_DISALLOW_COPY(ConstData);
cannam@132:   inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {}
cannam@132: 
cannam@132:   inline Data::Reader get() const {
cannam@132:     return Data::Reader(reinterpret_cast<const byte*>(ptr), size);
cannam@132:   }
cannam@132: 
cannam@132:   inline operator Data::Reader() const { return get(); }
cannam@132:   inline Data::Reader operator*() const { return get(); }
cannam@132:   inline TemporaryPointer<Data::Reader> operator->() const { return get(); }
cannam@132: 
cannam@132: private:
cannam@132:   const word* ptr;
cannam@132: };
cannam@132: 
cannam@132: template <size_t size>
cannam@132: inline auto KJ_STRINGIFY(const ConstData<size>& s) -> decltype(kj::toCharSequence(s.get())) {
cannam@132:   return kj::toCharSequence(s.get());
cannam@132: }
cannam@132: 
cannam@132: }  // namespace _ (private)
cannam@132: 
cannam@132: template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
cannam@132: inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; }
cannam@132: template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@132: inline constexpr uint64_t typeId() { return id; }
cannam@132: // typeId<MyType>() returns the type ID as defined in the schema.  Works with structs, enums, and
cannam@132: // interfaces.
cannam@132: 
cannam@132: template <typename T>
cannam@132: inline constexpr uint sizeInWords() {
cannam@132:   // Return the size, in words, of a Struct type, if allocated free-standing (not in a list).
cannam@132:   // May be useful for pre-computing space needed in order to precisely allocate messages.
cannam@132: 
cannam@132:   return (WordCount32(_::structSize<T>().data) +
cannam@132:       _::structSize<T>().pointers * WORDS_PER_POINTER) / WORDS;
cannam@132: }
cannam@132: 
cannam@132: }  // namespace capnp
cannam@132: 
cannam@132: #if _MSC_VER
cannam@132: // MSVC doesn't understand floating-point constexpr yet.
cannam@132: //
cannam@132: // TODO(msvc): Remove this hack when MSVC is fixed.
cannam@132: #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value)
cannam@132: #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value
cannam@132: #else
cannam@132: #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value
cannam@132: #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value)
cannam@132: #endif
cannam@132: 
cannam@132: #if CAPNP_LITE
cannam@132: 
cannam@132: #define CAPNP_DECLARE_SCHEMA(id) \
cannam@132:     extern ::capnp::word const* const bp_##id
cannam@132: 
cannam@132: #define CAPNP_DECLARE_ENUM(type, id) \
cannam@132:     inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
cannam@132:       return ::kj::str(static_cast<uint16_t>(value)); \
cannam@132:     } \
cannam@132:     template <> struct EnumInfo<type##_##id> { \
cannam@132:       struct IsEnum; \
cannam@132:       static constexpr uint64_t typeId = 0x##id; \
cannam@132:       static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
cannam@132:     }
cannam@132: 
cannam@132: #if _MSC_VER
cannam@132: // TODO(msvc): MSVC dosen't expect constexprs to have definitions.
cannam@132: #define CAPNP_DEFINE_ENUM(type, id)
cannam@132: #else
cannam@132: #define CAPNP_DEFINE_ENUM(type, id) \
cannam@132:     constexpr uint64_t EnumInfo<type>::typeId
cannam@132: #endif
cannam@132: 
cannam@132: #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
cannam@132:       struct IsStruct; \
cannam@132:       static constexpr uint64_t typeId = 0x##id; \
cannam@132:       static constexpr uint16_t dataWordSize = dataWordSize_; \
cannam@132:       static constexpr uint16_t pointerCount = pointerCount_; \
cannam@132:       static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; }
cannam@132: 
cannam@132: #else  // CAPNP_LITE
cannam@132: 
cannam@132: #define CAPNP_DECLARE_SCHEMA(id) \
cannam@132:     extern ::capnp::word const* const bp_##id; \
cannam@132:     extern const ::capnp::_::RawSchema s_##id
cannam@132: 
cannam@132: #define CAPNP_DECLARE_ENUM(type, id) \
cannam@132:     inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
cannam@132:       return ::capnp::_::enumString(value); \
cannam@132:     } \
cannam@132:     template <> struct EnumInfo<type##_##id> { \
cannam@132:       struct IsEnum; \
cannam@132:       static constexpr uint64_t typeId = 0x##id; \
cannam@132:       static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
cannam@132:       static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \
cannam@132:     }
cannam@132: #define CAPNP_DEFINE_ENUM(type, id) \
cannam@132:     constexpr uint64_t EnumInfo<type>::typeId; \
cannam@132:     constexpr ::capnp::_::RawSchema const* EnumInfo<type>::schema
cannam@132: 
cannam@132: #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
cannam@132:       struct IsStruct; \
cannam@132:       static constexpr uint64_t typeId = 0x##id; \
cannam@132:       static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \
cannam@132:       static constexpr uint16_t dataWordSize = dataWordSize_; \
cannam@132:       static constexpr uint16_t pointerCount = pointerCount_; \
cannam@132:       static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
cannam@132:       static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
cannam@132: 
cannam@132: #define CAPNP_DECLARE_INTERFACE_HEADER(id) \
cannam@132:       struct IsInterface; \
cannam@132:       static constexpr uint64_t typeId = 0x##id; \
cannam@132:       static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \
cannam@132:       static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
cannam@132:       static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
cannam@132: 
cannam@132: #endif  // CAPNP_LITE, else
cannam@132: 
cannam@132: #endif  // CAPNP_GENERATED_HEADER_SUPPORT_H_