using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
namespace LibGit2Sharp
{
///
/// Class to handle the mapping between libgit2 progress_cb callback on the git_checkout_opts
/// structure to the CheckoutProgressHandler delegate.
///
internal class CheckoutCallbacks
{
///
/// Managed delegate to call in response to checkout progress_cb callback.
///
private readonly CheckoutProgressHandler onCheckoutProgress;
///
/// Constructor to set up native callback for given managed delegate.
///
/// delegate to call in response to checkout progress_cb
private CheckoutCallbacks(CheckoutProgressHandler onCheckoutProgress)
{
this.onCheckoutProgress = onCheckoutProgress;
}
///
/// Generate a delegate matching the signature of the native progress_cb callback and wraps the delegate.
///
/// that should be wrapped in the native callback.
/// The delegate with signature matching the expected native callback.
internal static progress_cb GenerateCheckoutCallbacks(CheckoutProgressHandler onCheckoutProgress)
{
if (onCheckoutProgress == null)
{
return null;
}
return new CheckoutCallbacks(onCheckoutProgress).OnGitCheckoutProgress;
}
///
/// The delegate with a signature that matches the native checkout progress_cb function's signature.
///
/// The path that was updated.
/// The number of completed steps.
/// The total number of steps.
/// Payload object.
private void OnGitCheckoutProgress(IntPtr str, UIntPtr completedSteps, UIntPtr totalSteps, IntPtr payload)
{
// Convert null strings into empty strings.
string path = (str != IntPtr.Zero) ? Utf8Marshaler.FromNative(str) : string.Empty;
onCheckoutProgress(path, (int)completedSteps, (int)totalSteps);
}
}
}