AccessUtility.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Diagnostics;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace InitialPrefabs.NimGui.Collections {
  6. internal static class AccessUtility {
  7. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  8. internal static void CheckIndexOutOfRange(int index, int capacity) {
  9. if (index < 0) {
  10. throw new ArgumentOutOfRangeException($"Index {index} must be a value between [0..{capacity - 1}]");
  11. }
  12. if (index >= capacity) {
  13. throw new ArgumentOutOfRangeException($"Index {index} must not exceed the Capacity {capacity}");
  14. }
  15. }
  16. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  17. internal static void CheckAvailableSize(int requestedSize, int capacity) {
  18. if (requestedSize > capacity) {
  19. throw new InvalidOperationException($"Requested size exceeds capacity. " +
  20. $"Requesting: {requestedSize}, but only has {capacity} available.");
  21. }
  22. }
  23. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  24. internal static void CheckExists<T>(int id, UnsafeParallelHashMap<int, T> container) where T : struct {
  25. if (!container.ContainsKey(id)) {
  26. throw new InvalidOperationException($"ID {id} does not exist.");
  27. }
  28. }
  29. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  30. internal static void CheckExists<T>(int id, NativeParallelHashMap<int, T> container) where T : struct {
  31. if (!container.ContainsKey(id)) {
  32. throw new InvalidOperationException($"ID {id} does not exist.");
  33. }
  34. }
  35. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  36. internal static void IsNotNull<T>(T value) {
  37. if (value == null) {
  38. throw new InvalidOperationException($"Value for type: {typeof(T)} cannot be null!");
  39. }
  40. }
  41. }
  42. }