annotate win64-msvc/include/capnp/generated-header-support.h @ 58:eab3b14ddc95

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