annotate osx/include/capnp/schema-loader.h @ 54:5f67a29f0fc7

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