// dnlib: See LICENSE.txt for more info
using System;
namespace dnlib.DotNet {
///
/// Recursion counter
///
struct RecursionCounter {
///
/// Max recursion count. If this is reached, we won't continue, and will use a default value.
///
public const int MAX_RECURSION_COUNT = 100;
int counter;
///
/// Gets the recursion counter
///
public int Counter => counter;
///
/// Increments if it's not too high. ALL instance methods
/// that can be called recursively must call this method and
/// (if this method returns true)
///
/// true if it was incremented and caller can continue, false if
/// it was not incremented and the caller must return to its caller.
public bool Increment() {
if (counter >= MAX_RECURSION_COUNT)
return false;
counter++;
return true;
}
///
/// Must be called before returning to caller if
/// returned true.
///
public void Decrement() {
#if DEBUG
if (counter <= 0)
throw new InvalidOperationException("recursionCounter <= 0");
#endif
counter--;
}
///
public override string ToString() => counter.ToString();
}
}