Cache.vala 560 B

123456789101112131415161718192021
  1. namespace Invercargill {
  2. public delegate T CachedGet<T>();
  3. private static Dictionary<int, NativeElement> cache;
  4. public T once<T>(owned CachedGet<T> constructor) {
  5. if(cache == null) {
  6. cache = new Dictionary<int, NativeElement>();
  7. }
  8. int location = (int)constructor;
  9. NativeElement<T> element;
  10. if(!cache.try_get(location, out element)) {
  11. element = new NativeElement<T>(constructor());
  12. cache.set(location, element);
  13. }
  14. return element.assert_as<T>();
  15. }
  16. }