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