Example #1
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);
            }
        }