schema.h 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24 25
#ifndef CAPNP_SCHEMA_H_
#define CAPNP_SCHEMA_H_
26

Kenton Varda's avatar
Kenton Varda committed
27
#include <capnp/schema.capnp.h>
28

29
namespace capnp {
30 31 32 33 34

class Schema;
class StructSchema;
class EnumSchema;
class InterfaceSchema;
35
class ConstSchema;
36 37 38
class ListSchema;

template <typename T, Kind k = kind<T>()> struct SchemaType_ { typedef Schema Type; };
Kenton Varda's avatar
Kenton Varda committed
39 40
template <typename T> struct SchemaType_<T, Kind::PRIMITIVE> { typedef schema::Type::Which Type; };
template <typename T> struct SchemaType_<T, Kind::BLOB> { typedef schema::Type::Which Type; };
41 42 43 44 45 46 47 48 49
template <typename T> struct SchemaType_<T, Kind::ENUM> { typedef EnumSchema Type; };
template <typename T> struct SchemaType_<T, Kind::STRUCT> { typedef StructSchema Type; };
template <typename T> struct SchemaType_<T, Kind::INTERFACE> { typedef InterfaceSchema Type; };
template <typename T> struct SchemaType_<T, Kind::LIST> { typedef ListSchema Type; };

template <typename T>
using SchemaType = typename SchemaType_<T>::Type;
// SchemaType<T> is the type of T's schema, e.g. StructSchema if T is a struct.

50 51 52 53 54 55 56 57 58 59
namespace _ {  // private
extern const RawSchema NULL_SCHEMA;
extern const RawSchema NULL_STRUCT_SCHEMA;
extern const RawSchema NULL_ENUM_SCHEMA;
extern const RawSchema NULL_INTERFACE_SCHEMA;
extern const RawSchema NULL_CONST_SCHEMA;
// The schema types default to these null (empty) schemas in case of error, especially when
// exceptions are disabled.
}  // namespace _ (private)

60
class Schema {
61
  // Convenience wrapper around capnp::schema::Node.
62 63

public:
64
  inline Schema(): raw(&_::NULL_SCHEMA) {}
65 66 67 68 69

  template <typename T>
  static inline SchemaType<T> from() { return SchemaType<T>::template fromImpl<T>(); }
  // Get the Schema for a particular compiled-in type.

Kenton Varda's avatar
Kenton Varda committed
70
  schema::Node::Reader getProto() const;
71 72
  // Get the underlying Cap'n Proto representation of the schema node.  (Note that this accessor
  // has performance comparable to accessors of struct-typed fields on Reader classes.)
73

Kenton Varda's avatar
Kenton Varda committed
74 75 76 77
  kj::ArrayPtr<const word> asUncheckedMessage() const;
  // Get the encoded schema node content as a single message segment.  It is safe to read as an
  // unchecked message.

78 79 80
  Schema getDependency(uint64_t id) const;
  // Gets the Schema for one of this Schema's dependencies.  For example, if this Schema is for a
  // struct, you could look up the schema for one of its fields' types.  Throws an exception if this
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  // schema doesn't actually depend on the given id.
  //
  // Note that not all type IDs found in the schema node are considered "dependencies" -- only the
  // ones that are needed to implement the dynamic API are.  That includes:
  // - Field types.
  // - Group types.
  // - scopeId for group nodes, but NOT otherwise.
  // - Method parameter and return types.
  //
  // The following are NOT considered dependencies:
  // - Nested nodes.
  // - scopeId for a non-group node.
  // - Annotations.
  //
  // To obtain schemas for those, you would need a SchemaLoader.
96 97 98 99

  StructSchema asStruct() const;
  EnumSchema asEnum() const;
  InterfaceSchema asInterface() const;
100 101 102
  ConstSchema asConst() const;
  // Cast the Schema to a specific type.  Throws an exception if the type doesn't match.  Use
  // getProto() to determine type, e.g. getProto().isStruct().
103 104 105 106 107 108 109

