Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / wvdotnet / wvmoniker.cs
blob851a29e59cc40946c1bbc9387d2fa609a864919d
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Collections.Generic;
8 using System.Reflection;
9 using Wv;
10 using Wv.Extensions;
12 namespace Wv
14 public class WvMonikerAttribute : Attribute
15 { }
17 public class WvMonikerBase
19 static object lockobj = new Object();
20 static bool registered = false;
22 protected static void register_all()
24 if (!registered)
26 // feebly attempt to be threadsafe: prevent registrations from
27 // running except in one thread.
28 lock(lockobj)
30 if (registered) return;
32 foreach (var t in
33 WvReflection.find_types(typeof(WvMonikerAttribute)))
35 t.InvokeMember("wvmoniker_register",
36 BindingFlags.Static
37 | BindingFlags.Public
38 | BindingFlags.InvokeMethod,
39 null, null, null);
41 registered = true;
47 public class WvMoniker<T>: WvMonikerBase
49 static List<WvMoniker<T>> registry = new List<WvMoniker<T>>();
50 string prefix;
51 Func<string,object,T> func;
53 public static WvMoniker<T>
54 register(string prefix, Func<string,object,T> func)
56 return new WvMoniker<T>(prefix, func);
59 public WvMoniker(string prefix, Func<string,object,T> func)
61 this.prefix = prefix;
62 this.func = func;
63 registry.Add(this);
66 // probably nobody will ever call this
67 public void unregister()
69 registry.Remove(this);
72 public static WvMoniker<T> find(string prefix)
74 foreach (WvMoniker<T> m in registry)
75 if (m.prefix == prefix)
76 return m;
77 return null;
80 public static T create(string moniker, object o)
82 register_all();
84 int pos = moniker.IndexOf(':');
85 string prefix, suffix;
86 if (pos >= 0)
88 prefix = moniker.Substring(0, pos);
89 suffix = moniker.Substring(pos+1);
91 else
93 prefix = moniker;
94 suffix = "";
97 WvMoniker<T> m = find(prefix);
98 if (m == null)
99 return default(T);
100 else
101 return m.func(suffix, o);
104 public static T create(string moniker)
106 return create(moniker, null);