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