annotate osx/include/capnp/generated-header-support.h @ 141:1b5b6dfd0d0e

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