Exemplo n.º 1
0
        /// <summary>
        /// Create VectorOffset from List
        /// </summary>
        /// <typeparam name="T">IFBSerializable</typeparam>
        /// <typeparam name="S">FlatbufferType</typeparam>
        /// <param name="builder"></param>
        /// <param name="list"></param>
        /// <param name="fbCreateList"></param>
        /// <returns></returns>
        public static FlatBuffers.VectorOffset?CreateList <T, S>(FlatBuffers.FlatBufferBuilder builder
                                                                 , List <T> list, Func <FlatBufferBuilder, Offset <S>[], VectorOffset> fbCreateList)
        where S : struct, FlatBuffers.IFlatbufferObject where T : IFBSerializable
        {
            if (list == null)
            {
                return(null);
            }

            if (FlatbufferSerializer.obj2FSMapping.TryGetValue(list, out int bufPos))
            {
                // the list was already serialized so we need to use this VectorOffset in order to keep the reference
                var result = new FlatBuffers.VectorOffset(bufPos);
                return(result);
            }
            else
            {
                if (typeof(IFBSerializable).IsAssignableFrom(typeof(T)))
                {
                    var tempArray = new FlatBuffers.Offset <S> [list.Count];
                    for (int i = 0; i < list.Count; i++)
                    {
                        var listElemOffset = FlatbufferSerializer.GetOrCreateSerialize(builder, (IFBSerializable)list[i]);
                        if (listElemOffset != null)
                        {
                            tempArray[i] = new FlatBuffers.Offset <S>((int)listElemOffset);
                        }
                    }
                    var result = fbCreateList(builder, tempArray);
                    FlatbufferSerializer.obj2FSMapping[list] = result.Value;
                    return(result);
                }
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserialized a list with NON-Primitive content
        /// </summary>
        /// <typeparam name="T">The result-type e.g. UID,... that implements IFBSerializable</typeparam>
        /// <typeparam name="S">The FBType that contained in the fb-list</typeparam>
        /// <param name="bufferPos">The bufferposition of this list</param>
        /// <param name="amount">The amount of elements this list contains</param>
        /// <param name="getItem">The function that returns the corresponding list-item wrapped into a to the parameter</param>
        /// <returns></returns>
        public static List <T> DeserializeList <T, S>(int bufferPos, int amount, List <object> items) where S : IFlatbufferObject where T : IFBSerializable, new()
        {
            if (bufferPos == 0)
            {
                return(null);
            }

            if (fb2objMapping.TryGetValue(bufferPos, out object value))
            {
                // we already deserialized this list give back the already created version to keep the reference
                return((List <T>)value);
            }
            else
            {
                var result = new List <T>();
                for (int i = 0; i < amount; i++)
                {
                    var obj = items[i];
                    if (obj != null)
                    {
                        var deserializedElement = FlatbufferSerializer.GetOrCreateDeserialize <T>((S)obj);
                        result.Add(deserializedElement);
                    }
                }
                fb2objMapping[bufferPos] = result;
                return(result);
            }
        }