Esempio n. 1
0
        private static void SaveList(Pickler /*!*/ pickler, object obj)
        {
            Debug.Assert(obj.GetType() == typeof(List <object>), "arg must be list");
            Debug.Assert(!pickler.MemoContains(obj));

            int index = pickler.MemoizeNew(obj);

            pickler.Write(Opcode.EmptyList);

            pickler.WritePut(index);
            pickler.BatchAppends(((IEnumerable)obj).GetEnumerator());
        }
Esempio n. 2
0
        private static void SaveDict(Pickler /*!*/ pickler, object obj)
        {
            Debug.Assert(obj.GetType() == typeof(Dictionary <string, object>), "arg must be dict");
            Debug.Assert(!pickler.MemoContains(obj));

            int index = pickler.MemoizeNew(obj);

            pickler.Write(Opcode.EmptyDict);

            pickler.WritePut(index);

            pickler.BatchSetItems((Dictionary <string, object>)obj);
        }
Esempio n. 3
0
        private static void SaveTuple(Pickler /*!*/ pickler, object obj)
        {
            Debug.Assert(obj.GetType() == typeof(object[]), "arg must be array");
            Debug.Assert(!pickler.MemoContains(obj));
            object[] t = (object[])obj;
            byte     opcode;
            bool     needMark = false;
            int      len      = t.Length;

            if (len == 0)
            {
                opcode = Opcode.EmptyTuple;
            }
            else if (len == 1)
            {
                opcode = Opcode.Tuple1;
            }
            else if (len == 2)
            {
                opcode = Opcode.Tuple2;
            }
            else if (len == 3)
            {
                opcode = Opcode.Tuple3;
            }
            else
            {
                opcode   = Opcode.Tuple;
                needMark = true;
            }

            if (needMark)
            {
                pickler.Write(Opcode.Mark);
            }
            var data = t;

            for (int i = 0; i < data.Length; i++)
            {
                pickler.Save(data[i]);
            }

            if (len > 0)
            {
                if (pickler.MemoContains(obj))
                {
                    // recursive tuple
                    for (int i = 0; i < len; i++)
                    {
                        pickler.Write(Opcode.Pop);
                    }
                    pickler.WriteGet(obj);
                    return;
                }

                pickler.Write(opcode);

                pickler.Memoize(t);
                pickler.WritePut(t);
            }
            else
            {
                pickler.Write(opcode);
            }
        }