[gxml] Reducing memory for TwNode and TwDocument. Build Fixes.



commit 5bade7e70dde8c84bdd73ff6b19901028480778f
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Feb 11 22:38:39 2016 -0600

    Reducing memory for TwNode and TwDocument. Build Fixes.
    
    * Build fixes
    * Now some properties and child nodes are available only when is
      requested as fake collections when they don't have sense, reducing
      some memory
    * SerializableGeeDualKeyMap implements Gee.Traversable

 gxml/SerializableGeeDualKeyMap.vala |    6 +++++-
 gxml/TwDocument.vala                |    8 ++++++++
 gxml/TwElement.vala                 |   21 +++++++++++++++++++++
 gxml/TwNode.vala                    |    9 +++------
 4 files changed, 37 insertions(+), 7 deletions(-)
---
diff --git a/gxml/SerializableGeeDualKeyMap.vala b/gxml/SerializableGeeDualKeyMap.vala
index c6232ac..fd7d387 100644
--- a/gxml/SerializableGeeDualKeyMap.vala
+++ b/gxml/SerializableGeeDualKeyMap.vala
@@ -28,7 +28,7 @@ using Gee;
  * It implements { link Serializable} and { link SerializableCollection} interfaces, it is iterable as
  * other Gee collections.
  */
-public class GXml.SerializableDualKeyMap<P,S,V> : Object, Serializable, SerializableCollection
+public class GXml.SerializableDualKeyMap<P,S,V> : Object, Gee.Traversable <V>, Serializable, 
SerializableCollection
 {
   protected Gee.HashMultiMap<P,HashMap<S,V>> storage;
 
@@ -229,5 +229,9 @@ public class GXml.SerializableDualKeyMap<P,S,V> : Object, Serializable, Serializ
   {
     return true;
   }
+  // Traversable
+  public new bool @foreach (Gee.ForallFunc<V> f) {
+    return values ()  foreach (f);
+  }
 }
 
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 2cd50cd..353fbbe 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -30,6 +30,7 @@ using Xml;
  */
 public class GXml.TwDocument : GXml.TwNode, GXml.Document
 {
+  protected Gee.ArrayList<GXml.Node> _namespaces;
   GXml.Element _root = null;
   construct {
     _name = "#document";
@@ -41,6 +42,12 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     this.file = f;
   }
   // GXml.Node
+  public override Gee.List<GXml.Namespace> namespaces {
+    owned get {
+      if (_namespaces == null) _namespaces = new Gee.ArrayList<GXml.Node> ();
+      return _namespaces.ref () as Gee.List<GXml.Namespace>;
+    }
+  }
   /**
    * { inheritDoc}
    *
@@ -57,6 +64,7 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
    */
   public override bool set_namespace (string uri, string? prefix)
   {
+    if (_namespaces == null) _namespaces = new Gee.ArrayList<GXml.Node> ();
     _namespaces.add (new TwNamespace (this, uri, prefix));
     return true;
   }
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
index de1ab5d..c88b596 100644
--- a/gxml/TwElement.vala
+++ b/gxml/TwElement.vala
@@ -26,6 +26,9 @@ using Gee;
  */
 public class GXml.TwElement : GXml.TwNode, GXml.Element
 {
+  protected Gee.HashMap<string,GXml.Node> _attrs;
+  protected Gee.ArrayList<GXml.Node> _children;
+  protected Gee.ArrayList<GXml.Node> _namespaces;
   private string _content = null;
   public TwElement (GXml.Document d, string name)
     requires (d is TwDocument)
@@ -42,6 +45,24 @@ public class GXml.TwElement : GXml.TwNode, GXml.Element
     }
     set { update_content (value); }
   }
+  public override Gee.Map<string,GXml.Node> attrs {
+    owned get {
+      if (_attrs == null) _attrs = new Gee.HashMap<string,GXml.Node> ();
+      return _attrs.ref () as Gee.Map<string,GXml.Node>;
+    }
+  }
+  public override Gee.BidirList<GXml.Node> children {
+    owned get {
+      if (_children == null) _children  = new Gee.ArrayList<GXml.Node> ();
+      return _children.ref () as Gee.BidirList<GXml.Node>;
+    }
+  }
+  public override Gee.List<GXml.Namespace> namespaces {
+    owned get {
+      if (_namespaces == null) _namespaces = new Gee.ArrayList<GXml.Node> ();
+      return _namespaces.ref () as Gee.List<GXml.Namespace>;
+    }
+  }
   // GXml.Element
   public void set_attr (string name, string value)
   {
diff --git a/gxml/TwNode.vala b/gxml/TwNode.vala
index 95f2762..dd5c17c 100644
--- a/gxml/TwNode.vala
+++ b/gxml/TwNode.vala
@@ -26,9 +26,6 @@ using Gee;
  */
 public abstract class GXml.TwNode : Object, GXml.Node
 {
-  protected Gee.HashMap<string,GXml.Node> _attrs = new Gee.HashMap<string,GXml.Node> ();
-  protected Gee.ArrayList<GXml.Node> _children = new Gee.ArrayList<GXml.Node> ();
-  protected Gee.ArrayList<GXml.Node> _namespaces = new Gee.ArrayList<GXml.Node> ();
   protected string _name = null;
   protected string _value = null;
   protected GXml.Document _doc;
@@ -54,11 +51,11 @@ public abstract class GXml.TwNode : Object, GXml.Node
     return true;
   }
   public virtual string to_string () { return get_type ().name (); }
-  public virtual Gee.Map<string,GXml.Node> attrs { owned get { return (Gee.Map<string,GXml.Node>) _attrs.ref 
(); } }
-  public virtual Gee.BidirList<GXml.Node> children { owned get { return (Gee.BidirList<GXml.Node>) 
_children.ref (); } }
+  public virtual Gee.Map<string,GXml.Node> attrs { owned get { return new Gee.HashMap<string,GXml.Node> (); 
} }
+  public virtual Gee.BidirList<GXml.Node> children { owned get { return new Gee.ArrayList<GXml.Node> (); } }
   public virtual GXml.Document document { get { return _doc; } }
   public virtual string name { owned get { return _name.dup (); } }
-  public virtual Gee.List<GXml.Namespace> namespaces { owned get { return (Gee.List<GXml.Namespace>) 
_namespaces.ref (); } }
+  public virtual Gee.List<GXml.Namespace> namespaces { owned get { return new Gee.ArrayList<GXml.Node> (); } 
}
   public virtual GXml.NodeType type_node { get { return GXml.NodeType.DOCUMENT; } }
   public virtual string value { owned get { return _value.dup (); } set  { _value = value; } }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]