public static void Serialize <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s,
                                                     ref EngineBuildHandle value)
            where TDoc : class
            where TCursor : class
        {
            bool reading = s.IsReading;

            var engine_index = reading
                                ? TypeExtensions.kNone
                                : value.EngineIndex;
            var branch_index = reading
                                ? TypeExtensions.kNone
                                : value.BranchIndex;
            var revisn_index = reading
                                ? TypeExtensions.kNone
                                : value.RevisionIndex;

            if (BlamEngine.SerializeId(s, kAttributeNameEngine, ref engine_index, true))
            {
                var repo = EngineRegistry.Engines[engine_index].BuildRepository;

                if (repo.SerializeBranchId(s, kAttributeNameBranch, ref branch_index, true))
                {
                    var branch = repo.Branches[branch_index];

                    branch.SerializeRevisionId(s, kAttributeNameRevisn, ref revisn_index, true);
                }
            }

            if (reading)
            {
                value = new EngineBuildHandle(engine_index, branch_index, revisn_index);
            }
        }
        /// <summary>Serialize a build handle, using a 'baseline' to populate or cull (from writing) root build information</summary>
        /// <param name="s"></param>
        /// <param name="baseline">Root build information (ie, engine, or engine and branch)</param>
        /// <param name="value"></param>
        public static void SerializeWithBaseline <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s,
                                                                 EngineBuildHandle baseline,
                                                                 ref EngineBuildHandle value)
            where TDoc : class
            where TCursor : class
        {
            int engine_index             = baseline.EngineIndex;
            int branch_index             = baseline.BranchIndex;
            int revisn_index             = baseline.RevisionIndex;
            EngineBuildRepository repo   = null;
            EngineBuildBranch     branch = null;

            if (s.IsWriting)
            {
                #region prepare engine_index
                if (value.EngineIndex == engine_index)
                {
                    repo = EngineRegistry.Engines[engine_index].BuildRepository;
                    // cause engine_index not to be written
                    engine_index = TypeExtensions.kNone;
                }
                else
                {
                    engine_index = value.EngineIndex;
                }
                #endregion

                #region prepare branch_index
                if (value.BranchIndex == branch_index)
                {
                    branch = repo.Branches[branch_index];
                    // cause branch_index not to be written
                    branch_index = TypeExtensions.kNone;
                }
                else
                {
                    branch_index = value.BranchIndex;
                }
                #endregion

                #region prepare revisn_index
                if (value.BranchIndex == revisn_index)
                {
                    // cause branch_index not to be written
                    branch_index = TypeExtensions.kNone;
                }
                else
                {
                    revisn_index = value.RevisionIndex;
                }
                #endregion
            }

            // This is a logic driven mess, but having branches for both IsReading and IsWriting would result in more copy&paste code

            // reading: baseline EngineIndex is valid, or index is serialized
            // writing: value EngineIndex mismatches baseline
            if (engine_index.IsNotNone() || BlamEngine.SerializeId(s, kAttributeNameEngine, ref engine_index, true))
            {
                repo = EngineRegistry.Engines[engine_index].BuildRepository;
            }

            // precondition: someone's EngineIndex was valid
            // reading: baseline BranchIndex is valid, or index is serialized
            // writing: value BranchIndex mismatches baseline
            if (repo != null && (branch_index.IsNotNone() || repo.SerializeBranchId(s, kAttributeNameBranch, ref branch_index, true)))
            {
                branch = repo.Branches[branch_index];
            }

            // precondition: someone's BranchIndex was valid
            // reading: baseline RevisionIndex is valid
            // writing: value RevisionIndex mismatches baseline
            if (branch != null && (revisn_index.IsNotNone() || s.IsReading))
            {
                branch.SerializeRevisionId(s, kAttributeNameRevisn, ref revisn_index, true);
            }

            if (s.IsReading)
            {
                if (engine_index.IsNotNone())
                {
                    value = new EngineBuildHandle(engine_index, branch_index, revisn_index);
                }
                else                 // engine_index is NONE, don't even bother trying to encode a new handle that should be all NONE anyway
                {
                    value = EngineBuildHandle.None;
                }
            }
        }