Ejemplo n.º 1
0
        internal static void SerializeValueForEmbed <TDoc, TCursor>(MegaloScriptModel model, IO.TagElementStream <TDoc, TCursor, string> s,
                                                                    ref int valueId)
            where TDoc : class
            where TCursor : class
        {
            bool is_global = false;
            MegaloScriptValueBase value = null;

            if (s.IsReading)
            {
                if (s.ReadAttributeOpt(kIsGlobalAttributeName, ref is_global) && is_global)
                {
                    s.ReadCursor(ref valueId);
                }
                else
                {
                    bool streamed_sans_ids = model.TagElementStreamSerializeFlags.EmbedObjectsWriteSansIds();
                    var  value_type        = ReadType(s, model.Database);

                    if (streamed_sans_ids)
                    {
                        valueId = TypeExtensions.kNone;                                         // set to NONE to automatically add
                    }
                    else
                    {
                        SerializeId(s, ref valueId);
                    }

                    value = model.Recreate(value_type, valueId);

                    if (streamed_sans_ids)                     // since the stream didn't have the id, we need to explicit set it via value
                    {
                        valueId = value.Id;
                    }
                }
            }
            else
            {
                value = model.Values[valueId];
                if (is_global = value.IsGlobal)
                {
                    s.WriteAttribute(kIsGlobalAttributeName, true);
                    s.WriteCursor(valueId);
                }
            }

            if (!is_global)             // stream non-global values essentially like locals
            {
                Contract.Assume(value != null);
                value.Serialize(model, s);
            }
        }