浏览代码

Add build_for func

Billy Barrow 10 月之前
父节点
当前提交
49debb492d
共有 2 个文件被更改,包括 14 次插入1 次删除
  1. 5 1
      src/lib/Cache.vala
  2. 9 0
      src/lib/PropertyMapper.vala

+ 5 - 1
src/lib/Cache.vala

@@ -5,11 +5,15 @@ namespace Invercargill {
     private static Dictionary<int, NativeElement> cache;
 
     public T once<T>(owned CachedGet<T> constructor) {
+        int location = (int)constructor;
+        return keyed_once<T>(location, (owned)constructor);
+    }
+
+    public T keyed_once<T>(int location, owned CachedGet<T> constructor) {
         if(cache == null) {
             cache = new Dictionary<int, NativeElement>();
         }
 
-        int location = (int)constructor;
         NativeElement<T> element;
         if(!cache.try_get(location, out element)) {
             element = new NativeElement<T>(constructor());

+ 9 - 0
src/lib/PropertyMapper.vala

@@ -10,6 +10,15 @@ namespace Invercargill {
         private Vector<PropertyMapping<T>> mappings;
         private ObjectConstructor<T> constructor;
 
+        public static PropertyMapper<T> build_for<T>(Func<PropertyMapperBuilder<T>> func) {
+            int location = (int)func;
+            return keyed_once<PropertyMapper<T>>(location, () => {
+                var builder = new PropertyMapperBuilder<T>();
+                func(builder);
+                return builder.build();
+            });
+        }
+
         internal PropertyMapper(Vector<PropertyMapping<T>> mappings, ObjectConstructor<T> constructor) {
             this.mappings = mappings;
             this.constructor = constructor;