Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
NickStrupat / gist:1e101014f6218737279b
Created June 21, 2015 22:43
Instance Re-entrancy Guard
using System;
using System.Runtime.CompilerServices;
internal class InstanceReEntrancyGuard : IDisposable {
private class Ref<T> where T : struct { public T Value { get; set; } }
private static readonly ConditionalWeakTable<Object, Ref<Boolean>> reEntranceEphemerons = new ConditionalWeakTable<Object, Ref<Boolean>>();
private readonly Ref<Boolean> hasEnteredOnce;
public InstanceReEntrancyGuard(Object instance, String reEntrantMessage = null) {
hasEnteredOnce = reEntranceEphemerons.GetValue(instance, x => new Ref<Boolean>());
if (hasEnteredOnce.Value)
@NickStrupat
NickStrupat / Ref.cs
Last active December 15, 2015 02:00
[assembly:Ref.Ref.Ref, @ref.@ref.Ref]
namespace Ref {
[Ref, @ref.@ref.Ref] public struct Ref {
[Ref, @ref.@ref.Ref] public class RefAttribute : Attribute {}
[Ref, @ref.@ref.Ref] public Ref @ref([Ref, @ref.@ref.Ref] ref Ref @ref) { return @ref.@ref(ref @ref); }
}
}
namespace @ref {
[Ref, Ref.Ref.Ref] public class @ref {
@NickStrupat
NickStrupat / Out.cs
Last active December 15, 2015 15:58
Out
[assembly:Out.Out.Out, @out.@out.Out]
namespace Out {
[Out, @out.@out.Out] public struct Out {
[Out, @out.@out.Out] public class OutAttribute : Attribute {}
[Out, @out.@out.Out] public Out @out([Out, @out.@out.Out] out Out @out) { return @out.@out(out @out); }
}
}
namespace @out {
[Out, Out.Out.Out] public class @out {
@NickStrupat
NickStrupat / Struct.ReferenceEquals.cs
Last active January 11, 2016 03:42
StructReferenceEquals
using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
public static class Struct {
public static Boolean ReferenceEquals<T>(ref T a, ref T b) where T : struct => Cache<T>.Func(ref a, ref b);
private static class Cache<T> where T : struct {
public delegate Boolean ReferenceEqualsDelegate(ref T a, ref T b);
@NickStrupat
NickStrupat / TypeSwitch.cs
Created January 25, 2016 17:24
A class which allow switch...case-style code for matching type
public class TypeSwitch : ReadOnlyTypeSwitch {
public TypeSwitch Case<T>(Action<T> action) {
matches.Add(typeof(T), x => action((T) x));
return this;
}
public ReadOnlyTypeSwitch Default(Action @default) {
this.@default = @default ?? delegate { };
return this;
}
@NickStrupat
NickStrupat / LightMutex.cs
Last active April 17, 2016 03:13
A fast mutex struct
using System;
using System.Threading;
struct LightMutex {
private volatile Int32 inUse;
public void Lock() {
var spinWait = new SpinWait();
while (Interlocked.Exchange(ref inUse, 1) == 1)
spinWait.SpinOnce();
using System;
using System.Threading.Tasks;
using System.Windows.Input;
class AsyncCommand : ICommand {
public Boolean CanExecute(Object parameter = null) => !IsRunning;
public event EventHandler CanExecuteChanged = delegate { };
public async void Execute(Object parameter = null) {
@NickStrupat
NickStrupat / wat.cs
Created October 27, 2016 19:19
Soft-deletable entities
interface ISoftDeletable {
DateTime? Deleted { get; set; }
}
class MyEntity : ISoftDeletable {
public Int64 Id { get; set; }
public String Name { get; set; }
public DateTime? Deleted { get; set; }
}
@NickStrupat
NickStrupat / events.cs
Last active November 4, 2016 20:15
Event delegate contravariance
private ImmutableArray<THandler> handlers = ImmutableArray<THandler>.Empty;
internal static void Add<THandler>(ref ImmutableArray<Action<THandler>> handlers, Action<THandler> handler) {
if (handler == null)
return;
ImmutableArray<Action<THandler>> initial, computed;
do {
initial = InterlockedGet(ref handlers);
computed = initial.Add(handler);
public interface IInnumerable {
// *crickets*
}