using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Leaf.Parallel
{
/// <summary>
/// Class used to bridge methods together, execute a method before or after a normal routine
/// or execute another method use return value of the first one(Continuous)
/// </summary>
public static class NBridge
{
#region PreExec: Execute a method before the normal routine
public static Func<TResult> PreExec<TResult>(Func<TResult> normalRoutine, NVoidFunc preRoutine)
{
return () =>
{
preRoutine();
return normalRoutine();
};
}
public static Func<TArgument1 , TResult> PreExec<TArgument1 , TResult>(Func<TArgument1 , TResult> normalRoutine, NVoidFunc preRoutine)
{
return (t1) =>
{
preRoutine();
return normalRoutine(t1);
};
}
public static Func<TArgument1, TArgument2, TResult> PreExec<TArgument1, TArgument2, TResult>(Func<TArgument1, TArgument2, TResult> normalRoutine, NVoidFunc preRoutine)
{
return (t1 , t2) =>
{
preRoutine();
return normalRoutine(t1 , t2);
};
}
public static Func<TArgument1, TArgument2, TArgument3, TResult> PreExec<TArgument1, TArgument2, TArgument3, TResult>(Func<TArgument1, TArgument2, TArgument3, TResult> normalRoutine, NVoidFunc preRoutine)
{
return (t1 ,t2 , t3) =>
{
preRoutine();
return normalRoutine(t1 , t2 , t3);
};
}
public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> PreExec<TArgument1, TArgument2, TArgument3, TArgument4, TResult>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> normalRoutine, NVoidFunc preRoutine)
{
return (t1, t2, t3,t4) =>
{
preRoutine();
return normalRoutine(t1, t2, t3 , t4);
};
}
#endregion
#region PostExec : execute another methods after the nornal one
public static Func<TResult> PostExec<TResult>(Func<TResult> normalRoutine, NVoidFunc postRoutine)
{
return () =>
{
TResult result = normalRoutine();
postRoutine();
return result;
};
}
public static Func<TArgument1, TResult> PostExec<TArgument1, TResult>(Func<TArgument1, TResult> normalRoutine, NVoidFunc postRoutine)
{
return (t1) =>
{
TResult result = normalRoutine(t1);
postRoutine();
return result;
};
}
public static Func<TArgument1, TArgument2, TResult> PostExec<TArgument1, TArgument2, TResult>(Func<TArgument1, TArgument2, TResult> normalRoutine, NVoidFunc postRoutine)
{
return (t1, t2) =>
{
TResult result = normalRoutine(t1, t2);
postRoutine();
return result;
};
}
public static Func<TArgument1, TArgument2, TArgument3, TResult> PostExec<TArgument1, TArgument2, TArgument3, TResult>(Func<TArgument1, TArgument2, TArgument3, TResult> normalRoutine, NVoidFunc postRoutine)
{
return (t1, t2, t3) =>
{
TResult result = normalRoutine(t1, t2, t3);
postRoutine();
return result;
};
}
public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> PostExec<TArgument1, TArgument2, TArgument3, TArgument4, TResult>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> normalRoutine, NVoidFunc postRoutine)
{
return (t1, t2, t3, t4) =>
{
TResult result = normalRoutine(t1, t2, t3, t4);
postRoutine();
return result;
};
}
#endregion
#region Bridge: Used to connect two methods.
public static Func<TResult2> Bridge<TResult1 , TResult2>(Func<TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
{
return () =>
{
TResult1 tempResult = firstFunc();
return nextFunc(tempResult);
};
}
public static Func<TArgument1, TResult2> Bridge<TArgument1, TResult1, TResult2>(Func<TArgument1, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
{
return (t1) =>
{
TResult1 tempResult = firstFunc(t1);
return nextFunc(tempResult);
};
}
public static Func<TArgument1, TArgument2, TResult2> Bridge<TArgument1, TArgument2, TResult1, TResult2>(Func<TArgument1, TArgument2, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
{
return (t1 , t2) =>
{
TResult1 tempResult = firstFunc(t1 , t2);
return nextFunc(tempResult);
};
}
public static Func<TArgument1, TArgument2, TArgument3, TResult2> Bridge<TArgument1, TArgument2, TArgument3, TResult1, TResult2>(Func<TArgument1, TArgument2, TArgument3, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
{
return (t1 , t2 , t3) =>
{
TResult1 tempResult = firstFunc(t1 , t2 , t3);
return nextFunc(tempResult);
};
}
public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult2> Bridge<TArgument1, TArgument2, TArgument3, TArgument4, TResult1, TResult2>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
{
return (t1 ,t2 ,t3 ,t4) =>
{
TResult1 tempResult = firstFunc(t1 , t2 , t3 , t4);
return nextFunc(tempResult);
};
}
#endregion
}
}