annotate osx/include/capnp/common.h @ 54:5f67a29f0fc7

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