  inline bool operator==(const Schema& other) const { return raw == other.raw; }
  inline bool operator!=(const Schema& other) const { return raw != other.raw; }
  // Determine whether two Schemas are wrapping the exact same underlying data, by identity.  If
  // you want to check if two Schemas represent the same type (but possibly different versions of
  // it), compare their IDs instead.

110
  template <typename T>
111
  void requireUsableAs() const;
112 113 114 115 116 117
  // Throws an exception if a value with this Schema cannot safely be cast to a native value of
  // the given type.  This passes if either:
  // - *this == from<T>()
  // - This schema was loaded with SchemaLoader, the type ID matches typeId<T>(), and
  //   loadCompiledTypeAndDependencies<T>() was called on the SchemaLoader.

118 119 120
  kj::StringPtr getShortDisplayName() const;
  // Get the short version of the node's display name.

121
private:
122
  const _::RawSchema* raw;
123

124 125 126 127
  inline explicit Schema(const _::RawSchema* raw): raw(raw) {
    KJ_IREQUIRE(raw->lazyInitializer == nullptr,
        "Must call ensureInitialized() on RawSchema before constructing Schema.");
  }
128 129

  template <typename T> static inline Schema fromImpl() {
130
    return Schema(&_::rawSchema<T>());
131 132
  }

133
  void requireUsableAs(const _::RawSchema* expected) const;
134

135 136
  uint32_t getSchemaOffset(const schema::Value::Reader& value) const;

137 138 139
  friend class StructSchema;
  friend class EnumSchema;
  friend class InterfaceSchema;
140
  friend class ConstSchema;
141 142
  friend class ListSchema;
  friend class SchemaLoader;
143 144 145 146 147 148
};

// -------------------------------------------------------------------

class StructSchema: public Schema {
public:
149
  inline StructSchema(): Schema(&_::NULL_STRUCT_SCHEMA) {}
150

Kenton Varda's avatar
Kenton Varda committed
151
  class Field;
Kenton Varda's avatar
Kenton Varda committed
152 153
  class FieldList;
  class FieldSubset;
154

Kenton Varda's avatar
Kenton Varda committed
155 156 157 158
  FieldList getFields() const;
  // List top-level fields of this struct.  This list will contain top-level groups (including
  // named unions) but not the members of those groups.  The list does, however, contain the
  // members of the unnamed union, if there is one.
Kenton Varda's avatar
Kenton Varda committed
159

Kenton Varda's avatar
Kenton Varda committed
160 161 162 163
  FieldSubset getUnionFields() const;
  // If the field contains an unnamed union, get a list of fields in the union, ordered by
  // ordinal.  Since discriminant values are assigned sequentially by ordinal, you may index this
  // list by discriminant value.
164

Kenton Varda's avatar
Kenton Varda committed
165 166
  FieldSubset getNonUnionFields() const;
  // Get the fields of this struct which are not in an unnamed union, ordered by ordinal.
Kenton Varda's avatar
Kenton Varda committed
167

Kenton Varda's avatar
Kenton Varda committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
  kj::Maybe<Field> findFieldByName(kj::StringPtr name) const;
  // Find the field with the given name, or return null if there is no such field.  If the struct
  // contains an unnamed union, then this will find fields of that union in addition to fields
  // of the outer struct, since they exist in the same namespace.  It will not, however, find
  // members of groups (including named unions) -- you must first look up the group itself,
  // then dig into its type.

  Field getFieldByName(kj::StringPtr name) const;
  // Like findFieldByName() but throws an exception on failure.

  kj::Maybe<Field> getFieldByDiscriminant(uint16_t discriminant) const;
  // Finds the field whose `discriminantValue` is equal to the given value, or returns null if
  // there is no such field.  (If the schema does not represent a union or a struct containing
  // an unnamed union, then this always returns null.)
182

183
private:
184
  StructSchema(const _::RawSchema* raw): Schema(raw) {}
185
  template <typename T> static inline StructSchema fromImpl() {
186
    return StructSchema(&_::rawSchema<T>());
187 188
  }
  friend class Schema;
189
  friend kj::StringTree _::structString(
190
      _::StructReader reader, const _::RawSchema& schema);
191 192
};

Kenton Varda's avatar
Kenton Varda committed
193
class StructSchema::Field {
194
public:
Kenton Varda's avatar
Kenton Varda committed
195
  Field() = default;
196

Kenton Varda's avatar
Kenton Varda committed
197
  inline schema::Field::Reader getProto() const { return proto; }
198 199 200
  inline StructSchema getContainingStruct() const { return parent; }

