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