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