Exemple #1
0
        private static void PersistHashSetValueImpl <T>(this StatePersister persister, HashSet <T> value, PersistListItemCallback <T> callback)
        {
            var count = (ushort)value.Count;

            persister.PersistUInt16(ref count);

            persister.BeginArray("Items");

            if (persister.Mode == StatePersistMode.Read)
            {
                value.Clear();

                for (var i = 0; i < count; i++)
                {
                    var item = default(T);
                    callback(persister, ref item);
                    value.Add(item);
                }
            }
            else
            {
                foreach (var item in value)
                {
                    var itemCopy = item;
                    callback(persister, ref itemCopy);
                }
            }

            persister.EndArray();
        }
Exemple #2
0
        private static void PersistListImpl <T>(this StatePersister persister, List <T> value, uint count, PersistListItemCallback <T> callback)
        {
            persister.BeginArray("Items");

            if (persister.Mode == StatePersistMode.Read)
            {
                value.Clear();

                for (var i = 0; i < count; i++)
                {
                    var item = default(T);
                    callback(persister, ref item);
                    value.Add(item);
                }
            }
            else
            {
                for (var i = 0; i < count; i++)
                {
                    var item = value[i];
                    callback(persister, ref item);
                }
            }

            persister.EndArray();
        }
Exemple #3
0
        public static void PersistArray <T>(this StatePersister persister, T[] value, PersistListItemCallback <T> callback, [CallerArgumentExpression("value")] string name = "")
        {
            persister.BeginArray(name);

            for (var i = 0; i < value.Length; i++)
            {
                callback(persister, ref value[i]);
            }

            persister.EndArray();
        }
Exemple #4
0
        public static void PersistBitArray <TEnum>(this StatePersister persister, ref BitArray <TEnum> result, [CallerArgumentExpression("result")] string name = "")
            where TEnum : Enum
        {
            persister.BeginObject(name);

            persister.PersistVersion(1);

            if (persister.Mode == StatePersistMode.Read)
            {
                result.SetAll(false);
            }

            var count = (uint)result.NumBitsSet;

            persister.PersistUInt32(ref count);

            persister.BeginArray("Items");

            if (persister.Mode == StatePersistMode.Read)
            {
                var stringToValueMap = Data.Ini.IniParser.GetEnumMap <TEnum>();

                for (var i = 0; i < count; i++)
                {
                    string stringValue = default;
                    persister.PersistAsciiStringValue(ref stringValue);

                    var enumValue = (TEnum)stringToValueMap[stringValue];

                    result.Set(enumValue, true);
                }
            }
            else
            {
                var valueToStringMap = Data.Ini.IniParser.GetEnumMapReverse <TEnum>();

                foreach (var setBit in result.GetSetBits())
                {
                    var stringValue = valueToStringMap[setBit];

                    persister.PersistAsciiStringValue(ref stringValue);
                }
            }

            persister.EndArray();

            persister.EndObject();
        }
Exemple #5
0
        private static void PersistDictionaryImpl <TKey, TValue>(this StatePersister persister, Dictionary <TKey, TValue> value, uint count, PersistDictionaryItemCallback <TKey, TValue> callback)
        {
            persister.BeginArray("Items");

            if (persister.Mode == StatePersistMode.Read)
            {
                value.Clear();

                for (var i = 0; i < count; i++)
                {
                    persister.BeginObject();

                    var itemKey   = default(TKey);
                    var itemValue = default(TValue);
                    callback(persister, ref itemKey, ref itemValue);
                    value.Add(itemKey, itemValue);

                    persister.EndObject();
                }
            }
            else
            {
                foreach (var item in value)
                {
                    persister.BeginObject();

                    var itemKey   = item.Key;
                    var itemValue = item.Value;
                    callback(persister, ref itemKey, ref itemValue);

                    persister.EndObject();
                }
            }

            persister.EndArray();
        }