// Copyright (c) 2008 Maciej Kacper Jagiello // // The MIT License // // 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. using System; namespace nirami { /// /// Useful to manage state objects like textures, matrices, shaders... /// implementing objects can be "used" with the c#'s use keyword (it's a /// hack I know, but pretty convenient) or with delegates and lambda /// expressions(slow, causes heap allocations and stuff). /// For better performance you can use the IBindableExplicitUse interface. /// With structs use IBindableExplicitUse interface instead to avoid /// boxing (a big hit on performance). /// public interface IBindable { void Bind(); void Unbind(); } /// /// used internally /// public static class IBindableImplicitUseExtensions { public static Binder Use(this IBindable bindable) { return new Binder(bindable); } // can be used with delegates and labdas too, but it's much slower public delegate void SimpleDelegate(); public static void DoWith(this IBindable bindable, SimpleDelegate toDo) { bindable.Bind(); toDo(); bindable.Unbind(); } } /// /// used internally /// public struct Binder : IDisposable where T : IBindable { private T bindable; public Binder(T bindable) { this.bindable = bindable; bindable.Bind(); } public void Dispose() { bindable.Unbind(); } } /// /// used internally /// public struct Binder : IDisposable { private IBindable bindable; public Binder(IBindable bindable) { this.bindable = bindable; bindable.Bind(); } public void Dispose() { bindable.Unbind(); } } /// /// override the "Use" method and just return new Binder(this); /// public interface IBindableExplicitUse : IBindable { Binder Use(); } /// /// put the struct's type itself as type parameter /// override the "Use" method and just return new Binder(this); /// public interface IBindableExplicitUse : IBindable where T : IBindable { Binder Use(); } }