Limit the number of times a function can be called.
Given a delegate dg and an integer maxCalls, this function
returns a functor with the same parameter and return types as dg,
and that forwards up to maxCalls calls to dg. On the maxCalls+1th
call it throws an exception. This is useful for algorithms such
as scid.nonlinear.findRoot() which may require an arbitrary
number of function calls to complete.
In the example above, when we're taking the address of f, its
context may be copied to the heap. This is a potentially expensive
operation. To avoid it, use the unsafe function scopeLimitCalls()
instead. This should only be used if you are absolutely sure that
the returned delegate never escapes the current scope.
aliasvoiddelegate(int) DgType;
// Here, the delegate escapes by being assigned to a variable// outside the function scope.DgTypesomeGlobal;
voidbadIdea1()
{
voidf(inti) { ... }
someGlobal = scopeLimitCalls(&f, 10);
}
// Here it escapes because we return it.DgTypebadIdea2()
{
voidf(inti) { ... }
returnscopeLimitCalls(&f, 10);
}
Limit the number of times a function can be called.
Given a delegate dg and an integer maxCalls, this function returns a functor with the same parameter and return types as dg, and that forwards up to maxCalls calls to dg. On the maxCalls+1th call it throws an exception. This is useful for algorithms such as scid.nonlinear.findRoot() which may require an arbitrary number of function calls to complete.
In the example above, when we're taking the address of f, its context may be copied to the heap. This is a potentially expensive operation. To avoid it, use the unsafe function scopeLimitCalls() instead. This should only be used if you are absolutely sure that the returned delegate never escapes the current scope.