cannam@49: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@49: // Licensed under the MIT License: cannam@49: // cannam@49: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@49: // of this software and associated documentation files (the "Software"), to deal cannam@49: // in the Software without restriction, including without limitation the rights cannam@49: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@49: // copies of the Software, and to permit persons to whom the Software is cannam@49: // furnished to do so, subject to the following conditions: cannam@49: // cannam@49: // The above copyright notice and this permission notice shall be included in cannam@49: // all copies or substantial portions of the Software. cannam@49: // cannam@49: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@49: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@49: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@49: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@49: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@49: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@49: // THE SOFTWARE. cannam@49: cannam@49: // This file contains types which are intended to help detect incorrect usage at compile cannam@49: // time, but should then be optimized down to basic primitives (usually, integers) by the cannam@49: // compiler. cannam@49: cannam@49: #ifndef CAPNP_COMMON_H_ cannam@49: #define CAPNP_COMMON_H_ cannam@49: cannam@49: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@49: #pragma GCC system_header cannam@49: #endif cannam@49: cannam@49: #include cannam@49: #include cannam@49: #include cannam@49: #include cannam@49: cannam@49: namespace capnp { cannam@49: cannam@49: #define CAPNP_VERSION_MAJOR 0 cannam@49: #define CAPNP_VERSION_MINOR 6 cannam@49: #define CAPNP_VERSION_MICRO 0 cannam@49: cannam@49: #define CAPNP_VERSION \ cannam@49: (CAPNP_VERSION_MAJOR * 1000000 + CAPNP_VERSION_MINOR * 1000 + CAPNP_VERSION_MICRO) cannam@49: cannam@49: #ifdef _MSC_VER cannam@49: #define CAPNP_LITE 1 cannam@49: // MSVC only supports "lite" mode for now, due to missing C++11 features. cannam@49: #endif cannam@49: cannam@49: #ifndef CAPNP_LITE cannam@49: #define CAPNP_LITE 0 cannam@49: #endif cannam@49: cannam@49: typedef unsigned int uint; cannam@49: cannam@49: struct Void { cannam@49: // Type used for Void fields. Using C++'s "void" type creates a bunch of issues since it behaves cannam@49: // differently from other types. cannam@49: cannam@49: inline constexpr bool operator==(Void other) const { return true; } cannam@49: inline constexpr bool operator!=(Void other) const { return false; } cannam@49: }; cannam@49: cannam@49: static constexpr Void VOID = Void(); cannam@49: // Constant value for `Void`, which is an empty struct. cannam@49: cannam@49: inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; } cannam@49: cannam@49: struct Text; cannam@49: struct Data; cannam@49: cannam@49: enum class Kind: uint8_t { cannam@49: PRIMITIVE, cannam@49: BLOB, cannam@49: ENUM, cannam@49: STRUCT, cannam@49: UNION, cannam@49: INTERFACE, cannam@49: LIST, cannam@49: cannam@49: OTHER cannam@49: // Some other type which is often a type parameter to Cap'n Proto templates, but which needs cannam@49: // special handling. This includes types like AnyPointer, Dynamic*, etc. cannam@49: }; cannam@49: cannam@49: enum class Style: uint8_t { cannam@49: PRIMITIVE, cannam@49: POINTER, // other than struct cannam@49: STRUCT, cannam@49: CAPABILITY cannam@49: }; cannam@49: cannam@49: enum class ElementSize: uint8_t { cannam@49: // Size of a list element. cannam@49: cannam@49: VOID = 0, cannam@49: BIT = 1, cannam@49: BYTE = 2, cannam@49: TWO_BYTES = 3, cannam@49: FOUR_BYTES = 4, cannam@49: EIGHT_BYTES = 5, cannam@49: cannam@49: POINTER = 6, cannam@49: cannam@49: INLINE_COMPOSITE = 7 cannam@49: }; cannam@49: cannam@49: enum class PointerType { cannam@49: // Various wire types a pointer field can take cannam@49: cannam@49: NULL_, cannam@49: // Should be NULL, but that's #defined in stddef.h cannam@49: cannam@49: STRUCT, cannam@49: LIST, cannam@49: CAPABILITY cannam@49: }; cannam@49: cannam@49: namespace schemas { cannam@49: cannam@49: template cannam@49: struct EnumInfo; cannam@49: cannam@49: } // namespace schemas cannam@49: cannam@49: namespace _ { // private cannam@49: cannam@49: template struct Kind_; cannam@49: cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@49: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@49: cannam@49: template struct Kind_> { cannam@49: static constexpr Kind kind = Kind::STRUCT; cannam@49: }; cannam@49: template struct Kind_> { cannam@49: static constexpr Kind kind = Kind::INTERFACE; cannam@49: }; cannam@49: template struct Kind_::IsEnum>> { cannam@49: static constexpr Kind kind = Kind::ENUM; cannam@49: }; cannam@49: cannam@49: } // namespace _ (private) cannam@49: cannam@49: template ::kind> cannam@49: inline constexpr Kind kind() { cannam@49: // This overload of kind() matches types which have a Kind_ specialization. cannam@49: cannam@49: return k; cannam@49: } cannam@49: cannam@49: #if CAPNP_LITE cannam@49: cannam@49: #define CAPNP_KIND(T) ::capnp::_::Kind_::kind cannam@49: // Avoid constexpr methods in lite mode (MSVC is bad at constexpr). cannam@49: cannam@49: #else // CAPNP_LITE cannam@49: cannam@49: #define CAPNP_KIND(T) ::capnp::kind() cannam@49: // Use this macro rather than kind() in any code which must work in lite mode. cannam@49: cannam@49: template ()> cannam@49: inline constexpr Style style() { cannam@49: return k == Kind::PRIMITIVE || k == Kind::ENUM ? Style::PRIMITIVE cannam@49: : k == Kind::STRUCT ? Style::STRUCT cannam@49: : k == Kind::INTERFACE ? Style::CAPABILITY : Style::POINTER; cannam@49: } cannam@49: cannam@49: #endif // CAPNP_LITE, else cannam@49: cannam@49: template cannam@49: struct List; cannam@49: cannam@49: #if _MSC_VER cannam@49: cannam@49: template cannam@49: struct List {}; cannam@49: // For some reason, without this declaration, MSVC will error out on some uses of List cannam@49: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@49: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@49: // it. cannam@49: cannam@49: #endif cannam@49: cannam@49: template struct ListElementType_; cannam@49: template struct ListElementType_> { typedef T Type; }; cannam@49: template using ListElementType = typename ListElementType_::Type; cannam@49: cannam@49: namespace _ { // private cannam@49: template struct Kind_> { cannam@49: static constexpr Kind kind = Kind::LIST; cannam@49: }; cannam@49: } // namespace _ (private) cannam@49: cannam@49: template struct ReaderFor_ { typedef typename T::Reader Type; }; cannam@49: template struct ReaderFor_ { typedef T Type; }; cannam@49: template struct ReaderFor_ { typedef T Type; }; cannam@49: template struct ReaderFor_ { typedef typename T::Client Type; }; cannam@49: template using ReaderFor = typename ReaderFor_::Type; cannam@49: // The type returned by List::Reader::operator[]. cannam@49: cannam@49: template struct BuilderFor_ { typedef typename T::Builder Type; }; cannam@49: template struct BuilderFor_ { typedef T Type; }; cannam@49: template struct BuilderFor_ { typedef T Type; }; cannam@49: template struct BuilderFor_ { typedef typename T::Client Type; }; cannam@49: template using BuilderFor = typename BuilderFor_::Type; cannam@49: // The type returned by List::Builder::operator[]. cannam@49: cannam@49: template struct PipelineFor_ { typedef typename T::Pipeline Type;}; cannam@49: template struct PipelineFor_ { typedef typename T::Client Type; }; cannam@49: template using PipelineFor = typename PipelineFor_::Type; cannam@49: cannam@49: template struct TypeIfEnum_; cannam@49: template struct TypeIfEnum_ { typedef T Type; }; cannam@49: cannam@49: template cannam@49: using TypeIfEnum = typename TypeIfEnum_>::Type; cannam@49: cannam@49: template cannam@49: using FromReader = typename kj::Decay::Reads; cannam@49: // FromReader = MyType (for any Cap'n Proto type). cannam@49: cannam@49: template cannam@49: using FromBuilder = typename kj::Decay::Builds; cannam@49: // FromBuilder = MyType (for any Cap'n Proto type). cannam@49: cannam@49: template cannam@49: using FromPipeline = typename kj::Decay::Pipelines; cannam@49: // FromBuilder = MyType (for any Cap'n Proto type). cannam@49: cannam@49: template cannam@49: using FromClient = typename kj::Decay::Calls; cannam@49: // FromReader = MyType (for any Cap'n Proto interface type). cannam@49: cannam@49: template cannam@49: using FromServer = typename kj::Decay::Serves; cannam@49: // FromBuilder = MyType (for any Cap'n Proto interface type). cannam@49: cannam@49: template cannam@49: struct FromAny_; cannam@49: cannam@49: template cannam@49: struct FromAny_>> { cannam@49: using Type = FromReader; cannam@49: }; cannam@49: cannam@49: template cannam@49: struct FromAny_>> { cannam@49: using Type = FromBuilder; cannam@49: }; cannam@49: cannam@49: template cannam@49: struct FromAny_>> { cannam@49: using Type = FromPipeline; cannam@49: }; cannam@49: cannam@49: // Note that T::Client is covered by FromReader cannam@49: cannam@49: template cannam@49: struct FromAny_, kj::VoidSfinae>> { cannam@49: using Type = FromServer; cannam@49: }; cannam@49: cannam@49: template cannam@49: struct FromAny_::kind == Kind::PRIMITIVE || _::Kind_::kind == Kind::ENUM>> { cannam@49: // TODO(msvc): Ideally the EnableIf condition would be `style() == Style::PRIMITIVE`, but MSVC cannam@49: // cannot yet use style() in this constexpr context. cannam@49: cannam@49: using Type = kj::Decay; cannam@49: }; cannam@49: cannam@49: template cannam@49: using FromAny = typename FromAny_::Type; cannam@49: // Given any Cap'n Proto value type as an input, return the Cap'n Proto base type. That is: cannam@49: // cannam@49: // Foo::Reader -> Foo cannam@49: // Foo::Builder -> Foo cannam@49: // Foo::Pipeline -> Foo cannam@49: // Foo::Client -> Foo cannam@49: // Own -> Foo cannam@49: // uint32_t -> uint32_t cannam@49: cannam@49: namespace _ { // private cannam@49: cannam@49: template cannam@49: struct PointerHelpers; cannam@49: cannam@49: #if _MSC_VER cannam@49: cannam@49: template cannam@49: struct PointerHelpers {}; cannam@49: // For some reason, without this declaration, MSVC will error out on some uses of PointerHelpers cannam@49: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@49: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@49: // it. cannam@49: cannam@49: #endif cannam@49: cannam@49: } // namespace _ (private) cannam@49: cannam@49: struct MessageSize { cannam@49: // Size of a message. Every struct type has a method `.totalSize()` that returns this. cannam@49: uint64_t wordCount; cannam@49: uint capCount; cannam@49: }; cannam@49: cannam@49: // ======================================================================================= cannam@49: // Raw memory types and measures cannam@49: cannam@49: using kj::byte; cannam@49: cannam@49: class word { uint64_t content KJ_UNUSED_MEMBER; KJ_DISALLOW_COPY(word); public: word() = default; }; cannam@49: // word is an opaque type with size of 64 bits. This type is useful only to make pointer cannam@49: // arithmetic clearer. Since the contents are private, the only way to access them is to first cannam@49: // reinterpret_cast to some other pointer type. cannam@49: // cannam@49: // Copying is disallowed because you should always use memcpy(). Otherwise, you may run afoul of cannam@49: // aliasing rules. cannam@49: // cannam@49: // A pointer of type word* should always be word-aligned even if won't actually be dereferenced as cannam@49: // that type. cannam@49: cannam@49: static_assert(sizeof(byte) == 1, "uint8_t is not one byte?"); cannam@49: static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?"); cannam@49: cannam@49: #if CAPNP_DEBUG_TYPES cannam@49: // Set CAPNP_DEBUG_TYPES to 1 to use kj::Quantity for "count" types. Otherwise, plain integers are cannam@49: // used. All the code should still operate exactly the same, we just lose compile-time checking. cannam@49: // Note that this will also change symbol names, so it's important that the library and any clients cannam@49: // be compiled with the same setting here. cannam@49: // cannam@49: // We disable this by default to reduce symbol name size and avoid any possibility of the compiler cannam@49: // failing to fully-optimize the types, but anyone modifying Cap'n Proto itself should enable this cannam@49: // during development and testing. cannam@49: cannam@49: namespace _ { class BitLabel; class ElementLabel; struct WirePointer; } cannam@49: cannam@49: typedef kj::Quantity BitCount; cannam@49: typedef kj::Quantity BitCount8; cannam@49: typedef kj::Quantity BitCount16; cannam@49: typedef kj::Quantity BitCount32; cannam@49: typedef kj::Quantity BitCount64; cannam@49: cannam@49: typedef kj::Quantity ByteCount; cannam@49: typedef kj::Quantity ByteCount8; cannam@49: typedef kj::Quantity ByteCount16; cannam@49: typedef kj::Quantity ByteCount32; cannam@49: typedef kj::Quantity ByteCount64; cannam@49: cannam@49: typedef kj::Quantity WordCount; cannam@49: typedef kj::Quantity WordCount8; cannam@49: typedef kj::Quantity WordCount16; cannam@49: typedef kj::Quantity WordCount32; cannam@49: typedef kj::Quantity WordCount64; cannam@49: cannam@49: typedef kj::Quantity ElementCount; cannam@49: typedef kj::Quantity ElementCount8; cannam@49: typedef kj::Quantity ElementCount16; cannam@49: typedef kj::Quantity ElementCount32; cannam@49: typedef kj::Quantity ElementCount64; cannam@49: cannam@49: typedef kj::Quantity WirePointerCount; cannam@49: typedef kj::Quantity WirePointerCount8; cannam@49: typedef kj::Quantity WirePointerCount16; cannam@49: typedef kj::Quantity WirePointerCount32; cannam@49: typedef kj::Quantity WirePointerCount64; cannam@49: cannam@49: template cannam@49: inline constexpr U* operator+(U* ptr, kj::Quantity offset) { cannam@49: return ptr + offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr const U* operator+(const U* ptr, kj::Quantity offset) { cannam@49: return ptr + offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr U* operator+=(U*& ptr, kj::Quantity offset) { cannam@49: return ptr = ptr + offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr const U* operator+=(const U*& ptr, kj::Quantity offset) { cannam@49: return ptr = ptr + offset / kj::unit>(); cannam@49: } cannam@49: cannam@49: template cannam@49: inline constexpr U* operator-(U* ptr, kj::Quantity offset) { cannam@49: return ptr - offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr const U* operator-(const U* ptr, kj::Quantity offset) { cannam@49: return ptr - offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr U* operator-=(U*& ptr, kj::Quantity offset) { cannam@49: return ptr = ptr - offset / kj::unit>(); cannam@49: } cannam@49: template cannam@49: inline constexpr const U* operator-=(const U*& ptr, kj::Quantity offset) { cannam@49: return ptr = ptr - offset / kj::unit>(); cannam@49: } cannam@49: cannam@49: #else cannam@49: cannam@49: typedef uint BitCount; cannam@49: typedef uint8_t BitCount8; cannam@49: typedef uint16_t BitCount16; cannam@49: typedef uint32_t BitCount32; cannam@49: typedef uint64_t BitCount64; cannam@49: cannam@49: typedef uint ByteCount; cannam@49: typedef uint8_t ByteCount8; cannam@49: typedef uint16_t ByteCount16; cannam@49: typedef uint32_t ByteCount32; cannam@49: typedef uint64_t ByteCount64; cannam@49: cannam@49: typedef uint WordCount; cannam@49: typedef uint8_t WordCount8; cannam@49: typedef uint16_t WordCount16; cannam@49: typedef uint32_t WordCount32; cannam@49: typedef uint64_t WordCount64; cannam@49: cannam@49: typedef uint ElementCount; cannam@49: typedef uint8_t ElementCount8; cannam@49: typedef uint16_t ElementCount16; cannam@49: typedef uint32_t ElementCount32; cannam@49: typedef uint64_t ElementCount64; cannam@49: cannam@49: typedef uint WirePointerCount; cannam@49: typedef uint8_t WirePointerCount8; cannam@49: typedef uint16_t WirePointerCount16; cannam@49: typedef uint32_t WirePointerCount32; cannam@49: typedef uint64_t WirePointerCount64; cannam@49: cannam@49: #endif cannam@49: cannam@49: constexpr BitCount BITS = kj::unit(); cannam@49: constexpr ByteCount BYTES = kj::unit(); cannam@49: constexpr WordCount WORDS = kj::unit(); cannam@49: constexpr ElementCount ELEMENTS = kj::unit(); cannam@49: constexpr WirePointerCount POINTERS = kj::unit(); cannam@49: cannam@49: // GCC 4.7 actually gives unused warnings on these constants in opt mode... cannam@49: constexpr auto BITS_PER_BYTE KJ_UNUSED = 8 * BITS / BYTES; cannam@49: constexpr auto BITS_PER_WORD KJ_UNUSED = 64 * BITS / WORDS; cannam@49: constexpr auto BYTES_PER_WORD KJ_UNUSED = 8 * BYTES / WORDS; cannam@49: cannam@49: constexpr auto BITS_PER_POINTER KJ_UNUSED = 64 * BITS / POINTERS; cannam@49: constexpr auto BYTES_PER_POINTER KJ_UNUSED = 8 * BYTES / POINTERS; cannam@49: constexpr auto WORDS_PER_POINTER KJ_UNUSED = 1 * WORDS / POINTERS; cannam@49: cannam@49: constexpr WordCount POINTER_SIZE_IN_WORDS = 1 * POINTERS * WORDS_PER_POINTER; cannam@49: cannam@49: template cannam@49: inline KJ_CONSTEXPR() decltype(BYTES / ELEMENTS) bytesPerElement() { cannam@49: return sizeof(T) * BYTES / ELEMENTS; cannam@49: } cannam@49: cannam@49: template cannam@49: inline KJ_CONSTEXPR() decltype(BITS / ELEMENTS) bitsPerElement() { cannam@49: return sizeof(T) * 8 * BITS / ELEMENTS; cannam@49: } cannam@49: cannam@49: inline constexpr ByteCount intervalLength(const byte* a, const byte* b) { cannam@49: return uint(b - a) * BYTES; cannam@49: } cannam@49: inline constexpr WordCount intervalLength(const word* a, const word* b) { cannam@49: return uint(b - a) * WORDS; cannam@49: } cannam@49: cannam@49: } // namespace capnp cannam@49: cannam@49: #endif // CAPNP_COMMON_H_