Exemplo n.º 1
0
        public static void Register <T>(ushort typeId) where T : IObject, new()
        {
            var r = typeTypeIdMappings.Add(typeof(T), typeId);

            System.Diagnostics.Debug.Assert(r.success || typeTypeIdMappings.ValueAt(r.index) == typeId);
            TypeId <T> .value             = typeId;
            typeIdCreatorMappings[typeId] = () => { return(new T()); };
        }
Exemplo n.º 2
0
        public void Read <T>(ref T v) where T : IObject
        {
            var typeId = (ushort)0;

            Read(ref typeId);
            if (typeId == 0)
            {
                v = default(T);
                return;
            }
            if (typeId == 1)
            {
                throw new Exception("Read<T> does not support string type");
            }
            if (idxStore != null)
            {
                uint ptr_offset = 0, bb_offset_bak = (uint)this.offset - (uint)offsetRoot;
                Read(ref ptr_offset);
                if (ptr_offset == bb_offset_bak)
                {
                    v = (T)CreateByTypeId(typeId);
                    System.Diagnostics.Debug.Assert(v != null);
                    idxStore.Add(ptr_offset, v);
                    v.FromBBuffer(this);
                }
                else
                {
                    int idx = idxStore.Find(ptr_offset);
                    if (idx == -1)
                    {
                        throw new Exception("ptr_offset is not found");
                    }
                    v = (T)idxStore.ValueAt(idx);
                    System.Diagnostics.Debug.Assert(v != null);
                }
            }
            else
            {
                v = (T)CreateByTypeId(typeId);
                System.Diagnostics.Debug.Assert(v != null);
                v.FromBBuffer(this);
            }
        }
Exemplo n.º 3
0
        public bool Add(V value, K0 key0, K1 key1)
        {
            var d = new Data {
                value = value
            };
            var r0 = d0.Add(key0, d);

            if (!r0.success)
            {
                return(false);
            }

            var r1 = d1.Add(key1, d);

            if (!r1.success)
            {
                d0.RemoveAt(r0.index);
                return(false);
            }

            return(true);
        }
Exemplo n.º 4
0
 public void Write <T>(T v) where T : IObject
 {
     if (v == null)
     {
         Write((ushort)0);
     }
     else
     {
         System.Diagnostics.Debug.Assert(v.GetPackageId() != ushort.MaxValue);   // 通常是没有执行 XXXPKG.AllTypes.Register() 所致
         Write(v.GetPackageId());
         if (ptrStore != null)
         {
             var rtv = ptrStore.Add(v, (uint)(dataLen - offsetRoot));        // 试将 v 和 相对offset 放入字典, 得到下标和是否成功
             Write(ptrStore.ValueAt(rtv.index));                             // 取 offset ( 不管是否成功 )
             if (!rtv.success)
             {
                 return;                                                     // 如果首次出现就序列化类本体
             }
         }
         v.ToBBuffer(this);
     }
 }