-
Notifications
You must be signed in to change notification settings - Fork 1
General
Below is information about some of the classes from the PowerLib.System namespace.
There is a TryOut<T> structure that is designed to pass a value along with its validity flag and TryOut static class with methods for creating this structure. Used in methods to return value when it is impossible to use in output parameter.
Definition
public readonly struct TryOut<T> : IEquatable<TryOut<T>>
{
public static readonly TryOut<T> Failed;
public T? Value { get; }
public bool Success { get; }
}
public static class TryOut
{
public static ref readonly TryOut<T> Failure<T>()
=> ref TryOut<T>.Failed;
public static TryOut<T> Success<T>(T? value)
=> new(value);
}Static class Variable contains methods for replacing values by reference. Well suited for use in inline expressions.
Definition
public static class Variable
{
// Sets a new value to a variable passed by reference and returns the previous value.
public static T? Replace<T>(ref T? variable, T? value);
// Returns the value of the variable passed by reference and sets it to the default value.
public static T? Take<T>(ref T? variable);
}The Range and LongRange structures are used to work with elements ranges. They contain start index and element count properties and some methods.
Will be described a little later...
A static class PwrEnum contains methods and extensions to work with values of enumerated types.
Definition
public static class PwrEnum
{
// Convert Byte to TEnum
public static TEnum ToEnum<TEnum>(byte value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert UInt16 to TEnum
public static TEnum ToEnum<TEnum>(ushort value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert UInt32 to TEnum
public static TEnum ToEnum<TEnum>(uint value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert UInt64 to TEnum
public static TEnum ToEnum<TEnum>(ulong value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert SByte to TEnum
public static TEnum ToEnum<TEnum>(sbyte value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert Int16 to TEnum
public static TEnum ToEnum<TEnum>(short value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert Int32 to TEnum
public static TEnum ToEnum<TEnum>(int value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert Int64 to TEnum
public static TEnum ToEnum<TEnum>(long value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert object to TEnum
public static TEnum ToEnum<TEnum>(object value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Convert TEnum to value of underlying type
public static object ToUnderlying<TEnum>(TEnum value)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Parse TEnum flags from separated string.
// If item prefixed with '+' then flag added to value,
// if item prefixed with '-' then flag removed from value,
// if item prefixed with '!' then flag inverted in value.
public static TEnum ParseFlags<TEnum>(this TEnum value, string input, bool ignoreCase, params char[] separators)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Add flags to value
public static TEnum CombineFlags<TEnum>(this TEnum value, params TEnum[] flags)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Overlap flags with value
public static TEnum OverlapFlags<TEnum>(this TEnum value, params TEnum[] flags)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Remove flags from value
public static TEnum RemoveFlags<TEnum>(this TEnum value, params TEnum[] flags)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Inverse flags in value
public static TEnum InverseFlags<TEnum>(this TEnum value, TEnum mask)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Match two flags values
public static FlagsMatchResult MatchFlags<TEnum>(this TEnum xValue, TEnum yValue)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Determines whether the values of the flags parameter are set in the value.
public static bool IsFlagsSet<TEnum>(this TEnum value, TEnum flags)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
// Determines whether the values of the flags parameter are overlapped with the value.
public static bool IsFlagsOverlapped<TEnum>(this TEnum value, TEnum flags)
where TEnum : struct, Enum, IComparable, IConvertible, IFormattable;
}
public enum FlagsMatchResult
{
NonOverlap,
Overlap,
Equal,
Belong,
Enclose
}A read only struct DateTimeInterval contains the start date with time and interval duration. It allso contains some methods for working with values of this type.
Definition
public readonly struct DateTimeInterval : IEquatable<DateTimeInterval>
{
public DateTimeInterval(DateTime dateTime, TimeSpan timeSpan);
public DateTimeInterval(DateTime dateTimeStart, DateTime dateTimeEnd);
public DateTime DateTimeStart { get; }
public DateTime DateTimeEnd { get; }
public DateTime DateTime { get; }
public TimeSpan TimeSpan { get; }
public DateTimeInterval Shift(TimeSpan shift);
public DateTimeInterval Shift(TimeSpan shiftStart, TimeSpan shiftEnd);
public DateTimeIntervalMatchResult Match(DateTimeInterval dateTimeInterval);
public static DateTimeIntervalMatchResult Match(DateTimeInterval dateTimeFirst, DateTimeInterval dateTimeSecond);
}
public enum DateTimeIntervalMatchResult
{
Before,
After,
OverlapBefore,
OverlapAfter,
Equal,
Belong,
Enclose,
}Methods of the Safe class are for doing actions with the suppression of the exceptions that occur. The Invoke methods call the passed delegates. The predicate passed as a parameter determines which types of exceptions will be suppressed. Also, one of the parameters is a delegate that will be called in case of exception suppression (can be used for logging). There are exception filtering methods that can be used in the suppression predicate.
Definition
public static class Safe
{
public static IEnumerable<Exception> EnumerateExceptions(Exception exception, int maxDepth = -1);
public static bool SuppressException(Exception exception, bool suppress, bool strong, params Type[] exceptionTypes);
public static bool SuppressExceptions(IEnumerable<Exception> exceptions, bool suppress, bool strong, params Type[] exceptionTypes);
public static void Invoke(Action action, Predicate<Exception>? suppressPredicate = null, Action<Exception>? suppressAction = null);
public static T? Invoke<T>(Func<T?> functor, T? defaultResult = default, Predicate<Exception>? suppressPredicate = null, Action<Exception>? suppressAction = null);
public static T? Invoke<T>(Func<T?> functor, Predicate<Exception>? suppressPredicate = default, Func<Exception, T?>? suppressFunctor = default);
}