2021-09-30 10:33:12 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2022-07-28 06:03:08 +00:00
|
|
|
using JetBrains.Annotations;
|
2021-09-30 10:33:12 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A wrapper to provide access to database backed classes in a thread-safe manner.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The databased type.</typeparam>
|
2022-01-26 04:37:33 +00:00
|
|
|
public abstract class Live<T> : IEquatable<Live<T>>
|
|
|
|
where T : class, IHasGuidPrimaryKey
|
2021-09-30 10:33:12 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
public Guid ID { get; }
|
2021-09-30 10:33:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-07-28 06:03:08 +00:00
|
|
|
public abstract void PerformRead([InstantHandle] Action<T> perform);
|
2021-09-30 10:33:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-07-28 06:03:08 +00:00
|
|
|
public abstract TReturn PerformRead<TReturn>([InstantHandle] Func<T, TReturn> perform);
|
2021-09-30 10:33:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a write operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-07-28 06:03:08 +00:00
|
|
|
public abstract void PerformWrite([InstantHandle] Action<T> perform);
|
2021-09-30 10:33:12 +00:00
|
|
|
|
2021-11-26 05:39:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether this instance is tracking data which is managed by the database backing.
|
|
|
|
/// </summary>
|
2022-01-26 04:37:33 +00:00
|
|
|
public abstract bool IsManaged { get; }
|
2021-11-26 05:39:35 +00:00
|
|
|
|
2021-09-30 10:33:12 +00:00
|
|
|
/// <summary>
|
2021-11-30 02:56:53 +00:00
|
|
|
/// Resolve the value of this instance on the update thread.
|
2021-09-30 10:33:12 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2021-11-30 02:56:53 +00:00
|
|
|
/// After resolving, the data should not be passed between threads.
|
2021-09-30 10:33:12 +00:00
|
|
|
/// </remarks>
|
2022-01-26 04:37:33 +00:00
|
|
|
public abstract T Value { get; }
|
|
|
|
|
|
|
|
protected Live(Guid id)
|
|
|
|
{
|
|
|
|
ID = id;
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:16:25 +00:00
|
|
|
public bool Equals(Live<T>? other)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(this, other)) return true;
|
|
|
|
if (other == null) return false;
|
|
|
|
|
|
|
|
return ID == other.ID;
|
|
|
|
}
|
2022-01-26 04:37:33 +00:00
|
|
|
|
2022-09-13 05:01:40 +00:00
|
|
|
public override int GetHashCode() => HashCode.Combine(ID);
|
|
|
|
|
2022-12-16 09:16:26 +00:00
|
|
|
public override string? ToString() => PerformRead(i => i.ToString());
|
2021-09-30 10:33:12 +00:00
|
|
|
}
|
|
|
|
}
|