/// <summary>
        /// Create a <see cref="ModelSaveContext"/> supporting saving to a repository, for implementors of <see cref="ICanSaveModel"/>.
        /// </summary>
        internal ModelSaveContext(RepositoryWriter rep, string dir, string name)
        {
            Contracts.CheckValue(rep, nameof(rep));
            Repository = rep;
            _ectx      = rep.ExceptionContext;

            _ectx.CheckValueOrNull(dir);
            _ectx.CheckNonEmpty(name, nameof(name));

            Directory = dir;
            Strings   = new NormStr.Pool();

            _ent = rep.CreateEntry(dir, name);
            try
            {
                Writer = new BinaryWriter(_ent.Stream, Encoding.UTF8, leaveOpen: true);
                try
                {
                    ModelHeader.BeginWrite(Writer, out FpMin, out Header);
                }
                catch
                {
                    Writer.Dispose();
                    throw;
                }
            }
            catch
            {
                _ent.Dispose();
                throw;
            }
        }
        /// <summary>
        /// Create a <see cref="ModelSaveContext"/> supporting saving to a single-stream, for implementors of <see cref="ICanSaveInBinaryFormat"/>.
        /// </summary>
        internal ModelSaveContext(BinaryWriter writer, IExceptionContext ectx = null)
        {
            Contracts.AssertValueOrNull(ectx);
            _ectx = ectx;
            _ectx.CheckValue(writer, nameof(writer));

            Repository = null;
            Directory  = null;
            _ent       = null;

            Strings = new NormStr.Pool();
            Writer  = writer;
            ModelHeader.BeginWrite(Writer, out FpMin, out Header);
        }
        /// <summary>
        /// Create a ModelLoadContext supporting loading from a repository, for implementors of ICanSaveModel.
        /// </summary>
        internal ModelLoadContext(RepositoryReader rep, Repository.Entry ent, string dir)
        {
            Contracts.CheckValue(rep, nameof(rep));
            Repository = rep;
            _ectx      = rep.ExceptionContext;

            _ectx.CheckValue(ent, nameof(ent));
            _ectx.CheckValueOrNull(dir);

            Directory = dir;

            Reader = new BinaryReader(ent.Stream, Encoding.UTF8, leaveOpen: true);
            try
            {
                ModelHeader.BeginRead(out FpMin, out Header, out Strings, out LoaderAssemblyName, Reader);
            }
            catch
            {
                Reader.Dispose();
                throw;
            }
        }
示例#4
0
 /// <summary>
 /// Load from the given repository entry using the default loader(s) specified in the header.
 /// </summary>
 public static void LoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
     where TRes : class
 {
     Contracts.CheckValue(env, nameof(env));
     env.CheckValue(rep, nameof(rep));
     if (!TryLoadModel <TRes, TSig>(env, out result, rep, ent, dir, extra))
     {
         throw env.ExceptDecode("Couldn't load model: '{0}'", dir);
     }
 }
示例#5
0
        /// <summary>
        /// Try to load from the given repository entry using the default loader(s) specified in the header.
        /// Returns false iff the default loader(s) could not be bound to a compatible loadable class.
        /// </summary>
        private static bool TryLoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
            where TRes : class
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            long fp = ent.Stream.Position;

            using (var ctx = new ModelLoadContext(rep, ent, dir))
            {
                env.Assert(fp == ctx.FpMin);
                if (ctx.TryLoadModelCore <TRes, TSig>(env, out result, extra))
                {
                    return(true);
                }
            }

            // TryLoadModelCore should rewind on failure.
            Contracts.Assert(fp == ent.Stream.Position);
            return(false);
        }