TechbloxModdingAPI/GamecraftModdingAPI/Utility/OptionalRef.cs

60 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using GamecraftModdingAPI.Blocks;
using Svelto.DataStructures;
using Svelto.ECS;
namespace GamecraftModdingAPI
{
public struct OptionalRef<T> where T : unmanaged
{
private bool exists;
private NB<T> array;
private uint index;
public OptionalRef(NB<T> array, uint index)
{
exists = true;
this.array = array;
this.index = index;
}
public OptionalRef(ref T value)
{
exists = true;
array = default;
index = default;
}
public ref T Get(T def = default)
{
if (exists)
return ref array[index];
return ref CompRefCache<T>._default;
}
public bool Exists => exists;
public static implicit operator T(OptionalRef<T> opt) => opt.Get();
public static implicit operator bool(OptionalRef<T> opt) => opt.exists;
/*public delegate ref TR Mapper<TR>(ref T component) where TR : unmanaged;
public unsafe delegate TR* PMapper<TR>(T* component) where TR : unmanaged;
public unsafe OptionalRef<TR> Map<TR>(PMapper<TR> mapper) where TR : unmanaged =>
exists ? new OptionalRef<TR>(ref *mapper(pointer)) : new OptionalRef<TR>();*/
/// <summary>
/// Creates an instance of a struct T that can be referenced.
/// </summary>
/// <typeparam name="T">The struct type to cache</typeparam>
private struct CompRefCache<T> where T : unmanaged
{
public static T _default;
}
}
}