annotate osx/include/capnp/schema.h @ 139:413e081fcc6f

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