annotate win32-mingw/include/capnp/schema.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 #ifndef CAPNP_SCHEMA_H_
Chris@50 23 #define CAPNP_SCHEMA_H_
Chris@50 24
Chris@50 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@50 26 #pragma GCC system_header
Chris@50 27 #endif
Chris@50 28
Chris@50 29 #if CAPNP_LITE
Chris@50 30 #error "Reflection APIs, including this header, are not available in lite mode."
Chris@50 31 #endif
Chris@50 32
Chris@50 33 #include <capnp/schema.capnp.h>
Chris@50 34
Chris@50 35 namespace capnp {
Chris@50 36
Chris@50 37 class Schema;
Chris@50 38 class StructSchema;
Chris@50 39 class EnumSchema;
Chris@50 40 class InterfaceSchema;
Chris@50 41 class ConstSchema;
Chris@50 42 class ListSchema;
Chris@50 43 class Type;
Chris@50 44
Chris@50 45 template <typename T, Kind k = kind<T>()> struct SchemaType_ { typedef Schema Type; };
Chris@50 46 template <typename T> struct SchemaType_<T, Kind::PRIMITIVE> { typedef schema::Type::Which Type; };
Chris@50 47 template <typename T> struct SchemaType_<T, Kind::BLOB> { typedef schema::Type::Which Type; };
Chris@50 48 template <typename T> struct SchemaType_<T, Kind::ENUM> { typedef EnumSchema Type; };
Chris@50 49 template <typename T> struct SchemaType_<T, Kind::STRUCT> { typedef StructSchema Type; };
Chris@50 50 template <typename T> struct SchemaType_<T, Kind::INTERFACE> { typedef InterfaceSchema Type; };
Chris@50 51 template <typename T> struct SchemaType_<T, Kind::LIST> { typedef ListSchema Type; };
Chris@50 52
Chris@50 53 template <typename T>
Chris@50 54 using SchemaType = typename SchemaType_<T>::Type;
Chris@50 55 // SchemaType<T> is the type of T's schema, e.g. StructSchema if T is a struct.
Chris@50 56
Chris@50 57 namespace _ { // private
Chris@50 58 extern const RawSchema NULL_SCHEMA;
Chris@50 59 extern const RawSchema NULL_STRUCT_SCHEMA;
Chris@50 60 extern const RawSchema NULL_ENUM_SCHEMA;
Chris@50 61 extern const RawSchema NULL_INTERFACE_SCHEMA;
Chris@50 62 extern const RawSchema NULL_CONST_SCHEMA;
Chris@50 63 // The schema types default to these null (empty) schemas in case of error, especially when
Chris@50 64 // exceptions are disabled.
Chris@50 65 } // namespace _ (private)
Chris@50 66
Chris@50 67 class Schema {
Chris@50 68 // Convenience wrapper around capnp::schema::Node.
Chris@50 69
Chris@50 70 public:
Chris@50 71 inline Schema(): raw(&_::NULL_SCHEMA.defaultBrand) {}
Chris@50 72
Chris@50 73 template <typename T>
Chris@50 74 static inline SchemaType<T> from() { return SchemaType<T>::template fromImpl<T>(); }
Chris@50 75 // Get the Schema for a particular compiled-in type.
Chris@50 76
Chris@50 77 schema::Node::Reader getProto() const;
Chris@50 78 // Get the underlying Cap'n Proto representation of the schema node. (Note that this accessor
Chris@50 79 // has performance comparable to accessors of struct-typed fields on Reader classes.)
Chris@50 80
Chris@50 81 kj::ArrayPtr<const word> asUncheckedMessage() const;
Chris@50 82 // Get the encoded schema node content as a single message segment. It is safe to read as an
Chris@50 83 // unchecked message.
Chris@50 84
Chris@50 85 Schema getDependency(uint64_t id) const KJ_DEPRECATED("Does not handle generics correctly.");
Chris@50 86 // DEPRECATED: This method cannot correctly account for generic type parameter bindings that
Chris@50 87 // may apply to the dependency. Instead of using this method, use a method of the Schema API
Chris@50 88 // that corresponds to the exact kind of dependency. For example, to get a field type, use
Chris@50 89 // StructSchema::Field::getType().
Chris@50 90 //
Chris@50 91 // Gets the Schema for one of this Schema's dependencies. For example, if this Schema is for a
Chris@50 92 // struct, you could look up the schema for one of its fields' types. Throws an exception if this
Chris@50 93 // schema doesn't actually depend on the given id.
Chris@50 94 //
Chris@50 95 // Note that not all type IDs found in the schema node are considered "dependencies" -- only the
Chris@50 96 // ones that are needed to implement the dynamic API are. That includes:
Chris@50 97 // - Field types.
Chris@50 98 // - Group types.
Chris@50 99 // - scopeId for group nodes, but NOT otherwise.
Chris@50 100 // - Method parameter and return types.
Chris@50 101 //
Chris@50 102 // The following are NOT considered dependencies:
Chris@50 103 // - Nested nodes.
Chris@50 104 // - scopeId for a non-group node.
Chris@50 105 // - Annotations.
Chris@50 106 //
Chris@50 107 // To obtain schemas for those, you would need a SchemaLoader.
Chris@50 108
Chris@50 109 bool isBranded() const;
Chris@50 110 // Returns true if this schema represents a non-default parameterization of this type.
Chris@50 111
Chris@50 112 Schema getGeneric() const;
Chris@50 113 // Get the version of this schema with any brands removed.
Chris@50 114
Chris@50 115 class BrandArgumentList;
Chris@50 116 BrandArgumentList getBrandArgumentsAtScope(uint64_t scopeId) const;
Chris@50 117 // Gets the values bound to the brand parameters at the given scope.
Chris@50 118
Chris@50 119 StructSchema asStruct() const;
Chris@50 120 EnumSchema asEnum() const;
Chris@50 121 InterfaceSchema asInterface() const;
Chris@50 122 ConstSchema asConst() const;
Chris@50 123 // Cast the Schema to a specific type. Throws an exception if the type doesn't match. Use
Chris@50 124 // getProto() to determine type, e.g. getProto().isStruct().
Chris@50 125
Chris@50 126 inline bool operator==(const Schema& other) const { return raw == other.raw; }
Chris@50 127 inline bool operator!=(const Schema& other) const { return raw != other.raw; }
Chris@50 128 // Determine whether two Schemas are wrapping the exact same underlying data, by identity. If
Chris@50 129 // you want to check if two Schemas represent the same type (but possibly different versions of
Chris@50 130 // it), compare their IDs instead.
Chris@50 131
Chris@50 132 template <typename T>
Chris@50 133 void requireUsableAs() const;
Chris@50 134 // Throws an exception if a value with this Schema cannot safely be cast to a native value of
Chris@50 135 // the given type. This passes if either:
Chris@50 136 // - *this == from<T>()
Chris@50 137 // - This schema was loaded with SchemaLoader, the type ID matches typeId<T>(), and
Chris@50 138 // loadCompiledTypeAndDependencies<T>() was called on the SchemaLoader.
Chris@50 139
Chris@50 140 kj::StringPtr getShortDisplayName() const;
Chris@50 141 // Get the short version of the node's display name.
Chris@50 142
Chris@50 143 private:
Chris@50 144 const _::RawBrandedSchema* raw;
Chris@50 145
Chris@50 146 inline explicit Schema(const _::RawBrandedSchema* raw): raw(raw) {
Chris@50 147 KJ_IREQUIRE(raw->lazyInitializer == nullptr,
Chris@50 148 "Must call ensureInitialized() on RawSchema before constructing Schema.");
Chris@50 149 }
Chris@50 150
Chris@50 151 template <typename T> static inline Schema fromImpl() {
Chris@50 152 return Schema(&_::rawSchema<T>());
Chris@50 153 }
Chris@50 154
Chris@50 155 void requireUsableAs(const _::RawSchema* expected) const;
Chris@50 156
Chris@50 157 uint32_t getSchemaOffset(const schema::Value::Reader& value) const;
Chris@50 158
Chris@50 159 Type getBrandBinding(uint64_t scopeId, uint index) const;
Chris@50 160 // Look up the binding for a brand parameter used by this Schema. Returns `AnyPointer` if the
Chris@50 161 // parameter is not bound.
Chris@50 162 //
Chris@50 163 // TODO(someday): Public interface for iterating over all bindings?
Chris@50 164
Chris@50 165 Schema getDependency(uint64_t id, uint location) const;
Chris@50 166 // Look up schema for a particular dependency of this schema. `location` is the dependency
Chris@50 167 // location number as defined in _::RawBrandedSchema.
Chris@50 168
Chris@50 169 Type interpretType(schema::Type::Reader proto, uint location) const;
Chris@50 170 // Interpret a schema::Type in the given location within the schema, compiling it into a
Chris@50 171 // Type object.
Chris@50 172
Chris@50 173 friend class StructSchema;
Chris@50 174 friend class EnumSchema;
Chris@50 175 friend class InterfaceSchema;
Chris@50 176 friend class ConstSchema;
Chris@50 177 friend class ListSchema;
Chris@50 178 friend class SchemaLoader;
Chris@50 179 friend class Type;
Chris@50 180 friend kj::StringTree _::structString(
Chris@50 181 _::StructReader reader, const _::RawBrandedSchema& schema);
Chris@50 182 friend kj::String _::enumString(uint16_t value, const _::RawBrandedSchema& schema);
Chris@50 183 };
Chris@50 184
Chris@50 185 kj::StringPtr KJ_STRINGIFY(const Schema& schema);
Chris@50 186
Chris@50 187 class Schema::BrandArgumentList {
Chris@50 188 // A list of generic parameter bindings for parameters of some particular type. Note that since
Chris@50 189 // parameters on an outer type apply to all inner types as well, a deeply-nested type can have
Chris@50 190 // multiple BrandArgumentLists that apply to it.
Chris@50 191 //
Chris@50 192 // A BrandArgumentList only represents the arguments that the client of the type specified. Since
Chris@50 193 // new parameters can be added over time, this list may not cover all defined parameters for the
Chris@50 194 // type. Missing parameters should be treated as AnyPointer. This class's implementation of
Chris@50 195 // operator[] already does this for you; out-of-bounds access will safely return AnyPointer.
Chris@50 196
Chris@50 197 public:
Chris@50 198 inline BrandArgumentList(): scopeId(0), size_(0), bindings(nullptr) {}
Chris@50 199
Chris@50 200 inline uint size() const { return size_; }
Chris@50 201 Type operator[](uint index) const;
Chris@50 202
Chris@50 203 typedef _::IndexingIterator<const BrandArgumentList, Type> Iterator;
Chris@50 204 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 205 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 206
Chris@50 207 private:
Chris@50 208 uint64_t scopeId;
Chris@50 209 uint size_;
Chris@50 210 bool isUnbound;
Chris@50 211 const _::RawBrandedSchema::Binding* bindings;
Chris@50 212
Chris@50 213 inline BrandArgumentList(uint64_t scopeId, bool isUnbound)
Chris@50 214 : scopeId(scopeId), size_(0), isUnbound(isUnbound), bindings(nullptr) {}
Chris@50 215 inline BrandArgumentList(uint64_t scopeId, uint size,
Chris@50 216 const _::RawBrandedSchema::Binding* bindings)
Chris@50 217 : scopeId(scopeId), size_(size), isUnbound(false), bindings(bindings) {}
Chris@50 218
Chris@50 219 friend class Schema;
Chris@50 220 };
Chris@50 221
Chris@50 222 // -------------------------------------------------------------------
Chris@50 223
Chris@50 224 class StructSchema: public Schema {
Chris@50 225 public:
Chris@50 226 inline StructSchema(): Schema(&_::NULL_STRUCT_SCHEMA.defaultBrand) {}
Chris@50 227
Chris@50 228 class Field;
Chris@50 229 class FieldList;
Chris@50 230 class FieldSubset;
Chris@50 231
Chris@50 232 FieldList getFields() const;
Chris@50 233 // List top-level fields of this struct. This list will contain top-level groups (including
Chris@50 234 // named unions) but not the members of those groups. The list does, however, contain the
Chris@50 235 // members of the unnamed union, if there is one.
Chris@50 236
Chris@50 237 FieldSubset getUnionFields() const;
Chris@50 238 // If the field contains an unnamed union, get a list of fields in the union, ordered by
Chris@50 239 // ordinal. Since discriminant values are assigned sequentially by ordinal, you may index this
Chris@50 240 // list by discriminant value.
Chris@50 241
Chris@50 242 FieldSubset getNonUnionFields() const;
Chris@50 243 // Get the fields of this struct which are not in an unnamed union, ordered by ordinal.
Chris@50 244
Chris@50 245 kj::Maybe<Field> findFieldByName(kj::StringPtr name) const;
Chris@50 246 // Find the field with the given name, or return null if there is no such field. If the struct
Chris@50 247 // contains an unnamed union, then this will find fields of that union in addition to fields
Chris@50 248 // of the outer struct, since they exist in the same namespace. It will not, however, find
Chris@50 249 // members of groups (including named unions) -- you must first look up the group itself,
Chris@50 250 // then dig into its type.
Chris@50 251
Chris@50 252 Field getFieldByName(kj::StringPtr name) const;
Chris@50 253 // Like findFieldByName() but throws an exception on failure.
Chris@50 254
Chris@50 255 kj::Maybe<Field> getFieldByDiscriminant(uint16_t discriminant) const;
Chris@50 256 // Finds the field whose `discriminantValue` is equal to the given value, or returns null if
Chris@50 257 // there is no such field. (If the schema does not represent a union or a struct containing
Chris@50 258 // an unnamed union, then this always returns null.)
Chris@50 259
Chris@50 260 private:
Chris@50 261 StructSchema(Schema base): Schema(base) {}
Chris@50 262 template <typename T> static inline StructSchema fromImpl() {
Chris@50 263 return StructSchema(Schema(&_::rawBrandedSchema<T>()));
Chris@50 264 }
Chris@50 265 friend class Schema;
Chris@50 266 friend class Type;
Chris@50 267 };
Chris@50 268
Chris@50 269 class StructSchema::Field {
Chris@50 270 public:
Chris@50 271 Field() = default;
Chris@50 272
Chris@50 273 inline schema::Field::Reader getProto() const { return proto; }
Chris@50 274 inline StructSchema getContainingStruct() const { return parent; }
Chris@50 275
Chris@50 276 inline uint getIndex() const { return index; }
Chris@50 277 // Get the index of this field within the containing struct or union.
Chris@50 278
Chris@50 279 Type getType() const;
Chris@50 280 // Get the type of this field. Note that this is preferred over getProto().getType() as this
Chris@50 281 // method will apply generics.
Chris@50 282
Chris@50 283 uint32_t getDefaultValueSchemaOffset() const;
Chris@50 284 // For struct, list, and object fields, returns the offset, in words, within the first segment of
Chris@50 285 // the struct's schema, where this field's default value pointer is located. The schema is
Chris@50 286 // always stored as a single-segment unchecked message, which in turn means that the default
Chris@50 287 // value pointer itself can be treated as the root of an unchecked message -- if you know where
Chris@50 288 // to find it, which is what this method helps you with.
Chris@50 289 //
Chris@50 290 // For blobs, returns the offset of the beginning of the blob's content within the first segment
Chris@50 291 // of the struct's schema.
Chris@50 292 //
Chris@50 293 // This is primarily useful for code generators. The C++ code generator, for example, embeds
Chris@50 294 // the entire schema as a raw word array within the generated code. Of course, to implement
Chris@50 295 // field accessors, it needs access to those fields' default values. Embedding separate copies
Chris@50 296 // of those default values would be redundant since they are already included in the schema, but
Chris@50 297 // seeking through the schema at runtime to find the default values would be ugly. Instead,
Chris@50 298 // the code generator can use getDefaultValueSchemaOffset() to find the offset of the default
Chris@50 299 // value within the schema, and can simply apply that offset at runtime.
Chris@50 300 //
Chris@50 301 // If the above does not make sense, you probably don't need this method.
Chris@50 302
Chris@50 303 inline bool operator==(const Field& other) const;
Chris@50 304 inline bool operator!=(const Field& other) const { return !(*this == other); }
Chris@50 305
Chris@50 306 private:
Chris@50 307 StructSchema parent;
Chris@50 308 uint index;
Chris@50 309 schema::Field::Reader proto;
Chris@50 310
Chris@50 311 inline Field(StructSchema parent, uint index, schema::Field::Reader proto)
Chris@50 312 : parent(parent), index(index), proto(proto) {}
Chris@50 313
Chris@50 314 friend class StructSchema;
Chris@50 315 };
Chris@50 316
Chris@50 317 kj::StringPtr KJ_STRINGIFY(const StructSchema::Field& field);
Chris@50 318
Chris@50 319 class StructSchema::FieldList {
Chris@50 320 public:
Chris@50 321 FieldList() = default; // empty list
Chris@50 322
Chris@50 323 inline uint size() const { return list.size(); }
Chris@50 324 inline Field operator[](uint index) const { return Field(parent, index, list[index]); }
Chris@50 325
Chris@50 326 typedef _::IndexingIterator<const FieldList, Field> Iterator;
Chris@50 327 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 328 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 329
Chris@50 330 private:
Chris@50 331 StructSchema parent;
Chris@50 332 List<schema::Field>::Reader list;
Chris@50 333
Chris@50 334 inline FieldList(StructSchema parent, List<schema::Field>::Reader list)
Chris@50 335 : parent(parent), list(list) {}
Chris@50 336
Chris@50 337 friend class StructSchema;
Chris@50 338 };
Chris@50 339
Chris@50 340 class StructSchema::FieldSubset {
Chris@50 341 public:
Chris@50 342 FieldSubset() = default; // empty list
Chris@50 343
Chris@50 344 inline uint size() const { return size_; }
Chris@50 345 inline Field operator[](uint index) const {
Chris@50 346 return Field(parent, indices[index], list[indices[index]]);
Chris@50 347 }
Chris@50 348
Chris@50 349 typedef _::IndexingIterator<const FieldSubset, Field> Iterator;
Chris@50 350 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 351 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 352
Chris@50 353 private:
Chris@50 354 StructSchema parent;
Chris@50 355 List<schema::Field>::Reader list;
Chris@50 356 const uint16_t* indices;
Chris@50 357 uint size_;
Chris@50 358
Chris@50 359 inline FieldSubset(StructSchema parent, List<schema::Field>::Reader list,
Chris@50 360 const uint16_t* indices, uint size)
Chris@50 361 : parent(parent), list(list), indices(indices), size_(size) {}
Chris@50 362
Chris@50 363 friend class StructSchema;
Chris@50 364 };
Chris@50 365
Chris@50 366 // -------------------------------------------------------------------
Chris@50 367
Chris@50 368 class EnumSchema: public Schema {
Chris@50 369 public:
Chris@50 370 inline EnumSchema(): Schema(&_::NULL_ENUM_SCHEMA.defaultBrand) {}
Chris@50 371
Chris@50 372 class Enumerant;
Chris@50 373 class EnumerantList;
Chris@50 374
Chris@50 375 EnumerantList getEnumerants() const;
Chris@50 376
Chris@50 377 kj::Maybe<Enumerant> findEnumerantByName(kj::StringPtr name) const;
Chris@50 378
Chris@50 379 Enumerant getEnumerantByName(kj::StringPtr name) const;
Chris@50 380 // Like findEnumerantByName() but throws an exception on failure.
Chris@50 381
Chris@50 382 private:
Chris@50 383 EnumSchema(Schema base): Schema(base) {}
Chris@50 384 template <typename T> static inline EnumSchema fromImpl() {
Chris@50 385 return EnumSchema(Schema(&_::rawBrandedSchema<T>()));
Chris@50 386 }
Chris@50 387 friend class Schema;
Chris@50 388 friend class Type;
Chris@50 389 };
Chris@50 390
Chris@50 391 class EnumSchema::Enumerant {
Chris@50 392 public:
Chris@50 393 Enumerant() = default;
Chris@50 394
Chris@50 395 inline schema::Enumerant::Reader getProto() const { return proto; }
Chris@50 396 inline EnumSchema getContainingEnum() const { return parent; }
Chris@50 397
Chris@50 398 inline uint16_t getOrdinal() const { return ordinal; }
Chris@50 399 inline uint getIndex() const { return ordinal; }
Chris@50 400
Chris@50 401 inline bool operator==(const Enumerant& other) const;
Chris@50 402 inline bool operator!=(const Enumerant& other) const { return !(*this == other); }
Chris@50 403
Chris@50 404 private:
Chris@50 405 EnumSchema parent;
Chris@50 406 uint16_t ordinal;
Chris@50 407 schema::Enumerant::Reader proto;
Chris@50 408
Chris@50 409 inline Enumerant(EnumSchema parent, uint16_t ordinal, schema::Enumerant::Reader proto)
Chris@50 410 : parent(parent), ordinal(ordinal), proto(proto) {}
Chris@50 411
Chris@50 412 friend class EnumSchema;
Chris@50 413 };
Chris@50 414
Chris@50 415 class EnumSchema::EnumerantList {
Chris@50 416 public:
Chris@50 417 EnumerantList() = default; // empty list
Chris@50 418
Chris@50 419 inline uint size() const { return list.size(); }
Chris@50 420 inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); }
Chris@50 421
Chris@50 422 typedef _::IndexingIterator<const EnumerantList, Enumerant> Iterator;
Chris@50 423 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 424 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 425
Chris@50 426 private:
Chris@50 427 EnumSchema parent;
Chris@50 428 List<schema::Enumerant>::Reader list;
Chris@50 429
Chris@50 430 inline EnumerantList(EnumSchema parent, List<schema::Enumerant>::Reader list)
Chris@50 431 : parent(parent), list(list) {}
Chris@50 432
Chris@50 433 friend class EnumSchema;
Chris@50 434 };
Chris@50 435
Chris@50 436 // -------------------------------------------------------------------
Chris@50 437
Chris@50 438 class InterfaceSchema: public Schema {
Chris@50 439 public:
Chris@50 440 inline InterfaceSchema(): Schema(&_::NULL_INTERFACE_SCHEMA.defaultBrand) {}
Chris@50 441
Chris@50 442 class Method;
Chris@50 443 class MethodList;
Chris@50 444
Chris@50 445 MethodList getMethods() const;
Chris@50 446
Chris@50 447 kj::Maybe<Method> findMethodByName(kj::StringPtr name) const;
Chris@50 448
Chris@50 449 Method getMethodByName(kj::StringPtr name) const;
Chris@50 450 // Like findMethodByName() but throws an exception on failure.
Chris@50 451
Chris@50 452 class SuperclassList;
Chris@50 453
Chris@50 454 SuperclassList getSuperclasses() const;
Chris@50 455 // Get the immediate superclasses of this type, after applying generics.
Chris@50 456
Chris@50 457 bool extends(InterfaceSchema other) const;
Chris@50 458 // Returns true if `other` is a superclass of this interface (including if `other == *this`).
Chris@50 459
Chris@50 460 kj::Maybe<InterfaceSchema> findSuperclass(uint64_t typeId) const;
Chris@50 461 // Find the superclass of this interface with the given type ID. Returns null if the interface
Chris@50 462 // extends no such type.
Chris@50 463
Chris@50 464 private:
Chris@50 465 InterfaceSchema(Schema base): Schema(base) {}
Chris@50 466 template <typename T> static inline InterfaceSchema fromImpl() {
Chris@50 467 return InterfaceSchema(Schema(&_::rawBrandedSchema<T>()));
Chris@50 468 }
Chris@50 469 friend class Schema;
Chris@50 470 friend class Type;
Chris@50 471
Chris@50 472 kj::Maybe<Method> findMethodByName(kj::StringPtr name, uint& counter) const;
Chris@50 473 bool extends(InterfaceSchema other, uint& counter) const;
Chris@50 474 kj::Maybe<InterfaceSchema> findSuperclass(uint64_t typeId, uint& counter) const;
Chris@50 475 // We protect against malicious schemas with large or cyclic hierarchies by cutting off the
Chris@50 476 // search when the counter reaches a threshold.
Chris@50 477 };
Chris@50 478
Chris@50 479 class InterfaceSchema::Method {
Chris@50 480 public:
Chris@50 481 Method() = default;
Chris@50 482
Chris@50 483 inline schema::Method::Reader getProto() const { return proto; }
Chris@50 484 inline InterfaceSchema getContainingInterface() const { return parent; }
Chris@50 485
Chris@50 486 inline uint16_t getOrdinal() const { return ordinal; }
Chris@50 487 inline uint getIndex() const { return ordinal; }
Chris@50 488
Chris@50 489 StructSchema getParamType() const;
Chris@50 490 StructSchema getResultType() const;
Chris@50 491 // Get the parameter and result types, including substituting generic parameters.
Chris@50 492
Chris@50 493 inline bool operator==(const Method& other) const;
Chris@50 494 inline bool operator!=(const Method& other) const { return !(*this == other); }
Chris@50 495
Chris@50 496 private:
Chris@50 497 InterfaceSchema parent;
Chris@50 498 uint16_t ordinal;
Chris@50 499 schema::Method::Reader proto;
Chris@50 500
Chris@50 501 inline Method(InterfaceSchema parent, uint16_t ordinal,
Chris@50 502 schema::Method::Reader proto)
Chris@50 503 : parent(parent), ordinal(ordinal), proto(proto) {}
Chris@50 504
Chris@50 505 friend class InterfaceSchema;
Chris@50 506 };
Chris@50 507
Chris@50 508 class InterfaceSchema::MethodList {
Chris@50 509 public:
Chris@50 510 MethodList() = default; // empty list
Chris@50 511
Chris@50 512 inline uint size() const { return list.size(); }
Chris@50 513 inline Method operator[](uint index) const { return Method(parent, index, list[index]); }
Chris@50 514
Chris@50 515 typedef _::IndexingIterator<const MethodList, Method> Iterator;
Chris@50 516 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 517 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 518
Chris@50 519 private:
Chris@50 520 InterfaceSchema parent;
Chris@50 521 List<schema::Method>::Reader list;
Chris@50 522
Chris@50 523 inline MethodList(InterfaceSchema parent, List<schema::Method>::Reader list)
Chris@50 524 : parent(parent), list(list) {}
Chris@50 525
Chris@50 526 friend class InterfaceSchema;
Chris@50 527 };
Chris@50 528
Chris@50 529 class InterfaceSchema::SuperclassList {
Chris@50 530 public:
Chris@50 531 SuperclassList() = default; // empty list
Chris@50 532
Chris@50 533 inline uint size() const { return list.size(); }
Chris@50 534 InterfaceSchema operator[](uint index) const;
Chris@50 535
Chris@50 536 typedef _::IndexingIterator<const SuperclassList, InterfaceSchema> Iterator;
Chris@50 537 inline Iterator begin() const { return Iterator(this, 0); }
Chris@50 538 inline Iterator end() const { return Iterator(this, size()); }
Chris@50 539
Chris@50 540 private:
Chris@50 541 InterfaceSchema parent;
Chris@50 542 List<schema::Superclass>::Reader list;
Chris@50 543
Chris@50 544 inline SuperclassList(InterfaceSchema parent, List<schema::Superclass>::Reader list)
Chris@50 545 : parent(parent), list(list) {}
Chris@50 546
Chris@50 547 friend class InterfaceSchema;
Chris@50 548 };
Chris@50 549
Chris@50 550 // -------------------------------------------------------------------
Chris@50 551
Chris@50 552 class ConstSchema: public Schema {
Chris@50 553 // Represents a constant declaration.
Chris@50 554 //
Chris@50 555 // `ConstSchema` can be implicitly cast to DynamicValue to read its value.
Chris@50 556
Chris@50 557 public:
Chris@50 558 inline ConstSchema(): Schema(&_::NULL_CONST_SCHEMA.defaultBrand) {}
Chris@50 559
Chris@50 560 template <typename T>
Chris@50 561 ReaderFor<T> as() const;
Chris@50 562 // Read the constant's value. This is a convenience method equivalent to casting the ConstSchema
Chris@50 563 // to a DynamicValue and then calling its `as<T>()` method. For dependency reasons, this method
Chris@50 564 // is defined in <capnp/dynamic.h>, which you must #include explicitly.
Chris@50 565
Chris@50 566 uint32_t getValueSchemaOffset() const;
Chris@50 567 // Much like StructSchema::Field::getDefaultValueSchemaOffset(), if the constant has pointer
Chris@50 568 // type, this gets the offset from the beginning of the constant's schema node to a pointer
Chris@50 569 // representing the constant value.
Chris@50 570
Chris@50 571 Type getType() const;
Chris@50 572
Chris@50 573 private:
Chris@50 574 ConstSchema(Schema base): Schema(base) {}
Chris@50 575 friend class Schema;
Chris@50 576 };
Chris@50 577
Chris@50 578 // -------------------------------------------------------------------
Chris@50 579
Chris@50 580 class Type {
Chris@50 581 public:
Chris@50 582 struct BrandParameter {
Chris@50 583 uint64_t scopeId;
Chris@50 584 uint index;
Chris@50 585 };
Chris@50 586 struct ImplicitParameter {
Chris@50 587 uint index;
Chris@50 588 };
Chris@50 589
Chris@50 590 inline Type();
Chris@50 591 inline Type(schema::Type::Which primitive);
Chris@50 592 inline Type(StructSchema schema);
Chris@50 593 inline Type(EnumSchema schema);
Chris@50 594 inline Type(InterfaceSchema schema);
Chris@50 595 inline Type(ListSchema schema);
Chris@50 596 inline Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind);
Chris@50 597 inline Type(BrandParameter param);
Chris@50 598 inline Type(ImplicitParameter param);
Chris@50 599
Chris@50 600 template <typename T>
Chris@50 601 inline static Type from();
Chris@50 602
Chris@50 603 inline schema::Type::Which which() const;
Chris@50 604
Chris@50 605 StructSchema asStruct() const;
Chris@50 606 EnumSchema asEnum() const;
Chris@50 607 InterfaceSchema asInterface() const;
Chris@50 608 ListSchema asList() const;
Chris@50 609 // Each of these methods may only be called if which() returns the corresponding type.
Chris@50 610
Chris@50 611 kj::Maybe<BrandParameter> getBrandParameter() const;
Chris@50 612 // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular
Chris@50 613 // AnyPointer and not a parameter.
Chris@50 614
Chris@50 615 kj::Maybe<ImplicitParameter> getImplicitParameter() const;
Chris@50 616 // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular
Chris@50 617 // AnyPointer and not a parameter. "Implicit parameters" refer to type parameters on methods.
Chris@50 618
Chris@50 619 inline schema::Type::AnyPointer::Unconstrained::Which whichAnyPointerKind() const;
Chris@50 620 // Only callable if which() returns ANY_POINTER.
Chris@50 621
Chris@50 622 inline bool isVoid() const;
Chris@50 623 inline bool isBool() const;
Chris@50 624 inline bool isInt8() const;
Chris@50 625 inline bool isInt16() const;
Chris@50 626 inline bool isInt32() const;
Chris@50 627 inline bool isInt64() const;
Chris@50 628 inline bool isUInt8() const;
Chris@50 629 inline bool isUInt16() const;
Chris@50 630 inline bool isUInt32() const;
Chris@50 631 inline bool isUInt64() const;
Chris@50 632 inline bool isFloat32() const;
Chris@50 633 inline bool isFloat64() const;
Chris@50 634 inline bool isText() const;
Chris@50 635 inline bool isData() const;
Chris@50 636 inline bool isList() const;
Chris@50 637 inline bool isEnum() const;
Chris@50 638 inline bool isStruct() const;
Chris@50 639 inline bool isInterface() const;
Chris@50 640 inline bool isAnyPointer() const;
Chris@50 641
Chris@50 642 bool operator==(const Type& other) const;
Chris@50 643 inline bool operator!=(const Type& other) const { return !(*this == other); }
Chris@50 644
Chris@50 645 size_t hashCode() const;
Chris@50 646
Chris@50 647 inline Type wrapInList(uint depth = 1) const;
Chris@50 648 // Return the Type formed by wrapping this type in List() `depth` times.
Chris@50 649
Chris@50 650 inline Type(schema::Type::Which derived, const _::RawBrandedSchema* schema);
Chris@50 651 // For internal use.
Chris@50 652
Chris@50 653 private:
Chris@50 654 schema::Type::Which baseType; // type not including applications of List()
Chris@50 655 uint8_t listDepth; // 0 for T, 1 for List(T), 2 for List(List(T)), ...
Chris@50 656
Chris@50 657 bool isImplicitParam;
Chris@50 658 // If true, this refers to an implicit method parameter. baseType must be ANY_POINTER, scopeId
Chris@50 659 // must be zero, and paramIndex indicates the parameter index.
Chris@50 660
Chris@50 661 union {
Chris@50 662 uint16_t paramIndex;
Chris@50 663 // If baseType is ANY_POINTER but this Type actually refers to a type parameter, this is the
Chris@50 664 // index of the parameter among the parameters at its scope, and `scopeId` below is the type ID
Chris@50 665 // of the scope where the parameter was defined.
Chris@50 666
Chris@50 667 schema::Type::AnyPointer::Unconstrained::Which anyPointerKind;
Chris@50 668 // If scopeId is zero and isImplicitParam is false.
Chris@50 669 };
Chris@50 670
Chris@50 671 union {
Chris@50 672 const _::RawBrandedSchema* schema; // if type is struct, enum, interface...
Chris@50 673 uint64_t scopeId; // if type is AnyPointer but it's actually a type parameter...
Chris@50 674 };
Chris@50 675
Chris@50 676 Type(schema::Type::Which baseType, uint8_t listDepth, const _::RawBrandedSchema* schema)
Chris@50 677 : baseType(baseType), listDepth(listDepth), schema(schema) {
Chris@50 678 KJ_IREQUIRE(baseType != schema::Type::ANY_POINTER);
Chris@50 679 }
Chris@50 680
Chris@50 681 void requireUsableAs(Type expected) const;
Chris@50 682
Chris@50 683 friend class ListSchema; // only for requireUsableAs()
Chris@50 684 };
Chris@50 685
Chris@50 686 // -------------------------------------------------------------------
Chris@50 687
Chris@50 688 class ListSchema {
Chris@50 689 // ListSchema is a little different because list types are not described by schema nodes. So,
Chris@50 690 // ListSchema doesn't subclass Schema.
Chris@50 691
Chris@50 692 public:
Chris@50 693 ListSchema() = default;
Chris@50 694
Chris@50 695 static ListSchema of(schema::Type::Which primitiveType);
Chris@50 696 static ListSchema of(StructSchema elementType);
Chris@50 697 static ListSchema of(EnumSchema elementType);
Chris@50 698 static ListSchema of(InterfaceSchema elementType);
Chris@50 699 static ListSchema of(ListSchema elementType);
Chris@50 700 static ListSchema of(Type elementType);
Chris@50 701 // Construct the schema for a list of the given type.
Chris@50 702
Chris@50 703 static ListSchema of(schema::Type::Reader elementType, Schema context)
Chris@50 704 KJ_DEPRECATED("Does not handle generics correctly.");
Chris@50 705 // DEPRECATED: This method cannot correctly account for generic type parameter bindings that
Chris@50 706 // may apply to the input type. Instead of using this method, use a method of the Schema API
Chris@50 707 // that corresponds to the exact kind of dependency. For example, to get a field type, use
Chris@50 708 // StructSchema::Field::getType().
Chris@50 709 //
Chris@50 710 // Construct from an element type schema. Requires a context which can handle getDependency()
Chris@50 711 // requests for any type ID found in the schema.
Chris@50 712
Chris@50 713 Type getElementType() const;
Chris@50 714
Chris@50 715 inline schema::Type::Which whichElementType() const;
Chris@50 716 // Get the element type's "which()". ListSchema does not actually store a schema::Type::Reader
Chris@50 717 // describing the element type, but if it did, this would be equivalent to calling
Chris@50 718 // .getBody().which() on that type.
Chris@50 719
Chris@50 720 StructSchema getStructElementType() const;
Chris@50 721 EnumSchema getEnumElementType() const;
Chris@50 722 InterfaceSchema getInterfaceElementType() const;
Chris@50 723 ListSchema getListElementType() const;
Chris@50 724 // Get the schema for complex element types. Each of these throws an exception if the element
Chris@50 725 // type is not of the requested kind.
Chris@50 726
Chris@50 727 inline bool operator==(const ListSchema& other) const { return elementType == other.elementType; }
Chris@50 728 inline bool operator!=(const ListSchema& other) const { return elementType != other.elementType; }
Chris@50 729
Chris@50 730 template <typename T>
Chris@50 731 void requireUsableAs() const;
Chris@50 732
Chris@50 733 private:
Chris@50 734 Type elementType;
Chris@50 735
Chris@50 736 inline explicit ListSchema(Type elementType): elementType(elementType) {}
Chris@50 737
Chris@50 738 template <typename T>
Chris@50 739 struct FromImpl;
Chris@50 740 template <typename T> static inline ListSchema fromImpl() {
Chris@50 741 return FromImpl<T>::get();
Chris@50 742 }
Chris@50 743
Chris@50 744 void requireUsableAs(ListSchema expected) const;
Chris@50 745
Chris@50 746 friend class Schema;
Chris@50 747 };
Chris@50 748
Chris@50 749 // =======================================================================================
Chris@50 750 // inline implementation
Chris@50 751
Chris@50 752 template <> inline schema::Type::Which Schema::from<Void>() { return schema::Type::VOID; }
Chris@50 753 template <> inline schema::Type::Which Schema::from<bool>() { return schema::Type::BOOL; }
Chris@50 754 template <> inline schema::Type::Which Schema::from<int8_t>() { return schema::Type::INT8; }
Chris@50 755 template <> inline schema::Type::Which Schema::from<int16_t>() { return schema::Type::INT16; }
Chris@50 756 template <> inline schema::Type::Which Schema::from<int32_t>() { return schema::Type::INT32; }
Chris@50 757 template <> inline schema::Type::Which Schema::from<int64_t>() { return schema::Type::INT64; }
Chris@50 758 template <> inline schema::Type::Which Schema::from<uint8_t>() { return schema::Type::UINT8; }
Chris@50 759 template <> inline schema::Type::Which Schema::from<uint16_t>() { return schema::Type::UINT16; }
Chris@50 760 template <> inline schema::Type::Which Schema::from<uint32_t>() { return schema::Type::UINT32; }
Chris@50 761 template <> inline schema::Type::Which Schema::from<uint64_t>() { return schema::Type::UINT64; }
Chris@50 762 template <> inline schema::Type::Which Schema::from<float>() { return schema::Type::FLOAT32; }
Chris@50 763 template <> inline schema::Type::Which Schema::from<double>() { return schema::Type::FLOAT64; }
Chris@50 764 template <> inline schema::Type::Which Schema::from<Text>() { return schema::Type::TEXT; }
Chris@50 765 template <> inline schema::Type::Which Schema::from<Data>() { return schema::Type::DATA; }
Chris@50 766
Chris@50 767 inline Schema Schema::getDependency(uint64_t id) const {
Chris@50 768 return getDependency(id, 0);
Chris@50 769 }
Chris@50 770
Chris@50 771 inline bool Schema::isBranded() const {
Chris@50 772 return raw != &raw->generic->defaultBrand;
Chris@50 773 }
Chris@50 774
Chris@50 775 inline Schema Schema::getGeneric() const {
Chris@50 776 return Schema(&raw->generic->defaultBrand);
Chris@50 777 }
Chris@50 778
Chris@50 779 template <typename T>
Chris@50 780 inline void Schema::requireUsableAs() const {
Chris@50 781 requireUsableAs(&_::rawSchema<T>());
Chris@50 782 }
Chris@50 783
Chris@50 784 inline bool StructSchema::Field::operator==(const Field& other) const {
Chris@50 785 return parent == other.parent && index == other.index;
Chris@50 786 }
Chris@50 787 inline bool EnumSchema::Enumerant::operator==(const Enumerant& other) const {
Chris@50 788 return parent == other.parent && ordinal == other.ordinal;
Chris@50 789 }
Chris@50 790 inline bool InterfaceSchema::Method::operator==(const Method& other) const {
Chris@50 791 return parent == other.parent && ordinal == other.ordinal;
Chris@50 792 }
Chris@50 793
Chris@50 794 inline ListSchema ListSchema::of(StructSchema elementType) {
Chris@50 795 return ListSchema(Type(elementType));
Chris@50 796 }
Chris@50 797 inline ListSchema ListSchema::of(EnumSchema elementType) {
Chris@50 798 return ListSchema(Type(elementType));
Chris@50 799 }
Chris@50 800 inline ListSchema ListSchema::of(InterfaceSchema elementType) {
Chris@50 801 return ListSchema(Type(elementType));
Chris@50 802 }
Chris@50 803 inline ListSchema ListSchema::of(ListSchema elementType) {
Chris@50 804 return ListSchema(Type(elementType));
Chris@50 805 }
Chris@50 806 inline ListSchema ListSchema::of(Type elementType) {
Chris@50 807 return ListSchema(elementType);
Chris@50 808 }
Chris@50 809
Chris@50 810 inline Type ListSchema::getElementType() const {
Chris@50 811 return elementType;
Chris@50 812 }
Chris@50 813
Chris@50 814 inline schema::Type::Which ListSchema::whichElementType() const {
Chris@50 815 return elementType.which();
Chris@50 816 }
Chris@50 817
Chris@50 818 inline StructSchema ListSchema::getStructElementType() const {
Chris@50 819 return elementType.asStruct();
Chris@50 820 }
Chris@50 821
Chris@50 822 inline EnumSchema ListSchema::getEnumElementType() const {
Chris@50 823 return elementType.asEnum();
Chris@50 824 }
Chris@50 825
Chris@50 826 inline InterfaceSchema ListSchema::getInterfaceElementType() const {
Chris@50 827 return elementType.asInterface();
Chris@50 828 }
Chris@50 829
Chris@50 830 inline ListSchema ListSchema::getListElementType() const {
Chris@50 831 return elementType.asList();
Chris@50 832 }
Chris@50 833
Chris@50 834 template <typename T>
Chris@50 835 inline void ListSchema::requireUsableAs() const {
Chris@50 836 static_assert(kind<T>() == Kind::LIST,
Chris@50 837 "ListSchema::requireUsableAs<T>() requires T is a list type.");
Chris@50 838 requireUsableAs(Schema::from<T>());
Chris@50 839 }
Chris@50 840
Chris@50 841 inline void ListSchema::requireUsableAs(ListSchema expected) const {
Chris@50 842 elementType.requireUsableAs(expected.elementType);
Chris@50 843 }
Chris@50 844
Chris@50 845 template <typename T>
Chris@50 846 struct ListSchema::FromImpl<List<T>> {
Chris@50 847 static inline ListSchema get() { return of(Schema::from<T>()); }
Chris@50 848 };
Chris@50 849
Chris@50 850 inline Type::Type(): baseType(schema::Type::VOID), listDepth(0), schema(nullptr) {}
Chris@50 851 inline Type::Type(schema::Type::Which primitive)
Chris@50 852 : baseType(primitive), listDepth(0), isImplicitParam(false) {
Chris@50 853 KJ_IREQUIRE(primitive != schema::Type::STRUCT &&
Chris@50 854 primitive != schema::Type::ENUM &&
Chris@50 855 primitive != schema::Type::INTERFACE &&
Chris@50 856 primitive != schema::Type::LIST);
Chris@50 857 if (primitive == schema::Type::ANY_POINTER) {
Chris@50 858 scopeId = 0;
Chris@50 859 anyPointerKind = schema::Type::AnyPointer::Unconstrained::ANY_KIND;
Chris@50 860 } else {
Chris@50 861 schema = nullptr;
Chris@50 862 }
Chris@50 863 }
Chris@50 864 inline Type::Type(schema::Type::Which derived, const _::RawBrandedSchema* schema)
Chris@50 865 : baseType(derived), listDepth(0), isImplicitParam(false), schema(schema) {
Chris@50 866 KJ_IREQUIRE(derived == schema::Type::STRUCT ||
Chris@50 867 derived == schema::Type::ENUM ||
Chris@50 868 derived == schema::Type::INTERFACE);
Chris@50 869 }
Chris@50 870
Chris@50 871 inline Type::Type(StructSchema schema)
Chris@50 872 : baseType(schema::Type::STRUCT), listDepth(0), schema(schema.raw) {}
Chris@50 873 inline Type::Type(EnumSchema schema)
Chris@50 874 : baseType(schema::Type::ENUM), listDepth(0), schema(schema.raw) {}
Chris@50 875 inline Type::Type(InterfaceSchema schema)
Chris@50 876 : baseType(schema::Type::INTERFACE), listDepth(0), schema(schema.raw) {}
Chris@50 877 inline Type::Type(ListSchema schema)
Chris@50 878 : Type(schema.getElementType()) { ++listDepth; }
Chris@50 879 inline Type::Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind)
Chris@50 880 : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false),
Chris@50 881 anyPointerKind(anyPointerKind), scopeId(0) {}
Chris@50 882 inline Type::Type(BrandParameter param)
Chris@50 883 : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false),
Chris@50 884 paramIndex(param.index), scopeId(param.scopeId) {}
Chris@50 885 inline Type::Type(ImplicitParameter param)
Chris@50 886 : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(true),
Chris@50 887 paramIndex(param.index), scopeId(0) {}
Chris@50 888
Chris@50 889 inline schema::Type::Which Type::which() const {
Chris@50 890 return listDepth > 0 ? schema::Type::LIST : baseType;
Chris@50 891 }
Chris@50 892
Chris@50 893 inline schema::Type::AnyPointer::Unconstrained::Which Type::whichAnyPointerKind() const {
Chris@50 894 KJ_IREQUIRE(baseType == schema::Type::ANY_POINTER);
Chris@50 895 return !isImplicitParam && scopeId == 0 ? anyPointerKind
Chris@50 896 : schema::Type::AnyPointer::Unconstrained::ANY_KIND;
Chris@50 897 }
Chris@50 898
Chris@50 899 template <typename T>
Chris@50 900 inline Type Type::from() { return Type(Schema::from<T>()); }
Chris@50 901
Chris@50 902 inline bool Type::isVoid () const { return baseType == schema::Type::VOID && listDepth == 0; }
Chris@50 903 inline bool Type::isBool () const { return baseType == schema::Type::BOOL && listDepth == 0; }
Chris@50 904 inline bool Type::isInt8 () const { return baseType == schema::Type::INT8 && listDepth == 0; }
Chris@50 905 inline bool Type::isInt16 () const { return baseType == schema::Type::INT16 && listDepth == 0; }
Chris@50 906 inline bool Type::isInt32 () const { return baseType == schema::Type::INT32 && listDepth == 0; }
Chris@50 907 inline bool Type::isInt64 () const { return baseType == schema::Type::INT64 && listDepth == 0; }
Chris@50 908 inline bool Type::isUInt8 () const { return baseType == schema::Type::UINT8 && listDepth == 0; }
Chris@50 909 inline bool Type::isUInt16 () const { return baseType == schema::Type::UINT16 && listDepth == 0; }
Chris@50 910 inline bool Type::isUInt32 () const { return baseType == schema::Type::UINT32 && listDepth == 0; }
Chris@50 911 inline bool Type::isUInt64 () const { return baseType == schema::Type::UINT64 && listDepth == 0; }
Chris@50 912 inline bool Type::isFloat32() const { return baseType == schema::Type::FLOAT32 && listDepth == 0; }
Chris@50 913 inline bool Type::isFloat64() const { return baseType == schema::Type::FLOAT64 && listDepth == 0; }
Chris@50 914 inline bool Type::isText () const { return baseType == schema::Type::TEXT && listDepth == 0; }
Chris@50 915 inline bool Type::isData () const { return baseType == schema::Type::DATA && listDepth == 0; }
Chris@50 916 inline bool Type::isList () const { return listDepth > 0; }
Chris@50 917 inline bool Type::isEnum () const { return baseType == schema::Type::ENUM && listDepth == 0; }
Chris@50 918 inline bool Type::isStruct () const { return baseType == schema::Type::STRUCT && listDepth == 0; }
Chris@50 919 inline bool Type::isInterface() const {
Chris@50 920 return baseType == schema::Type::INTERFACE && listDepth == 0;
Chris@50 921 }
Chris@50 922 inline bool Type::isAnyPointer() const {
Chris@50 923 return baseType == schema::Type::ANY_POINTER && listDepth == 0;
Chris@50 924 }
Chris@50 925
Chris@50 926 inline Type Type::wrapInList(uint depth) const {
Chris@50 927 Type result = *this;
Chris@50 928 result.listDepth += depth;
Chris@50 929 return result;
Chris@50 930 }
Chris@50 931
Chris@50 932 } // namespace capnp
Chris@50 933
Chris@50 934 #endif // CAPNP_SCHEMA_H_