|
@@ -0,0 +1,22 @@
|
|
|
+namespace Invercargill {
|
|
|
+
|
|
|
+ public delegate T CachedGet<T>();
|
|
|
+
|
|
|
+ private static Dictionary<int, NativeElement> cache;
|
|
|
+
|
|
|
+ public T once<T>(owned CachedGet<T> constructor) {
|
|
|
+ if(cache == null) {
|
|
|
+ cache = new Dictionary<int, NativeElement>();
|
|
|
+ }
|
|
|
+
|
|
|
+ int location = (int)constructor;
|
|
|
+ print(@"Location=$location\n");
|
|
|
+ NativeElement<T> element;
|
|
|
+ if(!cache.try_get(location, out element)) {
|
|
|
+ element = new NativeElement<T>(constructor());
|
|
|
+ cache.set(location, element);
|
|
|
+ }
|
|
|
+
|
|
|
+ return element.assert_as<T>();
|
|
|
+ }
|
|
|
+}
|