using System; using System.Collections.Generic; using System.Linq; namespace CC_Functions.Misc { public static class GenericExtensions { public static T get(this Dictionary dict, G key, T def) { if (!dict.ContainsKey(key)) dict[key] = def; return dict[key]; } public static T set(this Dictionary dict, G key, T val) { dict[key] = val; return dict[key]; } public static bool tryCast(this object o, out T parsed) { try { parsed = (T)o; return true; } catch { parsed = default(T); return false; } } public static G selectO(this T self, Func func) => func.Invoke(self); public static void runIf(bool condition, Action func) { if (condition) func(); } public static T ParseToEnum(string value) => (T)Enum.Parse(typeof(T), Enum.GetNames(typeof(T)).First(s => s.ToLower() == value.ToLower())); public static bool? ParseBool(string value) => (string.IsNullOrWhiteSpace(value) || value.ToLower() == "Indeterminate") ? (bool?)null : bool.Parse(value); public static bool AND(this bool? left, bool? right) => left.TRUE() && right.TRUE(); public static bool OR(this bool? left, bool? right) => left.TRUE() || right.TRUE(); public static bool XOR(this bool? left, bool? right) => left.OR(right) && (!left.AND(right)); public static bool TRUE(this bool? self) => self == true; public static bool FALSE(this bool? self) => self == false; public static bool NULL(this bool? self) => self == null; } }