annotate osx/include/capnp/generated-header-support.h @ 57:e56993504470

Fixes and updates for 32-bit Windows build
author Chris Cannam
date Mon, 09 Jan 2017 11:53:06 +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 is included form all generated headers.
cannam@49 23
cannam@49 24 #ifndef CAPNP_GENERATED_HEADER_SUPPORT_H_
cannam@49 25 #define CAPNP_GENERATED_HEADER_SUPPORT_H_
cannam@49 26
cannam@49 27 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@49 28 #pragma GCC system_header
cannam@49 29 #endif
cannam@49 30
cannam@49 31 #include "layout.h"
cannam@49 32 #include "list.h"
cannam@49 33 #include "orphan.h"
cannam@49 34 #include "pointer-helpers.h"
cannam@49 35 #include "any.h"
cannam@49 36 #include <kj/string.h>
cannam@49 37 #include <kj/string-tree.h>
cannam@49 38
cannam@49 39 namespace capnp {
cannam@49 40
cannam@49 41 class MessageBuilder; // So that it can be declared a friend.
cannam@49 42
cannam@49 43 template <typename T, Kind k = CAPNP_KIND(T)>
cannam@49 44 struct ToDynamic_; // Defined in dynamic.h, needs to be declared as everyone's friend.
cannam@49 45
cannam@49 46 struct DynamicStruct; // So that it can be declared a friend.
cannam@49 47
cannam@49 48 struct Capability; // To declare brandBindingFor<Capability>()
cannam@49 49
cannam@49 50 namespace _ { // private
cannam@49 51
cannam@49 52 #if !CAPNP_LITE
cannam@49 53
cannam@49 54 struct RawSchema;
cannam@49 55
cannam@49 56 struct RawBrandedSchema {
cannam@49 57 // Represents a combination of a schema and bindings for its generic parameters.
cannam@49 58 //
cannam@49 59 // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for
cannam@49 60 // every _instance_ of a generic type -- or, at least, every instance that is actually used. For
cannam@49 61 // generated-code types, we use template magic to initialize these.
cannam@49 62
cannam@49 63 const RawSchema* generic;
cannam@49 64 // Generic type which we're branding.
cannam@49 65
cannam@49 66 struct Binding {
cannam@49 67 uint8_t which; // Numeric value of one of schema::Type::Which.
cannam@49 68
cannam@49 69 bool isImplicitParameter;
cannam@49 70 // For AnyPointer, true if it's an implicit method parameter.
cannam@49 71
cannam@49 72 uint16_t listDepth; // Number of times to wrap the base type in List().
cannam@49 73
cannam@49 74 uint16_t paramIndex;
cannam@49 75 // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter
cannam@49 76 // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric
cannam@49 77 // value of one of schema::Type::AnyPointer::Unconstrained::Which.
cannam@49 78
cannam@49 79 union {
cannam@49 80 const RawBrandedSchema* schema; // for struct, enum, interface
cannam@49 81 uint64_t scopeId; // for AnyPointer, if it's a type parameter
cannam@49 82 };
cannam@49 83
cannam@49 84 Binding() = default;
cannam@49 85 inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema)
cannam@49 86 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0),
cannam@49 87 schema(schema) {}
cannam@49 88 inline constexpr Binding(uint8_t which, uint16_t listDepth,
cannam@49 89 uint64_t scopeId, uint16_t paramIndex)
cannam@49 90 : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex),
cannam@49 91 scopeId(scopeId) {}
cannam@49 92 inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex)
cannam@49 93 : which(which), isImplicitParameter(true), listDepth(listDepth),
cannam@49 94 paramIndex(implicitParamIndex), scopeId(0) {}
cannam@49 95 };
cannam@49 96
cannam@49 97 struct Scope {
cannam@49 98 uint64_t typeId;
cannam@49 99 // Type ID whose parameters are being bound.
cannam@49 100
cannam@49 101 const Binding* bindings;
cannam@49 102 uint bindingCount;
cannam@49 103 // Bindings for those parameters.
cannam@49 104
cannam@49 105 bool isUnbound;
cannam@49 106 // This scope is unbound, in the sense of SchemaLoader::getUnbound().
cannam@49 107 };
cannam@49 108
cannam@49 109 const Scope* scopes;
cannam@49 110 // Array of enclosing scopes for which generic variables have been bound, sorted by type ID.
cannam@49 111
cannam@49 112 struct Dependency {
cannam@49 113 uint location;
cannam@49 114 const RawBrandedSchema* schema;
cannam@49 115 };
cannam@49 116
cannam@49 117 const Dependency* dependencies;
cannam@49 118 // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that
cannam@49 119 // are branded are included in this map; if a dependency is missing, use its `defaultBrand`.
cannam@49 120
cannam@49 121 uint32_t scopeCount;
cannam@49 122 uint32_t dependencyCount;
cannam@49 123
cannam@49 124 enum class DepKind {
cannam@49 125 // Component of a Dependency::location. Specifies what sort of dependency this is.
cannam@49 126
cannam@49 127 INVALID,
cannam@49 128 // Mostly defined to ensure that zero is not a valid location.
cannam@49 129
cannam@49 130 FIELD,
cannam@49 131 // Binding needed for a field's type. The index is the field index (NOT ordinal!).
cannam@49 132
cannam@49 133 METHOD_PARAMS,
cannam@49 134 // Bindings needed for a method's params type. The index is the method number.
cannam@49 135
cannam@49 136 METHOD_RESULTS,
cannam@49 137 // Bindings needed for a method's results type. The index is the method ordinal.
cannam@49 138
cannam@49 139 SUPERCLASS,
cannam@49 140 // Bindings needed for a superclass type. The index is the superclass's index in the
cannam@49 141 // "extends" list.
cannam@49 142
cannam@49 143 CONST_TYPE
cannam@49 144 // Bindings needed for the type of a constant. The index is zero.
cannam@49 145 };
cannam@49 146
cannam@49 147 static inline uint makeDepLocation(DepKind kind, uint index) {
cannam@49 148 // Make a number representing the location of a particular dependency within its parent
cannam@49 149 // schema.
cannam@49 150
cannam@49 151 return (static_cast<uint>(kind) << 24) | index;
cannam@49 152 }
cannam@49 153
cannam@49 154 class Initializer {
cannam@49 155 public:
cannam@49 156 virtual void init(const RawBrandedSchema* generic) const = 0;
cannam@49 157 };
cannam@49 158
cannam@49 159 const Initializer* lazyInitializer;
cannam@49 160 // Lazy initializer, invoked by ensureInitialized().
cannam@49 161
cannam@49 162 inline void ensureInitialized() const {
cannam@49 163 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
cannam@49 164 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
cannam@49 165 // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@49 166
cannam@49 167 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@49 168 if (i != nullptr) i->init(this);
cannam@49 169 }
cannam@49 170
cannam@49 171 inline bool isUnbound() const;
cannam@49 172 // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case
cannam@49 173 // binding lookups need to be handled specially.
cannam@49 174 };
cannam@49 175
cannam@49 176 struct RawSchema {
cannam@49 177 // The generated code defines a constant RawSchema for every compiled declaration.
cannam@49 178 //
cannam@49 179 // This is an internal structure which could change in the future.
cannam@49 180
cannam@49 181 uint64_t id;
cannam@49 182
cannam@49 183 const word* encodedNode;
cannam@49 184 // Encoded SchemaNode, readable via readMessageUnchecked<schema::Node>(encodedNode).
cannam@49 185
cannam@49 186 uint32_t encodedSize;
cannam@49 187 // Size of encodedNode, in words.
cannam@49 188
cannam@49 189 const RawSchema* const* dependencies;
cannam@49 190 // Pointers to other types on which this one depends, sorted by ID. The schemas in this table
cannam@49 191 // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before
cannam@49 192 // using it.
cannam@49 193 //
cannam@49 194 // TODO(someday): Make this a hashtable.
cannam@49 195
cannam@49 196 const uint16_t* membersByName;
cannam@49 197 // Indexes of members sorted by name. Used to implement name lookup.
cannam@49 198 // TODO(someday): Make this a hashtable.
cannam@49 199
cannam@49 200 uint32_t dependencyCount;
cannam@49 201 uint32_t memberCount;
cannam@49 202 // Sizes of above tables.
cannam@49 203
cannam@49 204 const uint16_t* membersByDiscriminant;
cannam@49 205 // List of all member indexes ordered by discriminant value. Those which don't have a
cannam@49 206 // discriminant value are listed at the end, in order by ordinal.
cannam@49 207
cannam@49 208 const RawSchema* canCastTo;
cannam@49 209 // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue
cannam@49 210 // with this schema. This is null for all compiled-in types; it is only set by SchemaLoader on
cannam@49 211 // dynamically-loaded types.
cannam@49 212
cannam@49 213 class Initializer {
cannam@49 214 public:
cannam@49 215 virtual void init(const RawSchema* schema) const = 0;
cannam@49 216 };
cannam@49 217
cannam@49 218 const Initializer* lazyInitializer;
cannam@49 219 // Lazy initializer, invoked by ensureInitialized().
cannam@49 220
cannam@49 221 inline void ensureInitialized() const {
cannam@49 222 // Lazy initialization support. Invoke to ensure that initialization has taken place. This
cannam@49 223 // is required in particular when traversing the dependency list. RawSchemas for compiled-in
cannam@49 224 // types are always initialized; only dynamically-loaded schemas may be lazy.
cannam@49 225
cannam@49 226 const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
cannam@49 227 if (i != nullptr) i->init(this);
cannam@49 228 }
cannam@49 229
cannam@49 230 RawBrandedSchema defaultBrand;
cannam@49 231 // Specifies the brand to use for this schema if no generic parameters have been bound to
cannam@49 232 // anything. Generally, in the default brand, all generic parameters are treated as if they were
cannam@49 233 // bound to `AnyPointer`.
cannam@49 234 };
cannam@49 235
cannam@49 236 inline bool RawBrandedSchema::isUnbound() const {
cannam@49 237 // The unbound schema is the only one that has no scopes but is not the default schema.
cannam@49 238 return scopeCount == 0 && this != &generic->defaultBrand;
cannam@49 239 }
cannam@49 240
cannam@49 241 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate, bool = false>
cannam@49 242 inline const RawSchema& rawSchema() {
cannam@49 243 return *CapnpPrivate::schema;
cannam@49 244 }
cannam@49 245 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@49 246 inline const RawSchema& rawSchema() {
cannam@49 247 return *schemas::EnumInfo<T>::schema;
cannam@49 248 }
cannam@49 249
cannam@49 250 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
cannam@49 251 inline const RawBrandedSchema& rawBrandedSchema() {
cannam@49 252 return *CapnpPrivate::brand;
cannam@49 253 }
cannam@49 254 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@49 255 inline const RawBrandedSchema& rawBrandedSchema() {
cannam@49 256 return schemas::EnumInfo<T>::schema->defaultBrand;
cannam@49 257 }
cannam@49 258
cannam@49 259 template <typename TypeTag, typename... Params>
cannam@49 260 struct ChooseBrand;
cannam@49 261 // If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a
cannam@49 262 // specific brand instance. TypeTag is the _capnpPrivate struct for the type in question.
cannam@49 263
cannam@49 264 template <typename TypeTag>
cannam@49 265 struct ChooseBrand<TypeTag> {
cannam@49 266 // All params were AnyPointer. No specific brand needed.
cannam@49 267 static constexpr _::RawBrandedSchema const* brand = &TypeTag::schema->defaultBrand;
cannam@49 268 };
cannam@49 269
cannam@49 270 template <typename TypeTag, typename... Rest>
cannam@49 271 struct ChooseBrand<TypeTag, AnyPointer, Rest...>: public ChooseBrand<TypeTag, Rest...> {};
cannam@49 272 // The first parameter is AnyPointer, so recurse to check the rest.
cannam@49 273
cannam@49 274 template <typename TypeTag, typename First, typename... Rest>
cannam@49 275 struct ChooseBrand<TypeTag, First, Rest...> {
cannam@49 276 // At least one parameter is not AnyPointer, so use the specificBrand constant.
cannam@49 277 static constexpr _::RawBrandedSchema const* brand = &TypeTag::specificBrand;
cannam@49 278 };
cannam@49 279
cannam@49 280 template <typename T, Kind k = kind<T>()>
cannam@49 281 struct BrandBindingFor_;
cannam@49 282
cannam@49 283 #define HANDLE_TYPE(Type, which) \
cannam@49 284 template <> \
cannam@49 285 struct BrandBindingFor_<Type, Kind::PRIMITIVE> { \
cannam@49 286 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \
cannam@49 287 return { which, listDepth, nullptr }; \
cannam@49 288 } \
cannam@49 289 }
cannam@49 290 HANDLE_TYPE(Void, 0);
cannam@49 291 HANDLE_TYPE(bool, 1);
cannam@49 292 HANDLE_TYPE(int8_t, 2);
cannam@49 293 HANDLE_TYPE(int16_t, 3);
cannam@49 294 HANDLE_TYPE(int32_t, 4);
cannam@49 295 HANDLE_TYPE(int64_t, 5);
cannam@49 296 HANDLE_TYPE(uint8_t, 6);
cannam@49 297 HANDLE_TYPE(uint16_t, 7);
cannam@49 298 HANDLE_TYPE(uint32_t, 8);
cannam@49 299 HANDLE_TYPE(uint64_t, 9);
cannam@49 300 HANDLE_TYPE(float, 10);
cannam@49 301 HANDLE_TYPE(double, 11);
cannam@49 302 #undef HANDLE_TYPE
cannam@49 303
cannam@49 304 template <>
cannam@49 305 struct BrandBindingFor_<Text, Kind::BLOB> {
cannam@49 306 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 307 return { 12, listDepth, nullptr };
cannam@49 308 }
cannam@49 309 };
cannam@49 310
cannam@49 311 template <>
cannam@49 312 struct BrandBindingFor_<Data, Kind::BLOB> {
cannam@49 313 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 314 return { 13, listDepth, nullptr };
cannam@49 315 }
cannam@49 316 };
cannam@49 317
cannam@49 318 template <typename T>
cannam@49 319 struct BrandBindingFor_<List<T>, Kind::LIST> {
cannam@49 320 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 321 return BrandBindingFor_<T>::get(listDepth + 1);
cannam@49 322 }
cannam@49 323 };
cannam@49 324
cannam@49 325 template <typename T>
cannam@49 326 struct BrandBindingFor_<T, Kind::ENUM> {
cannam@49 327 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 328 return { 15, listDepth, nullptr };
cannam@49 329 }
cannam@49 330 };
cannam@49 331
cannam@49 332 template <typename T>
cannam@49 333 struct BrandBindingFor_<T, Kind::STRUCT> {
cannam@49 334 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 335 return { 16, listDepth, T::_capnpPrivate::brand };
cannam@49 336 }
cannam@49 337 };
cannam@49 338
cannam@49 339 template <typename T>
cannam@49 340 struct BrandBindingFor_<T, Kind::INTERFACE> {
cannam@49 341 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 342 return { 17, listDepth, T::_capnpPrivate::brand };
cannam@49 343 }
cannam@49 344 };
cannam@49 345
cannam@49 346 template <>
cannam@49 347 struct BrandBindingFor_<AnyPointer, Kind::OTHER> {
cannam@49 348 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 349 return { 18, listDepth, 0, 0 };
cannam@49 350 }
cannam@49 351 };
cannam@49 352
cannam@49 353 template <>
cannam@49 354 struct BrandBindingFor_<AnyStruct, Kind::OTHER> {
cannam@49 355 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 356 return { 18, listDepth, 0, 1 };
cannam@49 357 }
cannam@49 358 };
cannam@49 359
cannam@49 360 template <>
cannam@49 361 struct BrandBindingFor_<AnyList, Kind::OTHER> {
cannam@49 362 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 363 return { 18, listDepth, 0, 2 };
cannam@49 364 }
cannam@49 365 };
cannam@49 366
cannam@49 367 template <>
cannam@49 368 struct BrandBindingFor_<Capability, Kind::OTHER> {
cannam@49 369 static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
cannam@49 370 return { 18, listDepth, 0, 3 };
cannam@49 371 }
cannam@49 372 };
cannam@49 373
cannam@49 374 template <typename T>
cannam@49 375 constexpr RawBrandedSchema::Binding brandBindingFor() {
cannam@49 376 return BrandBindingFor_<T>::get(0);
cannam@49 377 }
cannam@49 378
cannam@49 379 kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema);
cannam@49 380 kj::String enumString(uint16_t value, const RawBrandedSchema& schema);
cannam@49 381 // Declared here so that we can declare inline stringify methods on generated types.
cannam@49 382 // Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in.
cannam@49 383
cannam@49 384 template <typename T>
cannam@49 385 inline kj::StringTree structString(StructReader reader) {
cannam@49 386 return structString(reader, rawBrandedSchema<T>());
cannam@49 387 }
cannam@49 388 template <typename T>
cannam@49 389 inline kj::String enumString(T value) {
cannam@49 390 return enumString(static_cast<uint16_t>(value), rawBrandedSchema<T>());
cannam@49 391 }
cannam@49 392
cannam@49 393 #endif // !CAPNP_LITE
cannam@49 394
cannam@49 395 // TODO(cleanup): Unify ConstStruct and ConstList.
cannam@49 396 template <typename T>
cannam@49 397 class ConstStruct {
cannam@49 398 public:
cannam@49 399 ConstStruct() = delete;
cannam@49 400 KJ_DISALLOW_COPY(ConstStruct);
cannam@49 401 inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {}
cannam@49 402
cannam@49 403 inline typename T::Reader get() const {
cannam@49 404 return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<T>();
cannam@49 405 }
cannam@49 406
cannam@49 407 inline operator typename T::Reader() const { return get(); }
cannam@49 408 inline typename T::Reader operator*() const { return get(); }
cannam@49 409 inline TemporaryPointer<typename T::Reader> operator->() const { return get(); }
cannam@49 410
cannam@49 411 private:
cannam@49 412 const word* ptr;
cannam@49 413 };
cannam@49 414
cannam@49 415 template <typename T>
cannam@49 416 class ConstList {
cannam@49 417 public:
cannam@49 418 ConstList() = delete;
cannam@49 419 KJ_DISALLOW_COPY(ConstList);
cannam@49 420 inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {}
cannam@49 421
cannam@49 422 inline typename List<T>::Reader get() const {
cannam@49 423 return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<List<T>>();
cannam@49 424 }
cannam@49 425
cannam@49 426 inline operator typename List<T>::Reader() const { return get(); }
cannam@49 427 inline typename List<T>::Reader operator*() const { return get(); }
cannam@49 428 inline TemporaryPointer<typename List<T>::Reader> operator->() const { return get(); }
cannam@49 429
cannam@49 430 private:
cannam@49 431 const word* ptr;
cannam@49 432 };
cannam@49 433
cannam@49 434 template <size_t size>
cannam@49 435 class ConstText {
cannam@49 436 public:
cannam@49 437 ConstText() = delete;
cannam@49 438 KJ_DISALLOW_COPY(ConstText);
cannam@49 439 inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {}
cannam@49 440
cannam@49 441 inline Text::Reader get() const {
cannam@49 442 return Text::Reader(reinterpret_cast<const char*>(ptr), size);
cannam@49 443 }
cannam@49 444
cannam@49 445 inline operator Text::Reader() const { return get(); }
cannam@49 446 inline Text::Reader operator*() const { return get(); }
cannam@49 447 inline TemporaryPointer<Text::Reader> operator->() const { return get(); }
cannam@49 448
cannam@49 449 inline kj::StringPtr toString() const {
cannam@49 450 return get();
cannam@49 451 }
cannam@49 452
cannam@49 453 private:
cannam@49 454 const word* ptr;
cannam@49 455 };
cannam@49 456
cannam@49 457 template <size_t size>
cannam@49 458 inline kj::StringPtr KJ_STRINGIFY(const ConstText<size>& s) {
cannam@49 459 return s.get();
cannam@49 460 }
cannam@49 461
cannam@49 462 template <size_t size>
cannam@49 463 class ConstData {
cannam@49 464 public:
cannam@49 465 ConstData() = delete;
cannam@49 466 KJ_DISALLOW_COPY(ConstData);
cannam@49 467 inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {}
cannam@49 468
cannam@49 469 inline Data::Reader get() const {
cannam@49 470 return Data::Reader(reinterpret_cast<const byte*>(ptr), size);
cannam@49 471 }
cannam@49 472
cannam@49 473 inline operator Data::Reader() const { return get(); }
cannam@49 474 inline Data::Reader operator*() const { return get(); }
cannam@49 475 inline TemporaryPointer<Data::Reader> operator->() const { return get(); }
cannam@49 476
cannam@49 477 private:
cannam@49 478 const word* ptr;
cannam@49 479 };
cannam@49 480
cannam@49 481 template <size_t size>
cannam@49 482 inline auto KJ_STRINGIFY(const ConstData<size>& s) -> decltype(kj::toCharSequence(s.get())) {
cannam@49 483 return kj::toCharSequence(s.get());
cannam@49 484 }
cannam@49 485
cannam@49 486 } // namespace _ (private)
cannam@49 487
cannam@49 488 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
cannam@49 489 inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; }
cannam@49 490 template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
cannam@49 491 inline constexpr uint64_t typeId() { return id; }
cannam@49 492 // typeId<MyType>() returns the type ID as defined in the schema. Works with structs, enums, and
cannam@49 493 // interfaces.
cannam@49 494
cannam@49 495 template <typename T>
cannam@49 496 inline constexpr uint sizeInWords() {
cannam@49 497 // Return the size, in words, of a Struct type, if allocated free-standing (not in a list).
cannam@49 498 // May be useful for pre-computing space needed in order to precisely allocate messages.
cannam@49 499
cannam@49 500 return (WordCount32(_::structSize<T>().data) +
cannam@49 501 _::structSize<T>().pointers * WORDS_PER_POINTER) / WORDS;
cannam@49 502 }
cannam@49 503
cannam@49 504 } // namespace capnp
cannam@49 505
cannam@49 506 #if _MSC_VER
cannam@49 507 // MSVC doesn't understand floating-point constexpr yet.
cannam@49 508 //
cannam@49 509 // TODO(msvc): Remove this hack when MSVC is fixed.
cannam@49 510 #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value)
cannam@49 511 #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value
cannam@49 512 #else
cannam@49 513 #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value
cannam@49 514 #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value)
cannam@49 515 #endif
cannam@49 516
cannam@49 517 #if CAPNP_LITE
cannam@49 518
cannam@49 519 #define CAPNP_DECLARE_SCHEMA(id) \
cannam@49 520 extern ::capnp::word const* const bp_##id
cannam@49 521
cannam@49 522 #define CAPNP_DECLARE_ENUM(type, id) \
cannam@49 523 inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
cannam@49 524 return ::kj::str(static_cast<uint16_t>(value)); \
cannam@49 525 } \
cannam@49 526 template <> struct EnumInfo<type##_##id> { \
cannam@49 527 struct IsEnum; \
cannam@49 528 static constexpr uint64_t typeId = 0x##id; \
cannam@49 529 static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
cannam@49 530 }
cannam@49 531
cannam@49 532 #if _MSC_VER
cannam@49 533 // TODO(msvc): MSVC dosen't expect constexprs to have definitions.
cannam@49 534 #define CAPNP_DEFINE_ENUM(type, id)
cannam@49 535 #else
cannam@49 536 #define CAPNP_DEFINE_ENUM(type, id) \
cannam@49 537 constexpr uint64_t EnumInfo<type>::typeId
cannam@49 538 #endif
cannam@49 539
cannam@49 540 #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
cannam@49 541 struct IsStruct; \
cannam@49 542 static constexpr uint64_t typeId = 0x##id; \
cannam@49 543 static constexpr uint16_t dataWordSize = dataWordSize_; \
cannam@49 544 static constexpr uint16_t pointerCount = pointerCount_; \
cannam@49 545 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; }
cannam@49 546
cannam@49 547 #else // CAPNP_LITE
cannam@49 548
cannam@49 549 #define CAPNP_DECLARE_SCHEMA(id) \
cannam@49 550 extern ::capnp::word const* const bp_##id; \
cannam@49 551 extern const ::capnp::_::RawSchema s_##id
cannam@49 552
cannam@49 553 #define CAPNP_DECLARE_ENUM(type, id) \
cannam@49 554 inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
cannam@49 555 return ::capnp::_::enumString(value); \
cannam@49 556 } \
cannam@49 557 template <> struct EnumInfo<type##_##id> { \
cannam@49 558 struct IsEnum; \
cannam@49 559 static constexpr uint64_t typeId = 0x##id; \
cannam@49 560 static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
cannam@49 561 static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \
cannam@49 562 }
cannam@49 563 #define CAPNP_DEFINE_ENUM(type, id) \
cannam@49 564 constexpr uint64_t EnumInfo<type>::typeId; \
cannam@49 565 constexpr ::capnp::_::RawSchema const* EnumInfo<type>::schema
cannam@49 566
cannam@49 567 #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
cannam@49 568 struct IsStruct; \
cannam@49 569 static constexpr uint64_t typeId = 0x##id; \
cannam@49 570 static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \
cannam@49 571 static constexpr uint16_t dataWordSize = dataWordSize_; \
cannam@49 572 static constexpr uint16_t pointerCount = pointerCount_; \
cannam@49 573 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
cannam@49 574 static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
cannam@49 575
cannam@49 576 #define CAPNP_DECLARE_INTERFACE_HEADER(id) \
cannam@49 577 struct IsInterface; \
cannam@49 578 static constexpr uint64_t typeId = 0x##id; \
cannam@49 579 static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \
cannam@49 580 static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
cannam@49 581 static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
cannam@49 582
cannam@49 583 #endif // CAPNP_LITE, else
cannam@49 584
cannam@49 585 #endif // CAPNP_GENERATED_HEADER_SUPPORT_H_