diff --git a/generator/ClassBase.cs b/generator/ClassBase.cs index 8c77764c6..57e277044 100644 --- a/generator/ClassBase.cs +++ b/generator/ClassBase.cs @@ -34,6 +34,7 @@ namespace GtkSharp.Generation { private IDictionary props = new Dictionary (); private IDictionary fields = new Dictionary (); private IDictionary methods = new Dictionary (); + private IDictionary constants = new Dictionary(); protected IList interfaces = new List(); protected IList managed_interfaces = new List(); protected IList ctors = new List(); @@ -108,6 +109,11 @@ namespace GtkSharp.Generation { ctors.Add (new Ctor (member, this)); break; + case "constant": + name = member.GetAttribute ("name"); + constants.Add (name, new Constant (member)); + break; + default: break; } @@ -156,6 +162,14 @@ namespace GtkSharp.Generation { methods.Remove (method.Name); invalids.Clear (); + foreach (Constant con in constants.Values) { + if (!con.Validate (log)) + invalids.Add (con); + } + foreach (Constant con in invalids) + constants.Remove (con.Name); + invalids.Clear (); + foreach (Ctor ctor in ctors) { if (!ctor.Validate (log)) invalids.Add (ctor); @@ -199,6 +213,7 @@ namespace GtkSharp.Generation { case "implements": case "constructor": case "disabledefaultconstructor": + case "constant": return true; default: @@ -221,6 +236,12 @@ namespace GtkSharp.Generation { field.Generate (gen_info, "\t\t"); } + protected void GenConstants (GenerationInfo gen_info) + { + foreach (Constant con in constants.Values) + con.Generate (gen_info, "\t\t"); + } + private void ParseImplements (XmlElement member) { foreach (XmlNode node in member.ChildNodes) { diff --git a/generator/ClassGen.cs b/generator/ClassGen.cs index f0cf6a33f..f6ef5b958 100644 --- a/generator/ClassGen.cs +++ b/generator/ClassGen.cs @@ -77,9 +77,10 @@ namespace GtkSharp.Generation { sw.WriteLine (" {"); sw.WriteLine (); + GenConstants (gen_info); GenProperties (gen_info, null); GenMethods (gen_info, null, null); - + sw.WriteLine ("#endregion"); sw.WriteLine ("\t}"); diff --git a/generator/Constant.cs b/generator/Constant.cs new file mode 100644 index 000000000..c8a06ca14 --- /dev/null +++ b/generator/Constant.cs @@ -0,0 +1,64 @@ +using System; +using System.Xml; +using System.IO; + +namespace GtkSharp.Generation +{ + public class Constant + { + private readonly XmlElement elem; + private readonly string name; + private readonly string value; + private readonly string ctype; + + public Constant (XmlElement elem) + { + this.elem = elem; + this.name = elem.GetAttribute ("name"); + this.value = elem.GetAttribute ("value"); + this.ctype = elem.GetAttribute ("ctype"); + } + + public string Name { + get { + return this.name; + } + } + public string ConstType { + get { + if (IsString) + return "string"; + // gir registers all integer values as gint even for numbers which do not fit into a gint + // if the number is too big for an int, try to fit it into a long + if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length < 20 && long.Parse (value) > Int32.MaxValue) + return "long"; + return SymbolTable.Table.GetMarshalType (ctype); + } + } + + public bool IsString { + get { + return (SymbolTable.Table.GetCSType (ctype) == "string"); + } + } + + public virtual bool Validate (LogWriter log) + { + if (ConstType == String.Empty) { + log.Warn ("{0} type is missing or wrong", Name); + return false; + } + if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length >= 20) { + return false; + } + return true; + } + + public virtual void Generate (GenerationInfo gen_info, string indent) + { + StreamWriter sw = gen_info.Writer; + + sw.WriteLine ("{0}public const {1} {2} = {3}{4}{3};", indent, ConstType, Name, IsString ? "\"": String.Empty, value); + } + } +} diff --git a/generator/Makefile.am b/generator/Makefile.am index dfd24c401..2fc3aabe7 100644 --- a/generator/Makefile.am +++ b/generator/Makefile.am @@ -19,6 +19,7 @@ sources = \ CodeGenerator.cs \ ConstFilenameGen.cs \ ConstStringGen.cs \ + Constant.cs \ Ctor.cs \ DefaultSignalHandler.cs \ EnumGen.cs \ diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index 774a797da..05ea12ca1 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -195,9 +195,10 @@ namespace GtkSharp.Generation { GenSignals (gen_info, null); } + GenConstants (gen_info); GenClassMembers (gen_info, cs_parent); GenMethods (gen_info, null, null); - + if (interfaces.Count != 0) { var all_methods = new Dictionary (); foreach (Method m in Methods.Values) { diff --git a/generator/OpaqueGen.cs b/generator/OpaqueGen.cs index 5b71ea742..7cef60587 100644 --- a/generator/OpaqueGen.cs +++ b/generator/OpaqueGen.cs @@ -79,7 +79,8 @@ namespace GtkSharp.Generation { sw.WriteLine (" {"); sw.WriteLine (); - + + GenConstants (gen_info); GenFields (gen_info); GenMethods (gen_info, null, null); GenCtors (gen_info); diff --git a/generator/generator.csproj b/generator/generator.csproj index 415779754..41ccce2e6 100644 --- a/generator/generator.csproj +++ b/generator/generator.csproj @@ -91,6 +91,7 @@ +