  inline uint getIndex() const { return index; }
Kenton Varda's avatar
Kenton Varda committed
201
  // Get the index of this field within the containing struct or union.
Kenton Varda's avatar
Kenton Varda committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

  uint32_t getDefaultValueSchemaOffset() const;
  // For struct, list, and object fields, returns the offset, in words, within the first segment of
  // the struct's schema, where this field's default value pointer is located.  The schema is
  // always stored as a single-segment unchecked message, which in turn means that the default
  // value pointer itself can be treated as the root of an unchecked message -- if you know where
  // to find it, which is what this method helps you with.
  //
  // For blobs, returns the offset of the begging of the blob's content within the first segment of
  // the struct's schema.
  //
  // This is primarily useful for code generators.  The C++ code generator, for example, embeds
  // the entire schema as a raw word array within the generated code.  Of course, to implement
  // field accessors, it needs access to those fields' default values.  Embedding separate copies
  // of those default values would be redundant since they are already included in the schema, but
  // seeking through the schema at runtime to find the default values would be ugly.  Instead,
  // the code generator can use getDefaultValueSchemaOffset() to find the offset of the default
  // value within the schema, and can simply apply that offset at runtime.
  //
  // If the above does not make sense, you probably don't need this method.

Kenton Varda's avatar
Kenton Varda committed
223 224
  inline bool operator==(const Field& other) const;
  inline bool operator!=(const Field& other) const { return !(*this == other); }
Kenton Varda's avatar
Kenton Varda committed
225

226
private:
Kenton Varda's avatar
Kenton Varda committed
227 228
  StructSchema parent;
  uint index;
Kenton Varda's avatar
Kenton Varda committed
229
  schema::Field::Reader proto;
Kenton Varda's avatar
Kenton Varda committed
230

Kenton Varda's avatar
Kenton Varda committed
231
  inline Field(StructSchema parent, uint index, schema::Field::Reader proto)
Kenton Varda's avatar
Kenton Varda committed
232
      : parent(parent), index(index), proto(proto) {}
233 234 235 236

  friend class StructSchema;
};

Kenton Varda's avatar
Kenton Varda committed
237
class StructSchema::FieldList {
Kenton Varda's avatar
Kenton Varda committed
238
public:
Kenton Varda's avatar
Kenton Varda committed
239
  FieldList() = default;  // empty list
Kenton Varda's avatar
Kenton Varda committed
240

Kenton Varda's avatar
Kenton Varda committed
241 242
  inline uint size() const { return list.size(); }
  inline Field operator[](uint index) const { return Field(parent, index, list[index]); }
Kenton Varda's avatar
Kenton Varda committed
243

Kenton Varda's avatar
Kenton Varda committed
244 245 246
  typedef _::IndexingIterator<const FieldList, Field> Iterator;
  inline Iterator begin() const { return Iterator(this, 0); }
  inline Iterator end() const { return Iterator(this, size()); }
Kenton Varda's avatar
Kenton Varda committed
247 248

private:
Kenton Varda's avatar
Kenton Varda committed
249
  StructSchema parent;
Kenton Varda's avatar
Kenton Varda committed
250
  List<schema::Field>::Reader list;
Kenton Varda's avatar
Kenton Varda committed
251

Kenton Varda's avatar
Kenton Varda committed
252
  inline FieldList(StructSchema parent, List<schema::Field>::Reader list)
Kenton Varda's avatar
Kenton Varda committed
253
      : parent(parent), list(list) {}
Kenton Varda's avatar
Kenton Varda committed
254 255 256 257

