annotate win64-msvc/include/capnp/schema-loader.h @ 58:eab3b14ddc95

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