A game framework written with osu! in mind.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Constrain config manager to use enum and refine type argument names.

+48 -48
+29 -29
osu.Framework/Configuration/ConfigManager.cs
··· 11 11 12 12 namespace osu.Framework.Configuration 13 13 { 14 - public abstract class ConfigManager<T> : ITrackableConfigManager, IDisposable 15 - where T : struct 14 + public abstract class ConfigManager<TLookup> : ITrackableConfigManager, IDisposable 15 + where TLookup : struct, Enum 16 16 { 17 17 /// <summary> 18 18 /// Whether user specified configuration elements should be set even though a default was never specified. 19 19 /// </summary> 20 20 protected virtual bool AddMissingEntries => true; 21 21 22 - private readonly IDictionary<T, object> defaultOverrides; 22 + private readonly IDictionary<TLookup, object> defaultOverrides; 23 23 24 - protected readonly Dictionary<T, IBindable> ConfigStore = new Dictionary<T, IBindable>(); 24 + protected readonly Dictionary<TLookup, IBindable> ConfigStore = new Dictionary<TLookup, IBindable>(); 25 25 26 26 /// <summary> 27 - /// Initialise a new <see cref="ConfigManager{T}"/> 27 + /// Initialise a new <see cref="ConfigManager{TLookup}"/> 28 28 /// </summary> 29 - /// <param name="defaultOverrides">Dictionary of overrides which should take precedence over defaults specified by the <see cref="ConfigManager{T}"/> implementation.</param> 30 - protected ConfigManager(IDictionary<T, object> defaultOverrides = null) 29 + /// <param name="defaultOverrides">Dictionary of overrides which should take precedence over defaults specified by the <see cref="ConfigManager{TLookup}"/> implementation.</param> 30 + protected ConfigManager(IDictionary<TLookup, object> defaultOverrides = null) 31 31 { 32 32 this.defaultOverrides = defaultOverrides; 33 33 } ··· 40 40 { 41 41 } 42 42 43 - public BindableDouble Set(T lookup, double value, double? min = null, double? max = null, double? precision = null) 43 + public BindableDouble Set(TLookup lookup, double value, double? min = null, double? max = null, double? precision = null) 44 44 { 45 45 value = getDefault(lookup, value); 46 46 ··· 62 62 return bindable; 63 63 } 64 64 65 - public BindableFloat Set(T lookup, float value, float? min = null, float? max = null, float? precision = null) 65 + public BindableFloat Set(TLookup lookup, float value, float? min = null, float? max = null, float? precision = null) 66 66 { 67 67 value = getDefault(lookup, value); 68 68 ··· 84 84 return bindable; 85 85 } 86 86 87 - public BindableInt Set(T lookup, int value, int? min = null, int? max = null) 87 + public BindableInt Set(TLookup lookup, int value, int? min = null, int? max = null) 88 88 { 89 89 value = getDefault(lookup, value); 90 90 ··· 105 105 return bindable; 106 106 } 107 107 108 - public BindableBool Set(T lookup, bool value) 108 + public BindableBool Set(TLookup lookup, bool value) 109 109 { 110 110 value = getDefault(lookup, value); 111 111 ··· 124 124 return bindable; 125 125 } 126 126 127 - public BindableSize Set(T lookup, Size value, Size? min = null, Size? max = null) 127 + public BindableSize Set(TLookup lookup, Size value, Size? min = null, Size? max = null) 128 128 { 129 129 value = getDefault(lookup, value); 130 130 ··· 145 145 return bindable; 146 146 } 147 147 148 - public Bindable<U> Set<U>(T lookup, U value) 148 + public Bindable<TValue> Set<TValue>(TLookup lookup, TValue value) 149 149 { 150 150 value = getDefault(lookup, value); 151 151 152 - Bindable<U> bindable = GetOriginalBindable<U>(lookup); 152 + Bindable<TValue> bindable = GetOriginalBindable<TValue>(lookup); 153 153 154 154 if (bindable == null) 155 155 bindable = set(lookup, value); ··· 161 161 return bindable; 162 162 } 163 163 164 - protected virtual void AddBindable<TBindable>(T lookup, Bindable<TBindable> bindable) 164 + protected virtual void AddBindable<TBindable>(TLookup lookup, Bindable<TBindable> bindable) 165 165 { 166 166 ConfigStore[lookup] = bindable; 167 167 bindable.ValueChanged += _ => backgroundSave(); 168 168 } 169 169 170 - private TType getDefault<TType>(T lookup, TType fallback) 170 + private TValue getDefault<TValue>(TLookup lookup, TValue fallback) 171 171 { 172 172 if (defaultOverrides != null && defaultOverrides.TryGetValue(lookup, out object found)) 173 - return (TType)found; 173 + return (TValue)found; 174 174 175 175 return fallback; 176 176 } 177 177 178 - private Bindable<U> set<U>(T lookup, U value) 178 + private Bindable<TValue> set<TValue>(TLookup lookup, TValue value) 179 179 { 180 - Bindable<U> bindable = new Bindable<U>(value); 180 + Bindable<TValue> bindable = new Bindable<TValue>(value); 181 181 AddBindable(lookup, bindable); 182 182 return bindable; 183 183 } 184 184 185 - public U Get<U>(T lookup) => GetOriginalBindable<U>(lookup).Value; 185 + public TValue Get<TValue>(TLookup lookup) => GetOriginalBindable<TValue>(lookup).Value; 186 186 187 - protected Bindable<U> GetOriginalBindable<U>(T lookup) 187 + protected Bindable<TValue> GetOriginalBindable<TValue>(TLookup lookup) 188 188 { 189 189 if (ConfigStore.TryGetValue(lookup, out IBindable obj)) 190 190 { 191 - if (!(obj is Bindable<U>)) 192 - throw new InvalidCastException($"Cannot convert bindable of type {obj.GetType()} retrieved from {nameof(ConfigManager<T>)} to {typeof(Bindable<U>)}."); 191 + if (!(obj is Bindable<TValue>)) 192 + throw new InvalidCastException($"Cannot convert bindable of type {obj.GetType()} retrieved from {nameof(ConfigManager<TLookup>)} to {typeof(Bindable<TValue>)}."); 193 193 194 - return (Bindable<U>)obj; 194 + return (Bindable<TValue>)obj; 195 195 } 196 196 197 197 return null; ··· 203 203 /// a local reference. 204 204 /// </summary> 205 205 /// <returns>A weakly bound copy of the specified bindable.</returns> 206 - public Bindable<U> GetBindable<U>(T lookup) => GetOriginalBindable<U>(lookup)?.GetBoundCopy(); 206 + public Bindable<TValue> GetBindable<TValue>(TLookup lookup) => GetOriginalBindable<TValue>(lookup)?.GetBoundCopy(); 207 207 208 208 /// <summary> 209 209 /// Binds a local bindable with a configuration-backed bindable. 210 210 /// </summary> 211 - public void BindWith<U>(T lookup, Bindable<U> bindable) => bindable.BindTo(GetOriginalBindable<U>(lookup)); 211 + public void BindWith<TValue>(TLookup lookup, Bindable<TValue> bindable) => bindable.BindTo(GetOriginalBindable<TValue>(lookup)); 212 212 213 213 #region IDisposable Support 214 214 ··· 240 240 241 241 public void LoadInto(TrackedSettings settings) => settings.LoadFrom(this); 242 242 243 - public class TrackedSetting<U> : Tracking.TrackedSetting<U> 243 + public class TrackedSetting<TValue> : Tracking.TrackedSetting<TValue> 244 244 { 245 245 /// <summary> 246 - /// Constructs a new <see cref="TrackedSetting{U}"/>. 246 + /// Constructs a new <see cref="TrackedSetting{TValue}"/>. 247 247 /// </summary> 248 248 /// <param name="setting">The config setting to be tracked.</param> 249 249 /// <param name="generateDescription">A function that generates the description for the setting, invoked every time the value changes.</param> 250 - public TrackedSetting(T setting, Func<U, SettingDescription> generateDescription) 250 + public TrackedSetting(TLookup setting, Func<TValue, SettingDescription> generateDescription) 251 251 : base(setting, generateDescription) 252 252 { 253 253 }
+4 -4
osu.Framework/Configuration/IniConfigManager.cs
··· 10 10 11 11 namespace osu.Framework.Configuration 12 12 { 13 - public class IniConfigManager<T> : ConfigManager<T> 14 - where T : struct 13 + public class IniConfigManager<TLookup> : ConfigManager<TLookup> 14 + where TLookup : struct, Enum 15 15 { 16 16 /// <summary> 17 17 /// The backing file used to store the config. Null means no persistent storage. ··· 20 20 21 21 private readonly Storage storage; 22 22 23 - public IniConfigManager(Storage storage, IDictionary<T, object> defaultOverrides = null) 23 + public IniConfigManager(Storage storage, IDictionary<TLookup, object> defaultOverrides = null) 24 24 : base(defaultOverrides) 25 25 { 26 26 this.storage = storage; ··· 51 51 string key = line.Substring(0, equalsIndex).Trim(); 52 52 string val = line.Remove(0, equalsIndex + 1).Trim(); 53 53 54 - if (!Enum.TryParse(key, out T lookup)) 54 + if (!Enum.TryParse(key, out TLookup lookup)) 55 55 continue; 56 56 57 57 if (ConfigStore.TryGetValue(lookup, out IBindable b))
+3 -3
osu.Framework/Configuration/Tracking/ITrackedSetting.cs
··· 19 19 /// <summary> 20 20 /// Loads a <see cref="Bindable{T}"/> into this tracked setting, binding to <see cref="SettingChanged"/>. 21 21 /// </summary> 22 - /// <param name="configManager">The <see cref="ConfigManager{T}"/> to load from.</param> 23 - void LoadFrom<T>(ConfigManager<T> configManager) 24 - where T : struct; 22 + /// <param name="configManager">The <see cref="ConfigManager{TLookup}"/> to load from.</param> 23 + void LoadFrom<TLookup>(ConfigManager<TLookup> configManager) 24 + where TLookup : struct, Enum; 25 25 26 26 /// <summary> 27 27 /// Unloads the <see cref="Bindable{T}"/> from this tracked setting, unbinding from <see cref="SettingChanged"/>.
+10 -10
osu.Framework/Configuration/Tracking/TrackedSetting.cs
··· 9 9 /// <summary> 10 10 /// A singular tracked setting. 11 11 /// </summary> 12 - /// <typeparam name="U">The type of the tracked value.</typeparam> 13 - public abstract class TrackedSetting<U> : ITrackedSetting 12 + /// <typeparam name="TValue">The type of the tracked value.</typeparam> 13 + public abstract class TrackedSetting<TValue> : ITrackedSetting 14 14 { 15 15 public event Action<SettingDescription> SettingChanged; 16 16 17 17 private readonly object setting; 18 - private readonly Func<U, SettingDescription> generateDescription; 18 + private readonly Func<TValue, SettingDescription> generateDescription; 19 19 20 - private Bindable<U> bindable; 20 + private Bindable<TValue> bindable; 21 21 22 22 /// <summary> 23 - /// Constructs a new <see cref="TrackedSetting{U}"/>. 23 + /// Constructs a new <see cref="TrackedSetting{TValue}"/>. 24 24 /// </summary> 25 25 /// <param name="setting">The config setting to be tracked.</param> 26 26 /// <param name="generateDescription">A function that generates the description for the setting, invoked every time the value changes.</param> 27 - protected TrackedSetting(object setting, Func<U, SettingDescription> generateDescription) 27 + protected TrackedSetting(object setting, Func<TValue, SettingDescription> generateDescription) 28 28 { 29 29 this.setting = setting; 30 30 this.generateDescription = generateDescription; 31 31 } 32 32 33 - public void LoadFrom<T>(ConfigManager<T> configManager) 34 - where T : struct 33 + public void LoadFrom<TLookup>(ConfigManager<TLookup> configManager) 34 + where TLookup : struct, Enum 35 35 { 36 - bindable = configManager.GetBindable<U>((T)setting); 36 + bindable = configManager.GetBindable<TValue>((TLookup)setting); 37 37 bindable.ValueChanged += displaySetting; 38 38 } 39 39 ··· 42 42 bindable.ValueChanged -= displaySetting; 43 43 } 44 44 45 - private void displaySetting(ValueChangedEvent<U> args) => SettingChanged?.Invoke(generateDescription(args.NewValue)); 45 + private void displaySetting(ValueChangedEvent<TValue> args) => SettingChanged?.Invoke(generateDescription(args.NewValue)); 46 46 } 47 47 }
+2 -2
osu.Framework/Configuration/Tracking/TrackedSettings.cs
··· 10 10 { 11 11 public event Action<SettingDescription> SettingChanged; 12 12 13 - public void LoadFrom<T>(ConfigManager<T> configManager) 14 - where T : struct 13 + public void LoadFrom<TLookup>(ConfigManager<TLookup> configManager) 14 + where TLookup : struct, Enum 15 15 { 16 16 foreach (var value in this) 17 17 {