TechbloxModdingAPI/GamecraftModdingAPI/Utility/OptionalRef.cs
NorbiPeti 6e03847ab0
FlyCam additions, improve struct
Added property to get the camera from the player
Removed pointer magic
2021-04-27 01:52:54 +02:00

60 lines
No EOL
1.7 KiB
C#

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;
}
}
}