초기커밋

This commit is contained in:
2025-05-01 07:20:41 +09:00
commit 98bb2e3c5c
2747 changed files with 646947 additions and 0 deletions

View File

@@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore;
public static class LambdaSafeHelper
{
//=========================================================================================
// with for
//=========================================================================================
public static void forLambda<T>(IEnumerable<T> source, Action<T> action)
{
if (source is IList<T> list)
{
for (int i = 0; i < list.Count; ++i)
{
var node_item = list[i];
action(node_item);
}
}
else
{
throw new ArgumentException("Source must implement IList<T> for indexed access !!!", nameof(source));
}
}
public static async Task forLambdaAsync<T>(IEnumerable<T> source, Func<T, Task> action)
{
if (source is IList<T> list)
{
for (int i = 0; i < list.Count; ++i)
{
var node_item = list[i];
await action(node_item);
}
}
else
{
throw new ArgumentException("Source must implement IList<T> for indexed access !!!", nameof(source));
}
}
public static void forLambdaSafe<T>(IEnumerable<T> source, Action<T> action, Action<Exception> onException)
{
if (source is IList<T> list)
{
for (int i = 0; i < list.Count; ++i)
{
var node_item = list[i];
try { action(node_item); }
catch (Exception e) { onException(e); }
}
}
else
{
throw new ArgumentException("Source must implement IList<T> for indexed access !!!", nameof(source));
}
}
public static async Task forLambdaSafeAsync<T>( IEnumerable<T> source
, Func<T, Task> action, Func<Exception, Task> onException )
{
if (source is IList<T> list)
{
for (int i = 0; i < list.Count; ++i)
{
var node_item = list[i];
try { await action(node_item); }
catch (Exception e) { await onException(e); }
}
}
else
{
throw new ArgumentException("Source must implement IList<T> for indexed access !!!", nameof(source));
}
}
//=========================================================================================
// with foreach
//=========================================================================================
public static void forEachLambda<T>(IEnumerable<T> source, Action<T> action)
{
foreach (var node_item in source)
{
action(node_item);
}
}
public static async Task forEachLambdaAsync<T>(IEnumerable<T> source, Func<T, Task> action)
{
foreach (var node_item in source)
{
await action(node_item);
}
}
public static void forEachLambdaSafe<T>(IEnumerable<T> source, Action<T> action, Action<Exception> onException)
{
foreach (var node_item in source)
{
try { action(node_item); }
catch (Exception e) { onException(e); }
}
}
public static async Task forEachLambdaSafeAsync<T>(IEnumerable<T> source, Func<T, Task> action, Func<Exception, Task> onException)
{
foreach (var node_item in source)
{
try { await action(node_item); }
catch (Exception e) { await onException(e); }
}
}
//=========================================================================================
// with while
//=========================================================================================
public static void whileWithConditionLambda<T>(IEnumerable<T> source, Func<T, bool> condition, Action<T> action)
{
using var enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
var node_item = enumerator.Current;
if (false == condition(node_item)) { break; }
action(node_item);
}
}
public static async Task whileWithConditionLambdaAsync<T>(IEnumerable<T> source, Func<T, Task<bool>> condition, Func<T, Task> action)
{
using var enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
var node_item = enumerator.Current;
if (false == await condition(node_item)) { break; }
await action(node_item);
}
}
public static void whileWithConditionLambdaSafe<T>(IEnumerable<T> source, Func<T, bool> condition, Action<T> action, Action<Exception> onException)
{
using var enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
var node_item = enumerator.Current;
if (false == condition(node_item)) { break; }
try { action(node_item); }
catch (Exception e) { onException(e); }
}
}
public static async Task whileWithConditionLambdaSafeAsync<T>(IEnumerable<T> source, Func<T, Task<bool>> condition, Func<T, Task> action, Func<Exception, Task> onException)
{
using var enumerator = source.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
var node_item = enumerator.Current;
if (false == await condition(node_item)) { break; }
await action(node_item);
}
}
catch (Exception e)
{
await onException(e);
}
}
//=========================================================================================
// try-catch only
//=========================================================================================
public static void lambdaSafe(Action action, Action<Exception>? onException = null)
{
try { action(); }
catch (Exception e) { onException?.Invoke(e); }
}
public static async Task lambdaSafeAsync(Func<Task> action, Func<Exception, Task>? onException = null)
{
try { await action(); }
catch (Exception e)
{
if (onException != null)
{
await onException(e);
}
}
}
}