annotate win64-msvc/include/capnp/common.h @ 136:ce0478b62770

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