  friend class StructSchema;
};

Kenton Varda's avatar
Kenton Varda committed
258
class StructSchema::FieldSubset {
259
public:
Kenton Varda's avatar
Kenton Varda committed
260
  FieldSubset() = default;  // empty list
261

Kenton Varda's avatar
Kenton Varda committed
262 263 264 265
  inline uint size() const { return size_; }
  inline Field operator[](uint index) const {
    return Field(parent, indices[index], list[indices[index]]);
  }
266

Kenton Varda's avatar
Kenton Varda committed
267
  typedef _::IndexingIterator<const FieldSubset, Field> Iterator;
268 269
  inline Iterator begin() const { return Iterator(this, 0); }
  inline Iterator end() const { return Iterator(this, size()); }
270 271 272

private:
  StructSchema parent;
Kenton Varda's avatar
Kenton Varda committed
273
  List<schema::Field>::Reader list;
Kenton Varda's avatar
Kenton Varda committed
274 275
  const uint16_t* indices;
  uint size_;
276

Kenton Varda's avatar
Kenton Varda committed
277
  inline FieldSubset(StructSchema parent, List<schema::Field>::Reader list,
Kenton Varda's avatar
Kenton Varda committed
278 279
                     const uint16_t* indices, uint size)
      : parent(parent), list(list), indices(indices), size_(size) {}
280 281 282 283 284 285 286 287

  friend class StructSchema;
};

// -------------------------------------------------------------------

class EnumSchema: public Schema {
public:
288
  inline EnumSchema(): Schema(&_::NULL_ENUM_SCHEMA) {}
289

290 291 292
  class Enumerant;
  class EnumerantList;

293
  EnumerantList getEnumerants() const;
Kenton Varda's avatar
Kenton Varda committed
294

295
  kj::Maybe<Enumerant> findEnumerantByName(kj::StringPtr name) const;
296

297
  Enumerant getEnumerantByName(kj::StringPtr name) const;
Kenton Varda's avatar
Kenton Varda committed
298 299
  // Like findEnumerantByName() but throws an exception on failure.

300
private:
301
  EnumSchema(const _::RawSchema* raw): Schema(raw) {}
302
  template <typename T> static inline EnumSchema fromImpl() {
303
    return EnumSchema(&_::rawSchema<T>());
304 305 306 307 308 309 310 311
  }
  friend class Schema;
};

class EnumSchema::Enumerant {
public:
  Enumerant() = default;

Kenton Varda's avatar
Kenton Varda committed
312
  inline schema::Enumerant::Reader getProto() const { return proto; }
Kenton Varda's avatar
Kenton Varda committed
313
  inline EnumSchema getContainingEnum() const { return parent; }
314

Kenton Varda's avatar
Kenton Varda committed
315 316
  inline uint16_t getOrdinal() const { return ordinal; }
  inline uint getIndex() const { return ordinal; }
317 318 319 320 321 322 323

  inline bool operator==(const Enumerant& other) const;
  inline bool operator!=(const Enumerant& other) const { return !(*this == other); }

private:
  EnumSchema parent;
  uint16_t ordinal;
Kenton Varda's avatar
Kenton Varda committed
324
  schema::Enumerant::Reader proto;
325

Kenton Varda's avatar
Kenton Varda committed
326
  inline Enumerant(EnumSchema parent, uint16_t ordinal, schema::Enumerant::Reader proto)
327 328 329 330 331 332 333
      : parent(parent), ordinal(ordinal), proto(proto) {}

  friend class EnumSchema;
};

class EnumSchema::EnumerantList {
public:
334 335
  EnumerantList() = default;  // empty list

336 337 338
  inline uint size() const { return list.size(); }
  inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); }

339
  typedef _::IndexingIterator<const EnumerantList, Enumerant> Iterator;
340 341
  inline Iterator begin() const { return Iterator(this, 0); }
  inline Iterator end() const { return Iterator(this, size()); }
342 343 344

private:
  EnumSchema parent;
