annotate win32-mingw/include/capnp/common.h @ 50:37d53a7e8262

Headers for KJ/Capnp Win32
author Chris Cannam
date Wed, 26 Oct 2016 13:18:45 +0100
parents
children eccd51b72864
rev   line source
Chris@50 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@50 2 // Licensed under the MIT License:
Chris@50 3 //
Chris@50 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@50 5 // of this software and associated documentation files (the "Software"), to deal
Chris@50 6 // in the Software without restriction, including without limitation the rights
Chris@50 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@50 8 // copies of the Software, and to permit persons to whom the Software is
Chris@50 9 // furnished to do so, subject to the following conditions:
Chris@50 10 //
Chris@50 11 // The above copyright notice and this permission notice shall be included in
Chris@50 12 // all copies or substantial portions of the Software.
Chris@50 13 //
Chris@50 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@50 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@50 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@50 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@50 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@50 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@50 20 // THE SOFTWARE.
Chris@50 21
Chris@50 22 // This file contains types which are intended to help detect incorrect usage at compile
Chris@50 23 // time, but should then be optimized down to basic primitives (usually, integers) by the
Chris@50 24 // compiler.
Chris@50 25
Chris@50 26 #ifndef CAPNP_COMMON_H_
Chris@50 27 #define CAPNP_COMMON_H_
Chris@50 28
Chris@50 29 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@50 30 #pragma GCC system_header
Chris@50 31 #endif
Chris@50 32
Chris@50 33 #include <kj/units.h>
Chris@50 34 #include <inttypes.h>
Chris@50 35 #include <kj/string.h>
Chris@50 36 #include <kj/memory.h>
Chris@50 37
Chris@50 38 namespace capnp {
Chris@50 39
Chris@50 40 #define CAPNP_VERSION_MAJOR 0
Chris@50 41 #define CAPNP_VERSION_MINOR 6
Chris@50 42 #define CAPNP_VERSION_MICRO 0
Chris@50 43
Chris@50 44 #define CAPNP_VERSION \
Chris@50 45 (CAPNP_VERSION_MAJOR * 1000000 + CAPNP_VERSION_MINOR * 1000 + CAPNP_VERSION_MICRO)
Chris@50 46
Chris@50 47 #ifdef _MSC_VER
Chris@50 48 #define CAPNP_LITE 1
Chris@50 49 // MSVC only supports "lite" mode for now, due to missing C++11 features.
Chris@50 50 #endif
Chris@50 51
Chris@50 52 #ifndef CAPNP_LITE
Chris@50 53 #define CAPNP_LITE 0
Chris@50 54 #endif
Chris@50 55
Chris@50 56 typedef unsigned int uint;
Chris@50 57
Chris@50 58 struct Void {
Chris@50 59 // Type used for Void fields. Using C++'s "void" type creates a bunch of issues since it behaves
Chris@50 60 // differently from other types.
Chris@50 61
Chris@50 62 inline constexpr bool operator==(Void other) const { return true; }
Chris@50 63 inline constexpr bool operator!=(Void other) const { return false; }
Chris@50 64 };
Chris@50 65
Chris@50 66 static constexpr Void VOID = Void();
Chris@50 67 // Constant value for `Void`, which is an empty struct.
Chris@50 68
Chris@50 69 inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; }
Chris@50 70
Chris@50 71 struct Text;
Chris@50 72 struct Data;
Chris@50 73
Chris@50 74 enum class Kind: uint8_t {
Chris@50 75 PRIMITIVE,
Chris@50 76 BLOB,
Chris@50 77 ENUM,
Chris@50 78 STRUCT,
Chris@50 79 UNION,
Chris@50 80 INTERFACE,
Chris@50 81 LIST,
Chris@50 82
Chris@50 83 OTHER
Chris@50 84 // Some other type which is often a type parameter to Cap'n Proto templates, but which needs
Chris@50 85 // special handling. This includes types like AnyPointer, Dynamic*, etc.
Chris@50 86 };
Chris@50 87
Chris@50 88 enum class Style: uint8_t {
Chris@50 89 PRIMITIVE,
Chris@50 90 POINTER, // other than struct
Chris@50 91 STRUCT,
Chris@50 92 CAPABILITY
Chris@50 93 };
Chris@50 94
Chris@50 95 enum class ElementSize: uint8_t {
Chris@50 96 // Size of a list element.
Chris@50 97
Chris@50 98 VOID = 0,
Chris@50 99 BIT = 1,
Chris@50 100 BYTE = 2,
Chris@50 101 TWO_BYTES = 3,
Chris@50 102 FOUR_BYTES = 4,
Chris@50 103 EIGHT_BYTES = 5,
Chris@50 104
Chris@50 105 POINTER = 6,
Chris@50 106
Chris@50 107 INLINE_COMPOSITE = 7
Chris@50 108 };
Chris@50 109
Chris@50 110 enum class PointerType {
Chris@50 111 // Various wire types a pointer field can take
Chris@50 112
Chris@50 113 NULL_,
Chris@50 114 // Should be NULL, but that's #defined in stddef.h
Chris@50 115
Chris@50 116 STRUCT,
Chris@50 117 LIST,
Chris@50 118 CAPABILITY
Chris@50 119 };
Chris@50 120
Chris@50 121 namespace schemas {
Chris@50 122
Chris@50 123 template <typename T>
Chris@50 124 struct EnumInfo;
Chris@50 125
Chris@50 126 } // namespace schemas
Chris@50 127
Chris@50 128 namespace _ { // private
Chris@50 129
Chris@50 130 template <typename T, typename = void> struct Kind_;
Chris@50 131
Chris@50 132 template <> struct Kind_<Void> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 133 template <> struct Kind_<bool> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 134 template <> struct Kind_<int8_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 135 template <> struct Kind_<int16_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 136 template <> struct Kind_<int32_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 137 template <> struct Kind_<int64_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 138 template <> struct Kind_<uint8_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 139 template <> struct Kind_<uint16_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 140 template <> struct Kind_<uint32_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 141 template <> struct Kind_<uint64_t> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 142 template <> struct Kind_<float> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 143 template <> struct Kind_<double> { static constexpr Kind kind = Kind::PRIMITIVE; };
Chris@50 144 template <> struct Kind_<Text> { static constexpr Kind kind = Kind::BLOB; };
Chris@50 145 template <> struct Kind_<Data> { static constexpr Kind kind = Kind::BLOB; };
Chris@50 146
Chris@50 147 template <typename T> struct Kind_<T, kj::VoidSfinae<typename T::_capnpPrivate::IsStruct>> {
Chris@50 148 static constexpr Kind kind = Kind::STRUCT;
Chris@50 149 };
Chris@50 150 template <typename T> struct Kind_<T, kj::VoidSfinae<typename T::_capnpPrivate::IsInterface>> {
Chris@50 151 static constexpr Kind kind = Kind::INTERFACE;
Chris@50 152 };
Chris@50 153 template <typename T> struct Kind_<T, kj::VoidSfinae<typename schemas::EnumInfo<T>::IsEnum>> {
Chris@50 154 static constexpr Kind kind = Kind::ENUM;
Chris@50 155 };
Chris@50 156
Chris@50 157 } // namespace _ (private)
Chris@50 158
Chris@50 159 template <typename T, Kind k = _::Kind_<T>::kind>
Chris@50 160 inline constexpr Kind kind() {
Chris@50 161 // This overload of kind() matches types which have a Kind_ specialization.
Chris@50 162
Chris@50 163 return k;
Chris@50 164 }
Chris@50 165
Chris@50 166 #if CAPNP_LITE
Chris@50 167
Chris@50 168 #define CAPNP_KIND(T) ::capnp::_::Kind_<T>::kind
Chris@50 169 // Avoid constexpr methods in lite mode (MSVC is bad at constexpr).
Chris@50 170
Chris@50 171 #else // CAPNP_LITE
Chris@50 172
Chris@50 173 #define CAPNP_KIND(T) ::capnp::kind<T>()
Chris@50 174 // Use this macro rather than kind<T>() in any code which must work in lite mode.
Chris@50 175
Chris@50 176 template <typename T, Kind k = kind<T>()>
Chris@50 177 inline constexpr Style style() {
Chris@50 178 return k == Kind::PRIMITIVE || k == Kind::ENUM ? Style::PRIMITIVE
Chris@50 179 : k == Kind::STRUCT ? Style::STRUCT
Chris@50 180 : k == Kind::INTERFACE ? Style::CAPABILITY : Style::POINTER;
Chris@50 181 }
Chris@50 182
Chris@50 183 #endif // CAPNP_LITE, else
Chris@50 184
Chris@50 185 template <typename T, Kind k = CAPNP_KIND(T)>
Chris@50 186 struct List;
Chris@50 187
Chris@50 188 #if _MSC_VER
Chris@50 189
Chris@50 190 template <typename T, Kind k>
Chris@50 191 struct List {};
Chris@50 192 // For some reason, without this declaration, MSVC will error out on some uses of List
Chris@50 193 // claiming that "T" -- as used in the default initializer for the second template param, "k" --
Chris@50 194 // is not defined. I do not understand this error, but adding this empty default declaration fixes
Chris@50 195 // it.
Chris@50 196
Chris@50 197 #endif
Chris@50 198
Chris@50 199 template <typename T> struct ListElementType_;
Chris@50 200 template <typename T> struct ListElementType_<List<T>> { typedef T Type; };
Chris@50 201 template <typename T> using ListElementType = typename ListElementType_<T>::Type;
Chris@50 202
Chris@50 203 namespace _ { // private
Chris@50 204 template <typename T, Kind k> struct Kind_<List<T, k>> {
Chris@50 205 static constexpr Kind kind = Kind::LIST;
Chris@50 206 };
Chris@50 207 } // namespace _ (private)
Chris@50 208
Chris@50 209 template <typename T, Kind k = CAPNP_KIND(T)> struct ReaderFor_ { typedef typename T::Reader Type; };
Chris@50 210 template <typename T> struct ReaderFor_<T, Kind::PRIMITIVE> { typedef T Type; };
Chris@50 211 template <typename T> struct ReaderFor_<T, Kind::ENUM> { typedef T Type; };
Chris@50 212 template <typename T> struct ReaderFor_<T, Kind::INTERFACE> { typedef typename T::Client Type; };
Chris@50 213 template <typename T> using ReaderFor = typename ReaderFor_<T>::Type;
Chris@50 214 // The type returned by List<T>::Reader::operator[].
Chris@50 215
Chris@50 216 template <typename T, Kind k = CAPNP_KIND(T)> struct BuilderFor_ { typedef typename T::Builder Type; };
Chris@50 217 template <typename T> struct BuilderFor_<T, Kind::PRIMITIVE> { typedef T Type; };
Chris@50 218 template <typename T> struct BuilderFor_<T, Kind::ENUM> { typedef T Type; };
Chris@50 219 template <typename T> struct BuilderFor_<T, Kind::INTERFACE> { typedef typename T::Client Type; };
Chris@50 220 template <typename T> using BuilderFor = typename BuilderFor_<T>::Type;
Chris@50 221 // The type returned by List<T>::Builder::operator[].
Chris@50 222
Chris@50 223 template <typename T, Kind k = CAPNP_KIND(T)> struct PipelineFor_ { typedef typename T::Pipeline Type;};
Chris@50 224 template <typename T> struct PipelineFor_<T, Kind::INTERFACE> { typedef typename T::Client Type; };
Chris@50 225 template <typename T> using PipelineFor = typename PipelineFor_<T>::Type;
Chris@50 226
Chris@50 227 template <typename T, Kind k = CAPNP_KIND(T)> struct TypeIfEnum_;
Chris@50 228 template <typename T> struct TypeIfEnum_<T, Kind::ENUM> { typedef T Type; };
Chris@50 229
Chris@50 230 template <typename T>
Chris@50 231 using TypeIfEnum = typename TypeIfEnum_<kj::Decay<T>>::Type;
Chris@50 232
Chris@50 233 template <typename T>
Chris@50 234 using FromReader = typename kj::Decay<T>::Reads;
Chris@50 235 // FromReader<MyType::Reader> = MyType (for any Cap'n Proto type).
Chris@50 236
Chris@50 237 template <typename T>
Chris@50 238 using FromBuilder = typename kj::Decay<T>::Builds;
Chris@50 239 // FromBuilder<MyType::Builder> = MyType (for any Cap'n Proto type).
Chris@50 240
Chris@50 241 template <typename T>
Chris@50 242 using FromPipeline = typename kj::Decay<T>::Pipelines;
Chris@50 243 // FromBuilder<MyType::Pipeline> = MyType (for any Cap'n Proto type).
Chris@50 244
Chris@50 245 template <typename T>
Chris@50 246 using FromClient = typename kj::Decay<T>::Calls;
Chris@50 247 // FromReader<MyType::Client> = MyType (for any Cap'n Proto interface type).
Chris@50 248
Chris@50 249 template <typename T>
Chris@50 250 using FromServer = typename kj::Decay<T>::Serves;
Chris@50 251 // FromBuilder<MyType::Server> = MyType (for any Cap'n Proto interface type).
Chris@50 252
Chris@50 253 template <typename T, typename = void>
Chris@50 254 struct FromAny_;
Chris@50 255
Chris@50 256 template <typename T>
Chris@50 257 struct FromAny_<T, kj::VoidSfinae<FromReader<T>>> {
Chris@50 258 using Type = FromReader<T>;
Chris@50 259 };
Chris@50 260
Chris@50 261 template <typename T>
Chris@50 262 struct FromAny_<T, kj::VoidSfinae<FromBuilder<T>>> {
Chris@50 263 using Type = FromBuilder<T>;
Chris@50 264 };
Chris@50 265
Chris@50 266 template <typename T>
Chris@50 267 struct FromAny_<T, kj::VoidSfinae<FromPipeline<T>>> {
Chris@50 268 using Type = FromPipeline<T>;
Chris@50 269 };
Chris@50 270
Chris@50 271 // Note that T::Client is covered by FromReader
Chris@50 272
Chris@50 273 template <typename T>
Chris@50 274 struct FromAny_<kj::Own<T>, kj::VoidSfinae<FromServer<T>>> {
Chris@50 275 using Type = FromServer<T>;
Chris@50 276 };
Chris@50 277
Chris@50 278 template <typename T>
Chris@50 279 struct FromAny_<T,
Chris@50 280 kj::EnableIf<_::Kind_<T>::kind == Kind::PRIMITIVE || _::Kind_<T>::kind == Kind::ENUM>> {
Chris@50 281 // TODO(msvc): Ideally the EnableIf condition would be `style<T>() == Style::PRIMITIVE`, but MSVC
Chris@50 282 // cannot yet use style<T>() in this constexpr context.
Chris@50 283
Chris@50 284 using Type = kj::Decay<T>;
Chris@50 285 };
Chris@50 286
Chris@50 287 template <typename T>
Chris@50 288 using FromAny = typename FromAny_<T>::Type;
Chris@50 289 // Given any Cap'n Proto value type as an input, return the Cap'n Proto base type. That is:
Chris@50 290 //
Chris@50 291 // Foo::Reader -> Foo
Chris@50 292 // Foo::Builder -> Foo
Chris@50 293 // Foo::Pipeline -> Foo
Chris@50 294 // Foo::Client -> Foo
Chris@50 295 // Own<Foo::Server> -> Foo
Chris@50 296 // uint32_t -> uint32_t
Chris@50 297
Chris@50 298 namespace _ { // private
Chris@50 299
Chris@50 300 template <typename T, Kind k = CAPNP_KIND(T)>
Chris@50 301 struct PointerHelpers;
Chris@50 302
Chris@50 303 #if _MSC_VER
Chris@50 304
Chris@50 305 template <typename T, Kind k>
Chris@50 306 struct PointerHelpers {};
Chris@50 307 // For some reason, without this declaration, MSVC will error out on some uses of PointerHelpers
Chris@50 308 // claiming that "T" -- as used in the default initializer for the second template param, "k" --
Chris@50 309 // is not defined. I do not understand this error, but adding this empty default declaration fixes
Chris@50 310 // it.
Chris@50 311
Chris@50 312 #endif
Chris@50 313
Chris@50 314 } // namespace _ (private)
Chris@50 315
Chris@50 316 struct MessageSize {
Chris@50 317 // Size of a message. Every struct type has a method `.totalSize()` that returns this.
Chris@50 318 uint64_t wordCount;
Chris@50 319 uint capCount;
Chris@50 320 };
Chris@50 321
Chris@50 322 // =======================================================================================
Chris@50 323 // Raw memory types and measures
Chris@50 324
Chris@50 325 using kj::byte;
Chris@50 326
Chris@50 327 class word { uint64_t content KJ_UNUSED_MEMBER; KJ_DISALLOW_COPY(word); public: word() = default; };
Chris@50 328 // word is an opaque type with size of 64 bits. This type is useful only to make pointer
Chris@50 329 // arithmetic clearer. Since the contents are private, the only way to access them is to first
Chris@50 330 // reinterpret_cast to some other pointer type.
Chris@50 331 //
Chris@50 332 // Copying is disallowed because you should always use memcpy(). Otherwise, you may run afoul of
Chris@50 333 // aliasing rules.
Chris@50 334 //
Chris@50 335 // A pointer of type word* should always be word-aligned even if won't actually be dereferenced as
Chris@50 336 // that type.
Chris@50 337
Chris@50 338 static_assert(sizeof(byte) == 1, "uint8_t is not one byte?");
Chris@50 339 static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?");
Chris@50 340
Chris@50 341 #if CAPNP_DEBUG_TYPES
Chris@50 342 // Set CAPNP_DEBUG_TYPES to 1 to use kj::Quantity for "count" types. Otherwise, plain integers are
Chris@50 343 // used. All the code should still operate exactly the same, we just lose compile-time checking.
Chris@50 344 // Note that this will also change symbol names, so it's important that the library and any clients
Chris@50 345 // be compiled with the same setting here.
Chris@50 346 //
Chris@50 347 // We disable this by default to reduce symbol name size and avoid any possibility of the compiler
Chris@50 348 // failing to fully-optimize the types, but anyone modifying Cap'n Proto itself should enable this
Chris@50 349 // during development and testing.
Chris@50 350
Chris@50 351 namespace _ { class BitLabel; class ElementLabel; struct WirePointer; }
Chris@50 352
Chris@50 353 typedef kj::Quantity<uint, _::BitLabel> BitCount;
Chris@50 354 typedef kj::Quantity<uint8_t, _::BitLabel> BitCount8;
Chris@50 355 typedef kj::Quantity<uint16_t, _::BitLabel> BitCount16;
Chris@50 356 typedef kj::Quantity<uint32_t, _::BitLabel> BitCount32;
Chris@50 357 typedef kj::Quantity<uint64_t, _::BitLabel> BitCount64;
Chris@50 358
Chris@50 359 typedef kj::Quantity<uint, byte> ByteCount;
Chris@50 360 typedef kj::Quantity<uint8_t, byte> ByteCount8;
Chris@50 361 typedef kj::Quantity<uint16_t, byte> ByteCount16;
Chris@50 362 typedef kj::Quantity<uint32_t, byte> ByteCount32;
Chris@50 363 typedef kj::Quantity<uint64_t, byte> ByteCount64;
Chris@50 364
Chris@50 365 typedef kj::Quantity<uint, word> WordCount;
Chris@50 366 typedef kj::Quantity<uint8_t, word> WordCount8;
Chris@50 367 typedef kj::Quantity<uint16_t, word> WordCount16;
Chris@50 368 typedef kj::Quantity<uint32_t, word> WordCount32;
Chris@50 369 typedef kj::Quantity<uint64_t, word> WordCount64;
Chris@50 370
Chris@50 371 typedef kj::Quantity<uint, _::ElementLabel> ElementCount;
Chris@50 372 typedef kj::Quantity<uint8_t, _::ElementLabel> ElementCount8;
Chris@50 373 typedef kj::Quantity<uint16_t, _::ElementLabel> ElementCount16;
Chris@50 374 typedef kj::Quantity<uint32_t, _::ElementLabel> ElementCount32;
Chris@50 375 typedef kj::Quantity<uint64_t, _::ElementLabel> ElementCount64;
Chris@50 376
Chris@50 377 typedef kj::Quantity<uint, _::WirePointer> WirePointerCount;
Chris@50 378 typedef kj::Quantity<uint8_t, _::WirePointer> WirePointerCount8;
Chris@50 379 typedef kj::Quantity<uint16_t, _::WirePointer> WirePointerCount16;
Chris@50 380 typedef kj::Quantity<uint32_t, _::WirePointer> WirePointerCount32;
Chris@50 381 typedef kj::Quantity<uint64_t, _::WirePointer> WirePointerCount64;
Chris@50 382
Chris@50 383 template <typename T, typename U>
Chris@50 384 inline constexpr U* operator+(U* ptr, kj::Quantity<T, U> offset) {
Chris@50 385 return ptr + offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 386 }
Chris@50 387 template <typename T, typename U>
Chris@50 388 inline constexpr const U* operator+(const U* ptr, kj::Quantity<T, U> offset) {
Chris@50 389 return ptr + offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 390 }
Chris@50 391 template <typename T, typename U>
Chris@50 392 inline constexpr U* operator+=(U*& ptr, kj::Quantity<T, U> offset) {
Chris@50 393 return ptr = ptr + offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 394 }
Chris@50 395 template <typename T, typename U>
Chris@50 396 inline constexpr const U* operator+=(const U*& ptr, kj::Quantity<T, U> offset) {
Chris@50 397 return ptr = ptr + offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 398 }
Chris@50 399
Chris@50 400 template <typename T, typename U>
Chris@50 401 inline constexpr U* operator-(U* ptr, kj::Quantity<T, U> offset) {
Chris@50 402 return ptr - offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 403 }
Chris@50 404 template <typename T, typename U>
Chris@50 405 inline constexpr const U* operator-(const U* ptr, kj::Quantity<T, U> offset) {
Chris@50 406 return ptr - offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 407 }
Chris@50 408 template <typename T, typename U>
Chris@50 409 inline constexpr U* operator-=(U*& ptr, kj::Quantity<T, U> offset) {
Chris@50 410 return ptr = ptr - offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 411 }
Chris@50 412 template <typename T, typename U>
Chris@50 413 inline constexpr const U* operator-=(const U*& ptr, kj::Quantity<T, U> offset) {
Chris@50 414 return ptr = ptr - offset / kj::unit<kj::Quantity<T, U>>();
Chris@50 415 }
Chris@50 416
Chris@50 417 #else
Chris@50 418
Chris@50 419 typedef uint BitCount;
Chris@50 420 typedef uint8_t BitCount8;
Chris@50 421 typedef uint16_t BitCount16;
Chris@50 422 typedef uint32_t BitCount32;
Chris@50 423 typedef uint64_t BitCount64;
Chris@50 424
Chris@50 425 typedef uint ByteCount;
Chris@50 426 typedef uint8_t ByteCount8;
Chris@50 427 typedef uint16_t ByteCount16;
Chris@50 428 typedef uint32_t ByteCount32;
Chris@50 429 typedef uint64_t ByteCount64;
Chris@50 430
Chris@50 431 typedef uint WordCount;
Chris@50 432 typedef uint8_t WordCount8;
Chris@50 433 typedef uint16_t WordCount16;
Chris@50 434 typedef uint32_t WordCount32;
Chris@50 435 typedef uint64_t WordCount64;
Chris@50 436
Chris@50 437 typedef uint ElementCount;
Chris@50 438 typedef uint8_t ElementCount8;
Chris@50 439 typedef uint16_t ElementCount16;
Chris@50 440 typedef uint32_t ElementCount32;
Chris@50 441 typedef uint64_t ElementCount64;
Chris@50 442
Chris@50 443 typedef uint WirePointerCount;
Chris@50 444 typedef uint8_t WirePointerCount8;
Chris@50 445 typedef uint16_t WirePointerCount16;
Chris@50 446 typedef uint32_t WirePointerCount32;
Chris@50 447 typedef uint64_t WirePointerCount64;
Chris@50 448
Chris@50 449 #endif
Chris@50 450
Chris@50 451 constexpr BitCount BITS = kj::unit<BitCount>();
Chris@50 452 constexpr ByteCount BYTES = kj::unit<ByteCount>();
Chris@50 453 constexpr WordCount WORDS = kj::unit<WordCount>();
Chris@50 454 constexpr ElementCount ELEMENTS = kj::unit<ElementCount>();
Chris@50 455 constexpr WirePointerCount POINTERS = kj::unit<WirePointerCount>();
Chris@50 456
Chris@50 457 // GCC 4.7 actually gives unused warnings on these constants in opt mode...
Chris@50 458 constexpr auto BITS_PER_BYTE KJ_UNUSED = 8 * BITS / BYTES;
Chris@50 459 constexpr auto BITS_PER_WORD KJ_UNUSED = 64 * BITS / WORDS;
Chris@50 460 constexpr auto BYTES_PER_WORD KJ_UNUSED = 8 * BYTES / WORDS;
Chris@50 461
Chris@50 462 constexpr auto BITS_PER_POINTER KJ_UNUSED = 64 * BITS / POINTERS;
Chris@50 463 constexpr auto BYTES_PER_POINTER KJ_UNUSED = 8 * BYTES / POINTERS;
Chris@50 464 constexpr auto WORDS_PER_POINTER KJ_UNUSED = 1 * WORDS / POINTERS;
Chris@50 465
Chris@50 466 constexpr WordCount POINTER_SIZE_IN_WORDS = 1 * POINTERS * WORDS_PER_POINTER;
Chris@50 467
Chris@50 468 template <typename T>
Chris@50 469 inline KJ_CONSTEXPR() decltype(BYTES / ELEMENTS) bytesPerElement() {
Chris@50 470 return sizeof(T) * BYTES / ELEMENTS;
Chris@50 471 }
Chris@50 472
Chris@50 473 template <typename T>
Chris@50 474 inline KJ_CONSTEXPR() decltype(BITS / ELEMENTS) bitsPerElement() {
Chris@50 475 return sizeof(T) * 8 * BITS / ELEMENTS;
Chris@50 476 }
Chris@50 477
Chris@50 478 inline constexpr ByteCount intervalLength(const byte* a, const byte* b) {
Chris@50 479 return uint(b - a) * BYTES;
Chris@50 480 }
Chris@50 481 inline constexpr WordCount intervalLength(const word* a, const word* b) {
Chris@50 482 return uint(b - a) * WORDS;
Chris@50 483 }
Chris@50 484
Chris@50 485 } // namespace capnp
Chris@50 486
Chris@50 487 #endif // CAPNP_COMMON_H_