Files
spark/src/client/spark_core/utils/ConvertUtils.cs
T

164 lines
5.9 KiB
C#

namespace System {
using bts;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
public static class ConvertUtils {
public static object try_convert(Type type, object obj) {
try_convert(type, obj, out object result);
return result;
}
#nullable enable
public static bool try_convert<T>(
object obj,
[NotNullWhen(true)]out T? result,
LogLevel? log_level = null,
LogSubsystem log_subsystem = LogSubsystem.gen,
string? name = null) {
if (obj is T t) {
result = t;
return true;
}
if (try_convert(typeof(T), obj, out var result_obj)) {
result = (T)result_obj;
return true;
}
if (log_level.HasValue) {
name = name is null ? "obj" : $"'{name}'";
var msg = $"Expected {name} to be of type '{typeof(T)}', actually " +
$"'{obj?.GetType()?.ToString() ?? "null"}'.";
Log.log(new(log_subsystem, log_level.Value, msg));
}
result = default;
return false;
}
#nullable disable
public static bool try_convert(Type type, object obj, out object result) {
if (obj == null) {
result = get_default(type);
return false;
} else if (type.IsPrimitive && obj is IConvertible) {
result = Convert.ChangeType(obj, type);
return true;
} else if (type.IsAssignableFrom(obj.GetType())) {
result = obj;
return true;
} else if (type.IsEnum && obj is string) {
result = try_parse_enum(type, obj.ToString());
return true;
} else if (type.IsNullable()) {
if (try_convert(type.GetGenericArguments()[0], obj, out result)) {
return true;
}
result = null;
return false;
} else {
result = get_default(type);
return false;
}
}
public static T try_convert<T>(object obj) =>
(T)try_convert(typeof(T), obj);
public static T try_convert<T>(object obj, T default_value) =>
try_convert(obj, out T result) ? result : default_value;
public static bool try_convert<T>(object obj, out T result) {
bool rv = try_convert(typeof(T), obj, out object r_obj);
result = (T)r_obj;
return rv;
}
public static object get_default(Type type) =>
type.IsValueType ? Activator.CreateInstance(type) : null;
public static T ValueOrDefault<T>(T? nullable) where T : struct => nullable.HasValue ? nullable.Value : default;
public static bool is_compatible<T>(object obj) { return obj is T || (obj == null && default(T) == null); }
public static bool try_convert_args<T1, T2>(object[] args, out T1 arg1, out T2 arg2) {
if (args == null || args.Length != 2) {
arg1 = default;
arg2 = default;
return false;
}
bool rv = try_convert(args[0], out arg1);
rv |= try_convert(args[1], out arg2);
return rv;
}
public static bool try_convert_args<T1, T2, T3>(object[] args, out T1 arg1, out T2 arg2, out T3 arg3) {
if (args == null || args.Length != 3) {
arg1 = default;
arg2 = default;
arg3 = default;
return false;
}
bool rv = try_convert(args[0], out arg1);
rv |= try_convert(args[1], out arg2);
rv |= try_convert(args[2], out arg3);
return rv;
}
public static bool try_convert_args<T1, T2, T3, T4>(object[] args, out T1 arg1, out T2 arg2, out T3 arg3, out T4 arg4) {
if (args == null || args.Length != 4) {
arg1 = default;
arg2 = default;
arg3 = default;
arg4 = default;
return false;
}
bool rv = try_convert(args[0], out arg1);
rv |= try_convert(args[1], out arg2);
rv |= try_convert(args[2], out arg3);
rv |= try_convert(args[3], out arg4);
return rv;
}
public static bool TryConvertArgs<T1, T2, T3, T4, T5>(IEnumerable<object> args, out T1 arg1, out T2 arg2, out T3 arg3, out T4 arg4, out T5 arg5) {
using var en = args.GetEnumerator();
bool success = true;
convertNext(en, ref success, out arg1);
convertNext(en, ref success, out arg2);
convertNext(en, ref success, out arg3);
convertNext(en, ref success, out arg4);
convertNext(en, ref success, out arg5);
return success;
}
public static object try_parse_enum(Type enum_type, string value, bool ignore_case = true) {
if (!enum_type.IsEnum) throw new ArgumentException("Must an enum type.", nameof(enum_type));
Array vals = Enum.GetValues(enum_type);
var str_comp = ignore_case ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
foreach (object v in vals) {
if (string.Equals(v.ToString(), value, str_comp))
return v;
}
return Activator.CreateInstance(enum_type);
}
public static bool update_value_type<T>(ref T field, T newval) where T : struct, IEquatable<T> {
bool rv = !field.Equals(newval);
field = newval;
return rv;
}
static void convertNext<T>(IEnumerator<object> enumerator, ref bool success, out T arg) {
if (!success || !enumerator.MoveNext()) {
arg = default;
success = false;
}
success = try_convert(enumerator.Current, out arg);
}
public static class CastDelegate<T, TResult> {
public static readonly Func<T, TResult> value =
v => v is TResult r ? r : throw new InvalidCastException($"Failed to cast element in source collection from type {typeof(T)} to {typeof(TResult)}.");
}
}
}