Kenton Varda's avatar
Kenton Varda committed
345
  List<schema::Enumerant>::Reader list;
346

Kenton Varda's avatar
Kenton Varda committed
347
  inline EnumerantList(EnumSchema parent, List<schema::Enumerant>::Reader list)
348 349 350 351 352 353 354 355 356
      : parent(parent), list(list) {}

  friend class EnumSchema;
};

// -------------------------------------------------------------------

class InterfaceSchema: public Schema {
public:
357
  inline InterfaceSchema(): Schema(&_::NULL_INTERFACE_SCHEMA) {}
358

359 360 361
  class Method;
  class MethodList;

362
  MethodList getMethods() const;
Kenton Varda's avatar
Kenton Varda committed
363

364
  kj::Maybe<Method> findMethodByName(kj::StringPtr name) const;
365

366
  Method getMethodByName(kj::StringPtr name) const;
Kenton Varda's avatar
Kenton Varda committed
367 368
  // Like findMethodByName() but throws an exception on failure.

369 370 371 372 373 374 375
  bool extends(InterfaceSchema other) const;
  // Returns true if `other` is a superclass of this interface (including if `other == *this`).

  kj::Maybe<InterfaceSchema> findSuperclass(uint64_t typeId) const;
  // Find the superclass of this interface with the given type ID.  Returns null if the interface
  // extends no such type.

376
private:
377
  InterfaceSchema(const _::RawSchema* raw): Schema(raw) {}
378
  template <typename T> static inline InterfaceSchema fromImpl() {
379
    return InterfaceSchema(&_::rawSchema<T>());
380 381
  }
  friend class Schema;
382 383 384 385 386 387

  kj::Maybe<Method> findMethodByName(kj::StringPtr name, uint& counter) const;
  bool extends(InterfaceSchema other, uint& counter) const;
  kj::Maybe<InterfaceSchema> findSuperclass(uint64_t typeId, uint& counter) const;
  // We protect against malicious schemas with large or cyclic hierarchies by cutting off the
  // search when the counter reaches a threshold.
388 389 390 391 392 393
};

class InterfaceSchema::Method {
public:
  Method() = default;

Kenton Varda's avatar
Kenton Varda committed
394
  inline schema::Method::Reader getProto() const { return proto; }
Kenton Varda's avatar
Kenton Varda committed
395
  inline InterfaceSchema getContainingInterface() const { return parent; }
396

Kenton Varda's avatar
Kenton Varda committed
397 398
  inline uint16_t getOrdinal() const { return ordinal; }
  inline uint getIndex() const { return ordinal; }
399 400 401 402 403 404 405

  inline bool operator==(const Method& other) const;
  inline bool operator!=(const Method& other) const { return !(*this == other); }

private:
  InterfaceSchema parent;
  uint16_t ordinal;
Kenton Varda's avatar
Kenton Varda committed
406
  schema::Method::Reader proto;
407 408

  inline Method(InterfaceSchema parent, uint16_t ordinal,
Kenton Varda's avatar
Kenton Varda committed
409
                schema::Method::Reader proto)
410 411 412 413 414 415 416
      : parent(parent), ordinal(ordinal), proto(proto) {}

  friend class InterfaceSchema;
};

class InterfaceSchema::MethodList {
public:
417 418
  MethodList() = default;  // empty list

419 420 421
  inline uint size() const { return list.size(); }
  inline Method operator[](uint index) const { return Method(parent, index, list[index]); }

422
  typedef _::IndexingIterator<const MethodList, Method> Iterator;
423 424
  inline Iterator begin() const { return Iterator(this, 0); }
  inline Iterator end() const { return Iterator(this, size()); }
425 426 427

private:
  InterfaceSchema parent;
Kenton Varda's avatar
Kenton Varda committed
428
  List<schema::Method>::Reader list;
429

Kenton Varda's avatar
Kenton Varda committed
430
  inline MethodList(InterfaceSchema parent, List<schema::Method>::Reader list)
431 432 433 434 435 436 437
      : parent(parent), list(list) {}

  friend class InterfaceSchema;
};

