|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using TechbloxModdingAPI.Blocks;
- using Svelto.DataStructures;
- using Svelto.ECS;
-
- namespace TechbloxModdingAPI
- {
- public ref struct OptionalRef<T> where T : unmanaged, IEntityComponent
- {
- private bool exists;
- private NB<T> array;
- private uint index;
- private EntityInitializer initializer;
-
- public OptionalRef(NB<T> array, uint index)
- {
- exists = true;
- this.array = array;
- this.index = index;
- initializer = default;
- }
-
- /// <summary>
- /// Wraps the initializer data, if present.
- /// </summary>
- /// <param name="obj">The object with the initializer</param>
- public OptionalRef(EcsObjectBase obj)
- {
- if (obj.InitData.Valid)
- {
- initializer = obj.InitData.Initializer(obj.Id);
- exists = true;
- }
- else
- {
- initializer = default;
- exists = false;
- }
- array = default;
- index = default;
- }
-
- /// <summary>
- /// Returns the value or a default value if empty. Supports objects that are being initialized.
- /// </summary>
- /// <returns>The value or the default value</returns>
- public ref T Get()
- {
- if (!exists) return ref CompRefCache<T>.Default;
- if (initializer.EGID == EGID.Empty)
- return ref array[index];
- return ref initializer.GetOrCreate<T>();
- }
-
- 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="TR">The struct type to cache</typeparam>
- private struct CompRefCache<TR> where TR : unmanaged
- {
- public static TR Default;
- }
- }
- }
|