annotate win64-msvc/include/capnp/generated-header-support.h @ 146:206f0eb279b8

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