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 contains types which are intended to help detect incorrect usage at compile cannam@132: // time, but should then be optimized down to basic primitives (usually, integers) by the cannam@132: // compiler. cannam@132: cannam@132: #ifndef CAPNP_COMMON_H_ cannam@132: #define CAPNP_COMMON_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 cannam@132: #include cannam@132: #include cannam@132: #include cannam@132: cannam@132: namespace capnp { cannam@132: cannam@132: #define CAPNP_VERSION_MAJOR 0 cannam@132: #define CAPNP_VERSION_MINOR 6 cannam@132: #define CAPNP_VERSION_MICRO 0 cannam@132: cannam@132: #define CAPNP_VERSION \ cannam@132: (CAPNP_VERSION_MAJOR * 1000000 + CAPNP_VERSION_MINOR * 1000 + CAPNP_VERSION_MICRO) cannam@132: cannam@132: #ifdef _MSC_VER cannam@132: #define CAPNP_LITE 1 cannam@132: // MSVC only supports "lite" mode for now, due to missing C++11 features. cannam@132: #endif cannam@132: cannam@132: #ifndef CAPNP_LITE cannam@132: #define CAPNP_LITE 0 cannam@132: #endif cannam@132: cannam@132: typedef unsigned int uint; cannam@132: cannam@132: struct Void { cannam@132: // Type used for Void fields. Using C++'s "void" type creates a bunch of issues since it behaves cannam@132: // differently from other types. cannam@132: cannam@132: inline constexpr bool operator==(Void other) const { return true; } cannam@132: inline constexpr bool operator!=(Void other) const { return false; } cannam@132: }; cannam@132: cannam@132: static constexpr Void VOID = Void(); cannam@132: // Constant value for `Void`, which is an empty struct. cannam@132: cannam@132: inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; } cannam@132: cannam@132: struct Text; cannam@132: struct Data; cannam@132: cannam@132: enum class Kind: uint8_t { cannam@132: PRIMITIVE, cannam@132: BLOB, cannam@132: ENUM, cannam@132: STRUCT, cannam@132: UNION, cannam@132: INTERFACE, cannam@132: LIST, cannam@132: cannam@132: OTHER cannam@132: // Some other type which is often a type parameter to Cap'n Proto templates, but which needs cannam@132: // special handling. This includes types like AnyPointer, Dynamic*, etc. cannam@132: }; cannam@132: cannam@132: enum class Style: uint8_t { cannam@132: PRIMITIVE, cannam@132: POINTER, // other than struct cannam@132: STRUCT, cannam@132: CAPABILITY cannam@132: }; cannam@132: cannam@132: enum class ElementSize: uint8_t { cannam@132: // Size of a list element. cannam@132: cannam@132: VOID = 0, cannam@132: BIT = 1, cannam@132: BYTE = 2, cannam@132: TWO_BYTES = 3, cannam@132: FOUR_BYTES = 4, cannam@132: EIGHT_BYTES = 5, cannam@132: cannam@132: POINTER = 6, cannam@132: cannam@132: INLINE_COMPOSITE = 7 cannam@132: }; cannam@132: cannam@132: enum class PointerType { cannam@132: // Various wire types a pointer field can take cannam@132: cannam@132: NULL_, cannam@132: // Should be NULL, but that's #defined in stddef.h cannam@132: cannam@132: STRUCT, cannam@132: LIST, cannam@132: CAPABILITY cannam@132: }; cannam@132: cannam@132: namespace schemas { cannam@132: cannam@132: template cannam@132: struct EnumInfo; cannam@132: cannam@132: } // namespace schemas cannam@132: cannam@132: namespace _ { // private cannam@132: cannam@132: template struct Kind_; cannam@132: cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@132: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@132: cannam@132: template struct Kind_> { cannam@132: static constexpr Kind kind = Kind::STRUCT; cannam@132: }; cannam@132: template struct Kind_> { cannam@132: static constexpr Kind kind = Kind::INTERFACE; cannam@132: }; cannam@132: template struct Kind_::IsEnum>> { cannam@132: static constexpr Kind kind = Kind::ENUM; cannam@132: }; cannam@132: cannam@132: } // namespace _ (private) cannam@132: cannam@132: template ::kind> cannam@132: inline constexpr Kind kind() { cannam@132: // This overload of kind() matches types which have a Kind_ specialization. cannam@132: cannam@132: return k; cannam@132: } cannam@132: cannam@132: #if CAPNP_LITE cannam@132: cannam@132: #define CAPNP_KIND(T) ::capnp::_::Kind_::kind cannam@132: // Avoid constexpr methods in lite mode (MSVC is bad at constexpr). cannam@132: cannam@132: #else // CAPNP_LITE cannam@132: cannam@132: #define CAPNP_KIND(T) ::capnp::kind() cannam@132: // Use this macro rather than kind() in any code which must work in lite mode. cannam@132: cannam@132: template ()> cannam@132: inline constexpr Style style() { cannam@132: return k == Kind::PRIMITIVE || k == Kind::ENUM ? Style::PRIMITIVE cannam@132: : k == Kind::STRUCT ? Style::STRUCT cannam@132: : k == Kind::INTERFACE ? Style::CAPABILITY : Style::POINTER; cannam@132: } cannam@132: cannam@132: #endif // CAPNP_LITE, else cannam@132: cannam@132: template cannam@132: struct List; cannam@132: cannam@132: #if _MSC_VER cannam@132: cannam@132: template cannam@132: struct List {}; cannam@132: // For some reason, without this declaration, MSVC will error out on some uses of List cannam@132: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@132: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@132: // it. cannam@132: cannam@132: #endif cannam@132: cannam@132: template struct ListElementType_; cannam@132: template struct ListElementType_> { typedef T Type; }; cannam@132: template using ListElementType = typename ListElementType_::Type; cannam@132: cannam@132: namespace _ { // private cannam@132: template struct Kind_> { cannam@132: static constexpr Kind kind = Kind::LIST; cannam@132: }; cannam@132: } // namespace _ (private) cannam@132: cannam@132: template struct ReaderFor_ { typedef typename T::Reader Type; }; cannam@132: template struct ReaderFor_ { typedef T Type; }; cannam@132: template struct ReaderFor_ { typedef T Type; }; cannam@132: template struct ReaderFor_ { typedef typename T::Client Type; }; cannam@132: template using ReaderFor = typename ReaderFor_::Type; cannam@132: // The type returned by List::Reader::operator[]. cannam@132: cannam@132: template struct BuilderFor_ { typedef typename T::Builder Type; }; cannam@132: template struct BuilderFor_ { typedef T Type; }; cannam@132: template struct BuilderFor_ { typedef T Type; }; cannam@132: template struct BuilderFor_ { typedef typename T::Client Type; }; cannam@132: template using BuilderFor = typename BuilderFor_::Type; cannam@132: // The type returned by List::Builder::operator[]. cannam@132: cannam@132: template struct PipelineFor_ { typedef typename T::Pipeline Type;}; cannam@132: template struct PipelineFor_ { typedef typename T::Client Type; }; cannam@132: template using PipelineFor = typename PipelineFor_::Type; cannam@132: cannam@132: template struct TypeIfEnum_; cannam@132: template struct TypeIfEnum_ { typedef T Type; }; cannam@132: cannam@132: template cannam@132: using TypeIfEnum = typename TypeIfEnum_>::Type; cannam@132: cannam@132: template cannam@132: using FromReader = typename kj::Decay::Reads; cannam@132: // FromReader = MyType (for any Cap'n Proto type). cannam@132: cannam@132: template cannam@132: using FromBuilder = typename kj::Decay::Builds; cannam@132: // FromBuilder = MyType (for any Cap'n Proto type). cannam@132: cannam@132: template cannam@132: using FromPipeline = typename kj::Decay::Pipelines; cannam@132: // FromBuilder = MyType (for any Cap'n Proto type). cannam@132: cannam@132: template cannam@132: using FromClient = typename kj::Decay::Calls; cannam@132: // FromReader = MyType (for any Cap'n Proto interface type). cannam@132: cannam@132: template cannam@132: using FromServer = typename kj::Decay::Serves; cannam@132: // FromBuilder = MyType (for any Cap'n Proto interface type). cannam@132: cannam@132: template cannam@132: struct FromAny_; cannam@132: cannam@132: template cannam@132: struct FromAny_>> { cannam@132: using Type = FromReader; cannam@132: }; cannam@132: cannam@132: template cannam@132: struct FromAny_>> { cannam@132: using Type = FromBuilder; cannam@132: }; cannam@132: cannam@132: template cannam@132: struct FromAny_>> { cannam@132: using Type = FromPipeline; cannam@132: }; cannam@132: cannam@132: // Note that T::Client is covered by FromReader cannam@132: cannam@132: template cannam@132: struct FromAny_, kj::VoidSfinae>> { cannam@132: using Type = FromServer; cannam@132: }; cannam@132: cannam@132: template cannam@132: struct FromAny_::kind == Kind::PRIMITIVE || _::Kind_::kind == Kind::ENUM>> { cannam@132: // TODO(msvc): Ideally the EnableIf condition would be `style() == Style::PRIMITIVE`, but MSVC cannam@132: // cannot yet use style() in this constexpr context. cannam@132: cannam@132: using Type = kj::Decay; cannam@132: }; cannam@132: cannam@132: template cannam@132: using FromAny = typename FromAny_::Type; cannam@132: // Given any Cap'n Proto value type as an input, return the Cap'n Proto base type. That is: cannam@132: // cannam@132: // Foo::Reader -> Foo cannam@132: // Foo::Builder -> Foo cannam@132: // Foo::Pipeline -> Foo cannam@132: // Foo::Client -> Foo cannam@132: // Own -> Foo cannam@132: // uint32_t -> uint32_t cannam@132: cannam@132: namespace _ { // private cannam@132: cannam@132: template cannam@132: struct PointerHelpers; cannam@132: cannam@132: #if _MSC_VER cannam@132: cannam@132: template cannam@132: struct PointerHelpers {}; cannam@132: // For some reason, without this declaration, MSVC will error out on some uses of PointerHelpers cannam@132: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@132: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@132: // it. cannam@132: cannam@132: #endif cannam@132: cannam@132: } // namespace _ (private) cannam@132: cannam@132: struct MessageSize { cannam@132: // Size of a message. Every struct type has a method `.totalSize()` that returns this. cannam@132: uint64_t wordCount; cannam@132: uint capCount; cannam@132: }; cannam@132: cannam@132: // ======================================================================================= cannam@132: // Raw memory types and measures cannam@132: cannam@132: using kj::byte; cannam@132: cannam@132: class word { uint64_t content KJ_UNUSED_MEMBER; KJ_DISALLOW_COPY(word); public: word() = default; }; cannam@132: // word is an opaque type with size of 64 bits. This type is useful only to make pointer cannam@132: // arithmetic clearer. Since the contents are private, the only way to access them is to first cannam@132: // reinterpret_cast to some other pointer type. cannam@132: // cannam@132: // Copying is disallowed because you should always use memcpy(). Otherwise, you may run afoul of cannam@132: // aliasing rules. cannam@132: // cannam@132: // A pointer of type word* should always be word-aligned even if won't actually be dereferenced as cannam@132: // that type. cannam@132: cannam@132: static_assert(sizeof(byte) == 1, "uint8_t is not one byte?"); cannam@132: static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?"); cannam@132: cannam@132: #if CAPNP_DEBUG_TYPES cannam@132: // Set CAPNP_DEBUG_TYPES to 1 to use kj::Quantity for "count" types. Otherwise, plain integers are cannam@132: // used. All the code should still operate exactly the same, we just lose compile-time checking. cannam@132: // Note that this will also change symbol names, so it's important that the library and any clients cannam@132: // be compiled with the same setting here. cannam@132: // cannam@132: // We disable this by default to reduce symbol name size and avoid any possibility of the compiler cannam@132: // failing to fully-optimize the types, but anyone modifying Cap'n Proto itself should enable this cannam@132: // during development and testing. cannam@132: cannam@132: namespace _ { class BitLabel; class ElementLabel; struct WirePointer; } cannam@132: cannam@132: typedef kj::Quantity BitCount; cannam@132: typedef kj::Quantity BitCount8; cannam@132: typedef kj::Quantity BitCount16; cannam@132: typedef kj::Quantity BitCount32; cannam@132: typedef kj::Quantity BitCount64; cannam@132: cannam@132: typedef kj::Quantity ByteCount; cannam@132: typedef kj::Quantity ByteCount8; cannam@132: typedef kj::Quantity ByteCount16; cannam@132: typedef kj::Quantity ByteCount32; cannam@132: typedef kj::Quantity ByteCount64; cannam@132: cannam@132: typedef kj::Quantity WordCount; cannam@132: typedef kj::Quantity WordCount8; cannam@132: typedef kj::Quantity WordCount16; cannam@132: typedef kj::Quantity WordCount32; cannam@132: typedef kj::Quantity WordCount64; cannam@132: cannam@132: typedef kj::Quantity ElementCount; cannam@132: typedef kj::Quantity ElementCount8; cannam@132: typedef kj::Quantity ElementCount16; cannam@132: typedef kj::Quantity ElementCount32; cannam@132: typedef kj::Quantity ElementCount64; cannam@132: cannam@132: typedef kj::Quantity WirePointerCount; cannam@132: typedef kj::Quantity WirePointerCount8; cannam@132: typedef kj::Quantity WirePointerCount16; cannam@132: typedef kj::Quantity WirePointerCount32; cannam@132: typedef kj::Quantity WirePointerCount64; cannam@132: cannam@132: template cannam@132: inline constexpr U* operator+(U* ptr, kj::Quantity offset) { cannam@132: return ptr + offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr const U* operator+(const U* ptr, kj::Quantity offset) { cannam@132: return ptr + offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr U* operator+=(U*& ptr, kj::Quantity offset) { cannam@132: return ptr = ptr + offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr const U* operator+=(const U*& ptr, kj::Quantity offset) { cannam@132: return ptr = ptr + offset / kj::unit>(); cannam@132: } cannam@132: cannam@132: template cannam@132: inline constexpr U* operator-(U* ptr, kj::Quantity offset) { cannam@132: return ptr - offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr const U* operator-(const U* ptr, kj::Quantity offset) { cannam@132: return ptr - offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr U* operator-=(U*& ptr, kj::Quantity offset) { cannam@132: return ptr = ptr - offset / kj::unit>(); cannam@132: } cannam@132: template cannam@132: inline constexpr const U* operator-=(const U*& ptr, kj::Quantity offset) { cannam@132: return ptr = ptr - offset / kj::unit>(); cannam@132: } cannam@132: cannam@132: #else cannam@132: cannam@132: typedef uint BitCount; cannam@132: typedef uint8_t BitCount8; cannam@132: typedef uint16_t BitCount16; cannam@132: typedef uint32_t BitCount32; cannam@132: typedef uint64_t BitCount64; cannam@132: cannam@132: typedef uint ByteCount; cannam@132: typedef uint8_t ByteCount8; cannam@132: typedef uint16_t ByteCount16; cannam@132: typedef uint32_t ByteCount32; cannam@132: typedef uint64_t ByteCount64; cannam@132: cannam@132: typedef uint WordCount; cannam@132: typedef uint8_t WordCount8; cannam@132: typedef uint16_t WordCount16; cannam@132: typedef uint32_t WordCount32; cannam@132: typedef uint64_t WordCount64; cannam@132: cannam@132: typedef uint ElementCount; cannam@132: typedef uint8_t ElementCount8; cannam@132: typedef uint16_t ElementCount16; cannam@132: typedef uint32_t ElementCount32; cannam@132: typedef uint64_t ElementCount64; cannam@132: cannam@132: typedef uint WirePointerCount; cannam@132: typedef uint8_t WirePointerCount8; cannam@132: typedef uint16_t WirePointerCount16; cannam@132: typedef uint32_t WirePointerCount32; cannam@132: typedef uint64_t WirePointerCount64; cannam@132: cannam@132: #endif cannam@132: cannam@132: constexpr BitCount BITS = kj::unit(); cannam@132: constexpr ByteCount BYTES = kj::unit(); cannam@132: constexpr WordCount WORDS = kj::unit(); cannam@132: constexpr ElementCount ELEMENTS = kj::unit(); cannam@132: constexpr WirePointerCount POINTERS = kj::unit(); cannam@132: cannam@132: // GCC 4.7 actually gives unused warnings on these constants in opt mode... cannam@132: constexpr auto BITS_PER_BYTE KJ_UNUSED = 8 * BITS / BYTES; cannam@132: constexpr auto BITS_PER_WORD KJ_UNUSED = 64 * BITS / WORDS; cannam@132: constexpr auto BYTES_PER_WORD KJ_UNUSED = 8 * BYTES / WORDS; cannam@132: cannam@132: constexpr auto BITS_PER_POINTER KJ_UNUSED = 64 * BITS / POINTERS; cannam@132: constexpr auto BYTES_PER_POINTER KJ_UNUSED = 8 * BYTES / POINTERS; cannam@132: constexpr auto WORDS_PER_POINTER KJ_UNUSED = 1 * WORDS / POINTERS; cannam@132: cannam@132: constexpr WordCount POINTER_SIZE_IN_WORDS = 1 * POINTERS * WORDS_PER_POINTER; cannam@132: cannam@132: template cannam@132: inline KJ_CONSTEXPR() decltype(BYTES / ELEMENTS) bytesPerElement() { cannam@132: return sizeof(T) * BYTES / ELEMENTS; cannam@132: } cannam@132: cannam@132: template cannam@132: inline KJ_CONSTEXPR() decltype(BITS / ELEMENTS) bitsPerElement() { cannam@132: return sizeof(T) * 8 * BITS / ELEMENTS; cannam@132: } cannam@132: cannam@132: inline constexpr ByteCount intervalLength(const byte* a, const byte* b) { cannam@132: return uint(b - a) * BYTES; cannam@132: } cannam@132: inline constexpr WordCount intervalLength(const word* a, const word* b) { cannam@132: return uint(b - a) * WORDS; cannam@132: } cannam@132: cannam@132: } // namespace capnp cannam@132: cannam@132: #endif // CAPNP_COMMON_H_