annotate win32-mingw/include/capnp/schema-loader.h @ 64:eccd51b72864

Update Win32 capnp builds to v0.6
author Chris Cannam
date Tue, 23 May 2017 09:16:54 +0100
parents 37d53a7e8262
children
rev   line source
Chris@64 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@64 2 // Licensed under the MIT License:
Chris@64 3 //
Chris@64 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@64 5 // of this software and associated documentation files (the "Software"), to deal
Chris@64 6 // in the Software without restriction, including without limitation the rights
Chris@64 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@64 8 // copies of the Software, and to permit persons to whom the Software is
Chris@64 9 // furnished to do so, subject to the following conditions:
Chris@64 10 //
Chris@64 11 // The above copyright notice and this permission notice shall be included in
Chris@64 12 // all copies or substantial portions of the Software.
Chris@64 13 //
Chris@64 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@64 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@64 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@64 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@64 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@64 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@64 20 // THE SOFTWARE.
Chris@64 21
Chris@64 22 #ifndef CAPNP_SCHEMA_LOADER_H_
Chris@64 23 #define CAPNP_SCHEMA_LOADER_H_
Chris@64 24
Chris@64 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@64 26 #pragma GCC system_header
Chris@64 27 #endif
Chris@64 28
Chris@64 29 #include "schema.h"
Chris@64 30 #include <kj/memory.h>
Chris@64 31 #include <kj/mutex.h>
Chris@64 32
Chris@64 33 namespace capnp {
Chris@64 34
Chris@64 35 class SchemaLoader {
Chris@64 36 // Class which can be used to construct Schema objects from schema::Nodes as defined in
Chris@64 37 // schema.capnp.
Chris@64 38 //
Chris@64 39 // It is a bad idea to use this class on untrusted input with exceptions disabled -- you may
Chris@64 40 // be exposing yourself to denial-of-service attacks, as attackers can easily construct schemas
Chris@64 41 // that are subtly inconsistent in a way that causes exceptions to be thrown either by
Chris@64 42 // SchemaLoader or by the dynamic API when the schemas are subsequently used. If you enable and
Chris@64 43 // properly catch exceptions, you should be OK -- assuming no bugs in the Cap'n Proto
Chris@64 44 // implementation, of course.
Chris@64 45
Chris@64 46 public:
Chris@64 47 class LazyLoadCallback {
Chris@64 48 public:
Chris@64 49 virtual void load(const SchemaLoader& loader, uint64_t id) const = 0;
Chris@64 50 // Request that the schema node with the given ID be loaded into the given SchemaLoader. If
Chris@64 51 // the callback is able to find a schema for this ID, it should invoke `loadOnce()` on
Chris@64 52 // `loader` to load it. If no such node exists, it should simply do nothing and return.
Chris@64 53 //
Chris@64 54 // The callback is allowed to load schema nodes other than the one requested, e.g. because it
Chris@64 55 // expects they will be needed soon.
Chris@64 56 //
Chris@64 57 // If the `SchemaLoader` is used from multiple threads, the callback must be thread-safe.
Chris@64 58 // In particular, it's possible for multiple threads to invoke `load()` with the same ID.
Chris@64 59 // If the callback performs a large amount of work to look up IDs, it should be sure to
Chris@64 60 // de-dup these requests.
Chris@64 61 };
Chris@64 62
Chris@64 63 SchemaLoader();
Chris@64 64
Chris@64 65 SchemaLoader(const LazyLoadCallback& callback);
Chris@64 66 // Construct a SchemaLoader which will invoke the given callback when a schema node is requested
Chris@64 67 // that isn't already loaded.
Chris@64 68
Chris@64 69 ~SchemaLoader() noexcept(false);
Chris@64 70 KJ_DISALLOW_COPY(SchemaLoader);
Chris@64 71
Chris@64 72 Schema get(uint64_t id, schema::Brand::Reader brand = schema::Brand::Reader(),
Chris@64 73 Schema scope = Schema()) const;
Chris@64 74 // Gets the schema for the given ID, throwing an exception if it isn't present.
Chris@64 75 //
Chris@64 76 // The returned schema may be invalidated if load() is called with a new schema for the same ID.
Chris@64 77 // In general, you should not call load() while a schema from this loader is in-use.
Chris@64 78 //
Chris@64 79 // `brand` and `scope` are used to determine brand bindings where relevant. `brand` gives
Chris@64 80 // parameter bindings for the target type's brand parameters that were specified at the reference
Chris@64 81 // site. `scope` specifies the scope in which the type ID appeared -- if `brand` itself contains
Chris@64 82 // parameter references or indicates that some parameters will be inherited, these will be
Chris@64 83 // interpreted within / inherited from `scope`.
Chris@64 84
Chris@64 85 kj::Maybe<Schema> tryGet(uint64_t id, schema::Brand::Reader bindings = schema::Brand::Reader(),
Chris@64 86 Schema scope = Schema()) const;
Chris@64 87 // Like get() but doesn't throw.
Chris@64 88
Chris@64 89 Schema getUnbound(uint64_t id) const;
Chris@64 90 // Gets a special version of the schema in which all brand parameters are "unbound". This means
Chris@64 91 // that if you look up a type via the Schema API, and it resolves to a brand parameter, the
Chris@64 92 // returned Type's getBrandParameter() method will return info about that parameter. Otherwise,
Chris@64 93 // normally, all brand parameters that aren't otherwise bound are assumed to simply be
Chris@64 94 // "AnyPointer".
Chris@64 95
Chris@64 96 Type getType(schema::Type::Reader type, Schema scope = Schema()) const;
Chris@64 97 // Convenience method which interprets a schema::Type to produce a Type object. Implemented in
Chris@64 98 // terms of get().
Chris@64 99
Chris@64 100 Schema load(const schema::Node::Reader& reader);
Chris@64 101 // Loads the given schema node. Validates the node and throws an exception if invalid. This
Chris@64 102 // makes a copy of the schema, so the object passed in can be destroyed after this returns.
Chris@64 103 //
Chris@64 104 // If the node has any dependencies which are not already loaded, they will be initialized as
Chris@64 105 // stubs -- empty schemas of whichever kind is expected.
Chris@64 106 //
Chris@64 107 // If another schema for the given reader has already been seen, the loader will inspect both
Chris@64 108 // schemas to determine which one is newer, and use that that one. If the two versions are
Chris@64 109 // found to be incompatible, an exception is thrown. If the two versions differ but are
Chris@64 110 // compatible and the loader cannot determine which is newer (e.g., the only changes are renames),
Chris@64 111 // the existing schema will be preferred. Note that in any case, the loader will end up keeping
Chris@64 112 // around copies of both schemas, so you shouldn't repeatedly reload schemas into the same loader.
Chris@64 113 //
Chris@64 114 // The following properties of the schema node are validated:
Chris@64 115 // - Struct size and preferred list encoding are valid and consistent.
Chris@64 116 // - Struct members are fields or unions.
Chris@64 117 // - Union members are fields.
Chris@64 118 // - Field offsets are in-bounds.
Chris@64 119 // - Ordinals and codeOrders are sequential starting from zero.
Chris@64 120 // - Values are of the right union case to match their types.
Chris@64 121 //
Chris@64 122 // You should assume anything not listed above is NOT validated. In particular, things that are
Chris@64 123 // not validated now, but could be in the future, include but are not limited to:
Chris@64 124 // - Names.
Chris@64 125 // - Annotation values. (This is hard because the annotation declaration is not always
Chris@64 126 // available.)
Chris@64 127 // - Content of default/constant values of pointer type. (Validating these would require knowing
Chris@64 128 // their schema, but even if the schemas are available at validation time, they could be
Chris@64 129 // updated by a subsequent load(), invalidating existing values. Instead, these values are
Chris@64 130 // validated at the time they are used, as usual for Cap'n Proto objects.)
Chris@64 131 //
Chris@64 132 // Also note that unknown types are not considered invalid. Instead, the dynamic API returns
Chris@64 133 // a DynamicValue with type UNKNOWN for these.
Chris@64 134
Chris@64 135 Schema loadOnce(const schema::Node::Reader& reader) const;
Chris@64 136 // Like `load()` but does nothing if a schema with the same ID is already loaded. In contrast,
Chris@64 137 // `load()` would attempt to compare the schemas and take the newer one. `loadOnce()` is safe
Chris@64 138 // to call even while concurrently using schemas from this loader. It should be considered an
Chris@64 139 // error to call `loadOnce()` with two non-identical schemas that share the same ID, although
Chris@64 140 // this error may or may not actually be detected by the implementation.
Chris@64 141
Chris@64 142 template <typename T>
Chris@64 143 void loadCompiledTypeAndDependencies();
Chris@64 144 // Load the schema for the given compiled-in type and all of its dependencies.
Chris@64 145 //
Chris@64 146 // If you want to be able to cast a DynamicValue built from this SchemaLoader to the compiled-in
Chris@64 147 // type using as<T>(), you must call this method before constructing the DynamicValue. Otherwise,
Chris@64 148 // as<T>() will throw an exception complaining about type mismatch.
Chris@64 149
Chris@64 150 kj::Array<Schema> getAllLoaded() const;
Chris@64 151 // Get a complete list of all loaded schema nodes. It is particularly useful to call this after
Chris@64 152 // loadCompiledTypeAndDependencies<T>() in order to get a flat list of all of T's transitive
Chris@64 153 // dependencies.
Chris@64 154
Chris@64 155 private:
Chris@64 156 class Validator;
Chris@64 157 class CompatibilityChecker;
Chris@64 158 class Impl;
Chris@64 159 class InitializerImpl;
Chris@64 160 class BrandedInitializerImpl;
Chris@64 161 kj::MutexGuarded<kj::Own<Impl>> impl;
Chris@64 162
Chris@64 163 void loadNative(const _::RawSchema* nativeSchema);
Chris@64 164 };
Chris@64 165
Chris@64 166 template <typename T>
Chris@64 167 inline void SchemaLoader::loadCompiledTypeAndDependencies() {
Chris@64 168 loadNative(&_::rawSchema<T>());
Chris@64 169 }
Chris@64 170
Chris@64 171 } // namespace capnp
Chris@64 172
Chris@64 173 #endif // CAPNP_SCHEMA_LOADER_H_