rkm / ScopeGuard

A simple C# implementation of a Scope Guard, with an "Armed" variant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Nuget GitHub pre-commit pre-commit.ci status

Scope Guard

A simple C# implementation of a Scope Guard, with an "Armed" variant.

Scope guards are useful in methods which have complex logic paths, where you need to ensure a block of code is executed on every return path.

These are implemented as IDisposables in C#, so the callback provided will be called as soon as the guard goes out of scope. They can therefore also be used in anonymous blocks.

Installation

> dotnet add package Rkm.ScopeGuard

Usage

The below examples use the C# 8.0 variant of the using statement, which doesn't require braces and uses the end of the method as the scope boundary.

ScopeGuard

ScopeGuard runs the provided callback on any return path.

public void Foo(MyObj guarded)
{
    using var sg = new ScopeGuard(() => { guarded.HasDescoped = true; });

    // ...

    return;
} // HasDeScoped is now true

ArmedScopeGuard

ArmedScopeGuard runs the provided callback on any return path, unlesss its Disarm method is called. This can be useful in cases where you have one or more "successful" return paths.

public void Foo(MyObj guarded)
{
    using var asg = new ArmedScopeGuard(() => { guarded.HasDescoped = true; });

    // Any return statements will callback

    // Success case - disarm and don't callback
    if(someCondition)
        asg.Disarm();
}

Example - Usage in a text parser

public void Parse(Parser parser)
{
    using var asg = new ArmedScopeGuard(() => { parser.State = ParserState.Invalid; });

    while (parser.ReadChar())
    {
        if (parser.CurrentChar == '{')
        {
            parser.State = ParserState.StartOfBlock;
            // ...
            continue;
        }

        // ...
    }

    if (parser.State == ParserState.Complete)
        asg.Disarm();
}

Kudos

This is a port of the AK/ScopeGuard.h implementation from the excellent SerenityOS project. The BSD 2-Clause License is therefore retained.

About

A simple C# implementation of a Scope Guard, with an "Armed" variant

License:BSD 2-Clause "Simplified" License


Languages

Language:C# 100.0%