// -------------------------------------------------------------------

438 439 440 441 442 443
class ConstSchema: public Schema {
  // Represents a constant declaration.
  //
  // `ConstSchema` can be implicitly cast to DynamicValue to read its value.

public:
444
  inline ConstSchema(): Schema(&_::NULL_CONST_SCHEMA) {}
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

  template <typename T>
  ReaderFor<T> as() const;
  // Read the constant's value.  This is a convenience method equivalent to casting the ConstSchema
  // to a DynamicValue and then calling its `as<T>()` method.  For dependency reasons, this method
  // is defined in <capnp/dynamic.h>, which you must #include explicitly.

  uint32_t getValueSchemaOffset() const;
  // Much like StructSchema::Field::getDefaultValueSchemaOffset(), if the constant has pointer
  // type, this gets the offset from the beginning of the constant's schema node to a pointer
  // representing the constant value.

private:
  ConstSchema(const _::RawSchema* raw): Schema(raw) {}
  friend class Schema;
};

// -------------------------------------------------------------------

464 465 466 467 468 469 470
class ListSchema {
  // ListSchema is a little different because list types are not described by schema nodes.  So,
  // ListSchema doesn't subclass Schema.

public:
  ListSchema() = default;

Kenton Varda's avatar
Kenton Varda committed
471
  static ListSchema of(schema::Type::Which primitiveType);
472 473 474 475 476 477
  static ListSchema of(StructSchema elementType);
  static ListSchema of(EnumSchema elementType);
  static ListSchema of(InterfaceSchema elementType);
  static ListSchema of(ListSchema elementType);
  // Construct the schema for a list of the given type.

Kenton Varda's avatar
Kenton Varda committed
478
  static ListSchema of(schema::Type::Reader elementType, Schema context);
479 480 481
  // Construct from an element type schema.  Requires a context which can handle getDependency()
  // requests for any type ID found in the schema.

Kenton Varda's avatar
Kenton Varda committed
482
  inline schema::Type::Which whichElementType() const;
483 484 485 486 487 488 489 490 491 492 493
  // Get the element type's "which()".  ListSchema does not actually store a schema::Type::Reader
  // describing the element type, but if it did, this would be equivalent to calling
  // .getBody().which() on that type.

  StructSchema getStructElementType() const;
  EnumSchema getEnumElementType() const;
  InterfaceSchema getInterfaceElementType() const;
  ListSchema getListElementType() const;
  // Get the schema for complex element types.  Each of these throws an exception if the element
  // type is not of the requested kind.

494 495 496
  inline bool operator==(const ListSchema& other) const;
  inline bool operator!=(const ListSchema& other) const { return !(*this == other); }

497
  template <typename T>
498
  void requireUsableAs() const;
499

500
private:
Kenton Varda's avatar
Kenton Varda committed
501
  schema::Type::Which elementType;
502 503 504
  uint8_t nestingDepth;  // 0 for T, 1 for List(T), 2 for List(List(T)), ...
  Schema elementSchema;  // if elementType is struct, enum, interface...

Kenton Varda's avatar
Kenton Varda committed
505
  inline ListSchema(schema::Type::Which elementType)
506
      : elementType(elementType), nestingDepth(0) {}
Kenton Varda's avatar
Kenton Varda committed
507
  inline ListSchema(schema::Type::Which elementType, Schema elementSchema)
508
      : elementType(elementType), nestingDepth(0), elementSchema(elementSchema) {}
Kenton Varda's avatar
Kenton Varda committed
509
  inline ListSchema(schema::Type::Which elementType, uint8_t nestingDepth,
510 511 512 513 514 515 516 517 518
                    Schema elementSchema)
      : elementType(elementType), nestingDepth(nestingDepth), elementSchema(elementSchema) {}

  template <typename T>
  struct FromImpl;
  template <typename T> static inline ListSchema fromImpl() {
    return FromImpl<T>::get();
  }

519
  void requireUsableAs(ListSchema expected) const;
520

521 522 523 524 525 526
  friend class Schema;
};

