Tanoda
IIndexableStruct.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Query;
10using System;
11
12namespace Leap.Unity {
13
29 public interface IIndexableStruct<T, ThisIndexableType>
30 where ThisIndexableType : struct,
31 IIndexableStruct<T, ThisIndexableType> {
32
33 T this[int idx] { get; }
34
35 int Count { get; }
36
37 }
38
47 public class BoxedIndexableStruct<Element, IndexableStruct>
48 : IIndexable<Element>,
50 where IndexableStruct : struct,
51 IIndexableStruct<Element, IndexableStruct> {
52
56 public IndexableStruct? maybeIndexableStruct = null;
57
58 public Element this[int idx] {
59 get {
60 if (!maybeIndexableStruct.HasValue) {
61 throw new NullReferenceException(
62 "PooledIndexableStructWrapper failed to index missing "
63 + typeof(IndexableStruct).Name
64 + "; did you assign its maybeIndexableStruct field?");
65 }
66 return maybeIndexableStruct.Value[idx];
67 }
68 }
69
70 public int Count {
71 get {
72 if (!maybeIndexableStruct.HasValue) { return 0; }
73 return maybeIndexableStruct.Value.Count;
74 }
75 }
76
77 public void OnSpawn() { }
78
79 public void OnRecycle() {
81 }
82 }
83
84 public static class BoxedIndexableStructExtensions {
85
99 public static void Recycle<Element,
100 IndexableStruct>(this BoxedIndexableStruct<Element,
101 IndexableStruct> pooledWrapper)
102 where IndexableStruct : struct,
104 Pool<BoxedIndexableStruct<Element, IndexableStruct>>.Recycle(pooledWrapper);
105 }
106
107 }
108
113 public struct IndexableStructEnumerator<Element, IndexableStruct>
114 where IndexableStruct : struct, IIndexableStruct<Element, IndexableStruct> {
115
116 IndexableStruct? maybeIndexable;
117 int index;
118
119 public IndexableStructEnumerator(IndexableStruct indexable) {
120 this.maybeIndexable = indexable;
121 index = -1;
122 }
123
125 return this;
126 }
127
128 public bool MoveNext() {
129 if (!maybeIndexable.HasValue) return false;
130 index++; return index < maybeIndexable.Value.Count;
131 }
132
133 public void Reset() {
134 index = -1;
135 }
136
137 public Element Current { get { return maybeIndexable.Value[index]; } }
138 }
139}
Explicit boxing class for IIndexableStructs that implements IIndexable.
IndexableStruct? maybeIndexableStruct
The wrapped indexable struct, or null.
This easy-to-implement interface represents the ability to index into a collection of elements of typ...
Definition: IIndexable.cs:21
This is a definition-friendly interface that new "indexable" struct definitions can implement to make...
Implement this interface to recieve a callback whenever your object is spawned from a pool.
Definition: Pool.cs:19
A two-generic-argument variant of an enumerator that allows an IIndexableStruct to quickly define an ...
IndexableStructEnumerator< Element, IndexableStruct > GetEnumerator()
IndexableStructEnumerator(IndexableStruct indexable)