2021-12-16 06:11:48 +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;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides a method of working with unmanaged realm objects.
|
|
|
|
/// Usually used for testing purposes where the instance is never required to be managed.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The underlying object type.</typeparam>
|
2022-01-26 04:37:33 +00:00
|
|
|
public class RealmLiveUnmanaged<T> : Live<T> where T : RealmObjectBase, IHasGuidPrimaryKey
|
2021-12-16 06:11:48 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The original live data used to create this instance.
|
|
|
|
/// </summary>
|
|
|
|
public override T Value { get; }
|
|
|
|
|
2021-12-16 06:11:48 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new instance of live realm data.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">The realm data.</param>
|
|
|
|
public RealmLiveUnmanaged(T data)
|
2022-01-26 04:37:33 +00:00
|
|
|
: base(data.ID)
|
2021-12-16 06:11:48 +00:00
|
|
|
{
|
2022-01-17 08:28:01 +00:00
|
|
|
if (data.IsManaged)
|
|
|
|
throw new InvalidOperationException($"Cannot use {nameof(RealmLiveUnmanaged<T>)} with managed instances");
|
|
|
|
|
2021-12-16 06:11:48 +00:00
|
|
|
Value = data;
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override void PerformRead(Action<T> perform) => perform(Value);
|
2021-12-16 06:11:48 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override TReturn PerformRead<TReturn>(Func<T, TReturn> perform) => perform(Value);
|
2021-12-16 06:11:48 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override void PerformWrite(Action<T> perform) => throw new InvalidOperationException(@"Can't perform writes on a non-managed underlying value");
|
2021-12-16 06:11:48 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override bool IsManaged => false;
|
2021-12-16 06:11:48 +00:00
|
|
|
}
|
|
|
|
}
|