A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5
6namespace osu.Framework.Bindables
7{
8 /// <summary>
9 /// A bindable which holds a reference to a bound target, allowing switching between targets and handling unbind/rebind.
10 /// </summary>
11 /// <typeparam name="T">The type of our stored <see cref="Bindable{T}.Value"/>.</typeparam>
12 public class BindableWithCurrent<T> : Bindable<T>, IBindableWithCurrent<T>
13 {
14 private Bindable<T> currentBound;
15
16 public Bindable<T> Current
17 {
18 get => this;
19 set
20 {
21 if (value == null)
22 throw new ArgumentNullException(nameof(value));
23
24 if (currentBound != null) UnbindFrom(currentBound);
25 BindTo(currentBound = value);
26 }
27 }
28
29 public BindableWithCurrent(T defaultValue = default)
30 : base(defaultValue)
31 {
32 }
33
34 protected override Bindable<T> CreateInstance() => new BindableWithCurrent<T>();
35 }
36}