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 where T : unmanaged { private bool exists; private NB array; private uint index; public OptionalRef(NB 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._default; } public bool Exists => exists; public static implicit operator T(OptionalRef opt) => opt.Get(); public static implicit operator bool(OptionalRef opt) => opt.exists; /*public delegate ref TR Mapper(ref T component) where TR : unmanaged; public unsafe delegate TR* PMapper(T* component) where TR : unmanaged; public unsafe OptionalRef Map(PMapper mapper) where TR : unmanaged => exists ? new OptionalRef(ref *mapper(pointer)) : new OptionalRef();*/ /// /// Creates an instance of a struct T that can be referenced. /// /// The struct type to cache private struct CompRefCache where T : unmanaged { public static T _default; } } }