annotate win32-mingw/include/capnp/generated-header-support.h @ 135:38d1c0e7850b

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