Table of Contents

Class Safe

Namespace
Tlabs.Misc
Assembly
Tlabs.Core.dll

Miscelaneous helpers to enhance reliability of resource allocation, error handling, etc..

public static class Safe
Inheritance
Safe
Inherited Members

Fields

DisastrousExceptions

List of exceptions that are considered disastrous.

public static readonly IList<Type> DisastrousExceptions

Field Value

IList<Type>

Methods

Allocated<T, D>(Func<D, T>)

Safely returns a newly allocated object that depends on a IDisposable object.

public static T? Allocated<T, D>(Func<D, T> allocT) where D : class, IDisposable, new()

Parameters

allocT Func<D, T>

A delegate that creates an instance of type T.

Returns

T

instance of T

Type Parameters

T

The type returned.

D

A a reference type that implements IDisposable and has a default ctor.

Remarks

When the allocation succeeds, the IDisposable object of type D is assumed to be owned by the returned object of type T. (i.e. the returned object is responsible for disposing)

Allocated<T, D>(Func<D>, Func<D, T>)

Safely returns a newly allocated object that depends on a IDisposable object.

public static T? Allocated<T, D>(Func<D> allocD, Func<D, T> allocT) where D : class, IDisposable

Parameters

allocD Func<D>

A delegate that creates an instance of disposable type D

allocT Func<D, T>

A delegate that creates an instance of type T.

Returns

T

instance of T

Type Parameters

T

The type returned.

D

A a reference type that implements IDisposable.

Remarks

When the allocation succeeds, the IDisposable object of type D is assumed to be owned by the returned object of type T. (i.e. the returned object is responsible for disposing)

Allocated<T, D>(Func<D>, Func<D, T>, Action<D?, T?>)

Safely returns a newly allocated IDisposable object that depends on another IDisposable object.

public static T? Allocated<T, D>(Func<D> allocD, Func<D, T> allocT, Action<D?, T?> setupT) where T : class, IDisposable where D : class, IDisposable

Parameters

allocD Func<D>

A delegate that creates an instance of disposable type D

allocT Func<D, T>

A delegate that creates an instance of type T.

setupT Action<D, T>

A delegate to further setup an instance of type T.

Returns

T

instance of T

Type Parameters

T

The type returned.

D

A a reference type that implements IDisposable and has a default ctor.

Remarks

The object is returned after all these operations succeed:

  • creation of the other object
  • creation of the initial object to be returned
  • the further setup of the object to be returned
    If any of these operations fails (by throwing an exception), it is guaranteed that all created objects get disposed properly.

CompareExchange<T>(ref T, T, Func<T>)

Compares location with comparand (of reference type T for reference equality and, if they are equal, replaces location with the value returned from valueCreator.

public static T CompareExchange<T>(ref T location, T comparand, Func<T> valueCreator) where T : class?

Parameters

location T
comparand T
valueCreator Func<T>

Returns

T

The original value in location

Type Parameters

T

HandleDisastrousCondition(Exception)

Consider whether the e is critical and thus must terminate the process fast leaving the exception unhandled.

public static void HandleDisastrousCondition(Exception e)

Parameters

e Exception

Remarks

This check should only be used in top level catch clauses.

LoadType(string, string)

Loads a type specified as qualifiedTypeName.

public static Type LoadType(string qualifiedTypeName, string typeDesc)

Parameters

qualifiedTypeName string
typeDesc string

Returns

Type

Exceptions

AppConfigException

When type loading failed. Using typeDesc in error...

NoDisastrousCondition(Exception)

Check if an exception type is critical and thus should terminate the process leaving the exception unhandled.

public static bool NoDisastrousCondition(Exception e)

Parameters

e Exception

Returns

bool

Remarks

This check should only be used in top level catch clauses.

Undisposable<T>(ref T?)

Returns the passed obj and makes the the variable holding this reference undisposable by setting it to null for the caller.

public static T? Undisposable<T>(ref T? obj) where T : class, IDisposable

Parameters

obj T

IDisposable

Returns

T

obj

Type Parameters

T

type of obj