annotate win32-mingw/include/capnp/generated-header-support.h @ 50:37d53a7e8262

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