diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2016-08-09 03:37:43 +0200 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2016-08-09 03:39:47 +0200 |
| commit | 04c3deb931dfe039bf0a8f8665f6538f09f5508a (patch) | |
| tree | cf5fd1392b1c36996a46e2da82fc1c2d22688ef9 /src/Interface.cs | |
| parent | 4d4a9116ea5c68b27f0b7c43db9682ab57738ae7 (diff) | |
| parent | 91a40c76fc886501c644a702832a9b257bc154f9 (diff) | |
instanciator
Diffstat (limited to 'src/Interface.cs')
| -rw-r--r-- | src/Interface.cs | 83 |
1 files changed, 28 insertions, 55 deletions
diff --git a/src/Interface.cs b/src/Interface.cs index 7b324de7..57291370 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -107,14 +107,16 @@ namespace Crow g.IsQueueForRedraw = true; } } + //fast compiled IML instantiators + public static Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>(); #region Default values and Style loading /// Default values of properties from GraphicObjects are retrieve from XML Attributes. /// The reflexion process used to retrieve those values being very slow, it is compiled in MSIL /// and injected as a dynamic method referenced in the DefaultValuesLoader Dictionnary. /// The compilation is done on the first object instancing, and is also done for custom widgets - public delegate void loadDefaultInvoker(object instance); - public static Dictionary<String, loadDefaultInvoker> DefaultValuesLoader = new Dictionary<string, loadDefaultInvoker>(); + public delegate void LoaderInvoker(object instance); + public static Dictionary<String, LoaderInvoker> DefaultValuesLoader = new Dictionary<string, LoaderInvoker>(); public static Dictionary<string, Dictionary<string, object>> Styling; /// <summary> parse all styling data's and build global Styling Dictionary </summary> static void LoadStyling() { @@ -176,36 +178,6 @@ namespace Crow return stream; } - /// <summary> - /// Pre-read first node to set GraphicObject class for loading - /// and reset stream position to 0 - /// </summary> - public static Type GetTopContainerOfXMLStream (Stream stream) - { - string root = "Object"; - stream.Seek (0, SeekOrigin.Begin); - using (XmlReader reader = XmlReader.Create (stream)) { - while (reader.Read ()) { - // first element is the root element - if (reader.NodeType == XmlNodeType.Element) { - root = reader.Name; - break; - } - } - } - - Type t = Type.GetType ("Crow." + root); - if (t == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == root) - t = expT; - } - } - - stream.Seek (0, SeekOrigin.Begin); - return t; - } public static void Save<T> (string file, T graphicObject) { @@ -225,9 +197,10 @@ namespace Crow GraphicObject tmp = null; try { - using (Stream stream = GetStreamFromPath (path)) { - tmp = Load(stream, GetTopContainerOfXMLStream(stream)); - } + if (!Instantiators.ContainsKey(path)) + BuildInstaciator(path); + tmp = Instantiators [path].CreateInstance (); + } catch (Exception ex) { throw new Exception ("Error loading <" + path + ">:", ex); } @@ -236,37 +209,37 @@ namespace Crow return tmp; } - internal static GraphicObject Load (Stream stream, Type type) + public static void BuildInstaciator (string path) { + System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; + #if DEBUG_LOAD Stopwatch loadingTime = new Stopwatch (); loadingTime.Start (); #endif - GraphicObject result; - - CurrentInterface.XmlLoading = true; - - XmlSerializerNamespaces xn = new XmlSerializerNamespaces (); - xn.Add ("", ""); - - XmlSerializer xs = new XmlSerializer (type); - - result = (GraphicObject)xs.Deserialize (stream); - CurrentInterface.XmlLoading = false; + try { + using (Stream stream = GetStreamFromPath (path)) { + using (IMLInstantiatorBuilder itr = new IMLInstantiatorBuilder (stream)){ + itr.ReadRootType(); + itr.InitEmitter(); + CompilerServices.BuildInstanciator(itr, itr.RootType); + itr.Read();//close tag + Instantiators[path] = itr.GetInstanciator(); + } + } + } catch (Exception ex) { + throw new Exception ("Error loading <" + path + ">:", ex); + } #if DEBUG_LOAD - FileStream fs = stream as FileStream; - if (fs!=null) - CurrentGOMLPath = fs.Name; loadingTime.Stop (); - Debug.WriteLine ("GOML Loading ({2}->{3}): {0} ticks, {1} ms", - loadingTime.ElapsedTicks, - loadingTime.ElapsedMilliseconds, - CurrentGOMLPath, result.ToString()); + Debug.WriteLine ("IML Loading '{2}' : {0} ticks, {1} ms", + loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path); #endif - return result; + Thread.CurrentThread.CurrentCulture = savedCulture; } public GraphicObject LoadInterface (string path) |