// =======================================================================================
// inline implementation

Kenton Varda's avatar
Kenton Varda committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540
template <> inline schema::Type::Which Schema::from<Void>() { return schema::Type::VOID; }
template <> inline schema::Type::Which Schema::from<bool>() { return schema::Type::BOOL; }
template <> inline schema::Type::Which Schema::from<int8_t>() { return schema::Type::INT8; }
template <> inline schema::Type::Which Schema::from<int16_t>() { return schema::Type::INT16; }
template <> inline schema::Type::Which Schema::from<int32_t>() { return schema::Type::INT32; }
template <> inline schema::Type::Which Schema::from<int64_t>() { return schema::Type::INT64; }
template <> inline schema::Type::Which Schema::from<uint8_t>() { return schema::Type::UINT8; }
template <> inline schema::Type::Which Schema::from<uint16_t>() { return schema::Type::UINT16; }
template <> inline schema::Type::Which Schema::from<uint32_t>() { return schema::Type::UINT32; }
template <> inline schema::Type::Which Schema::from<uint64_t>() { return schema::Type::UINT64; }
template <> inline schema::Type::Which Schema::from<float>() { return schema::Type::FLOAT32; }
template <> inline schema::Type::Which Schema::from<double>() { return schema::Type::FLOAT64; }
template <> inline schema::Type::Which Schema::from<Text>() { return schema::Type::TEXT; }
template <> inline schema::Type::Which Schema::from<Data>() { return schema::Type::DATA; }
541

542
template <typename T>
543
inline void Schema::requireUsableAs() const {
544
  requireUsableAs(&_::rawSchema<T>());
545 546
}

Kenton Varda's avatar
Kenton Varda committed
547 548
inline bool StructSchema::Field::operator==(const Field& other) const {
  return parent == other.parent && index == other.index;
549 550 551 552 553 554 555 556 557
}
inline bool EnumSchema::Enumerant::operator==(const Enumerant& other) const {
  return parent == other.parent && ordinal == other.ordinal;
}
inline bool InterfaceSchema::Method::operator==(const Method& other) const {
  return parent == other.parent && ordinal == other.ordinal;
}

inline ListSchema ListSchema::of(StructSchema elementType) {
Kenton Varda's avatar
Kenton Varda committed
558
  return ListSchema(schema::Type::STRUCT, 0, elementType);
559 560
}
inline ListSchema ListSchema::of(EnumSchema elementType) {
Kenton Varda's avatar
Kenton Varda committed
561
  return ListSchema(schema::Type::ENUM, 0, elementType);
562 563
}
inline ListSchema ListSchema::of(InterfaceSchema elementType) {
Kenton Varda's avatar
Kenton Varda committed
564
  return ListSchema(schema::Type::INTERFACE, 0, elementType);
565 566 567 568 569 570
}
inline ListSchema ListSchema::of(ListSchema elementType) {
  return ListSchema(elementType.elementType, elementType.nestingDepth + 1,
                    elementType.elementSchema);
}

Kenton Varda's avatar
Kenton Varda committed
571 572
inline schema::Type::Which ListSchema::whichElementType() const {
  return nestingDepth == 0 ? elementType : schema::Type::LIST;
573 574
}

575 576 577 578 579
inline bool ListSchema::operator==(const ListSchema& other) const {
  return elementType == other.elementType && nestingDepth == other.nestingDepth &&
      elementSchema == other.elementSchema;
}

580
template <typename T>
581
inline void ListSchema::requireUsableAs() const {
582 583 584 585 586
  static_assert(kind<T>() == Kind::LIST,
                "ListSchema::requireUsableAs<T>() requires T is a list type.");
  requireUsableAs(Schema::from<T>());
}

587 588 589 590 591
template <typename T>
struct ListSchema::FromImpl<List<T>> {
  static inline ListSchema get() { return of(Schema::from<T>()); }
};

592
}  // namespace capnp
593

Kenton Varda's avatar
Kenton Varda committed
594
#endif  // CAPNP_SCHEMA_H_