Skip to content

Instantly share code, notes, and snippets.

@cjddmut
Last active January 28, 2024 21:39
Show Gist options
  • Save cjddmut/b4ece968404370bb6f01 to your computer and use it in GitHub Desktop.
Save cjddmut/b4ece968404370bb6f01 to your computer and use it in GitHub Desktop.
Wrapper struct for delegates. Helps avoid mistakes like '=' when '+=' was intended. Also checks for null automatically before invoke.
/*
* Created by C.J. Kimberlin (http://cjkimberlin.com)
*
* The MIT License (MIT)
*
* Copyright (c) 2014
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*
* ============= Description =============
*
* These are simple wrappers around delegates to help prevent simple mistakes like
* accidently overriding other registered delegates by doing = instead of +=. Also
* automatically will check if the callback is null before invoking.
*
* Notification onEvent1;
* Notification<int> onEvent2;
*
* onEvent1.Register(SomeFunction);
* onEvent2.Register(SomeFunctionWithIntArg);
*
* onEvent1.Invoke();
* onEvent2.Invoke(5);
* */
public struct Notification
{
public System.Action internalDelegate;
public void Register(System.Action func)
{
internalDelegate += func;
}
public void Unregister(System.Action func)
{
internalDelegate -= func;
}
public void Invoke()
{
if (internalDelegate != null)
{
internalDelegate();
}
}
public void UnregisterAll()
{
internalDelegate = null;
}
}
public struct Notification<T1>
{
public System.Action<T1> internalDelegate;
public void Register(System.Action<T1> func)
{
internalDelegate += func;
}
public void Unregister(System.Action<T1> func)
{
internalDelegate -= func;
}
public void Invoke(T1 t1)
{
if (internalDelegate != null)
{
internalDelegate(t1);
}
}
public void UnregisterAll()
{
internalDelegate = null;
}
}
public struct Notification<T1, T2>
{
public System.Action<T1, T2> internalDelegate;
public void Register(System.Action<T1, T2> func)
{
internalDelegate += func;
}
public void Unregister(System.Action<T1, T2> func)
{
internalDelegate -= func;
}
public void Invoke(T1 t1, T2 t2)
{
if (internalDelegate != null)
{
internalDelegate(t1, t2);
}
}
public void UnregisterAll()
{
internalDelegate = null;
}
}
public struct Notification<T1, T2, T3>
{
public System.Action<T1, T2, T3> internalDelegate;
public void Register(System.Action<T1, T2, T3> func)
{
internalDelegate += func;
}
public void Unregister(System.Action<T1, T2, T3> func)
{
internalDelegate -= func;
}
public void Invoke(T1 t1, T2 t2, T3 t3)
{
if (internalDelegate != null)
{
internalDelegate(t1, t2, t3);
}
}
public void UnregisterAll()
{
internalDelegate = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment