generator: added handling of optional parameters

This commit is contained in:
Stephan Sundermann 2013-10-09 15:31:10 +02:00 committed by Andrés G. Aragoneses
parent 33fd293b84
commit 388a2fe659
4 changed files with 63 additions and 0 deletions

View file

@ -207,6 +207,14 @@ namespace GtkSharp.Generation {
}
}
public void GenerateOverloads (StreamWriter sw)
{
sw.WriteLine ();
sw.WriteLine ("\t\t" + retval.CSType + " " + Name + "(" + (Signature != null ? Signature.WithoutOptional () : "") + ") {");
sw.WriteLine ("\t\t\t{0}{1} ({2});", !retval.IsVoid ? "return " : String.Empty, Name, Signature.CallWithoutOptionals ());
sw.WriteLine ("\t\t}");
}
public void Generate (GenerationInfo gen_info, ClassBase implementor)
{
Method comp = null;
@ -266,6 +274,9 @@ namespace GtkSharp.Generation {
}
else
gen_info.Writer.WriteLine();
if (Parameters.HasOptional && !(is_get || is_set))
GenerateOverloads (gen_info.Writer);
gen_info.Writer.WriteLine();

View file

@ -90,6 +90,12 @@ namespace GtkSharp.Generation {
}
}
internal bool IsOptional {
get {
return elem.GetAttributeAsBoolean ("allow-none");
}
}
bool is_count;
bool is_count_set;
public bool IsCount {

View file

@ -142,6 +142,11 @@ namespace GtkSharp.Generation {
set { is_static = value; }
}
bool has_optional;
internal bool HasOptional {
get { return has_optional;}
}
public Parameter GetCountParameter (string param_name)
{
foreach (Parameter p in this)
@ -198,6 +203,9 @@ namespace GtkSharp.Generation {
return false;
}
if (p.IsOptional && p.PassAs == String.Empty)
has_optional = true;
IGeneratable gen = p.Generatable;
if (p.IsArray) {

View file

@ -118,6 +118,44 @@ namespace GtkSharp.Generation {
return String.Join (", ", result);
}
}
public string WithoutOptional ()
{
if (parms.Count == 0)
return String.Empty;
var result = new string [parms.Count];
int i = 0;
foreach (Parameter p in parms) {
if (p.IsOptional && p.PassAs == String.Empty)
continue;
result [i] = p.PassAs != String.Empty ? p.PassAs + " " : String.Empty;
result [i++] += p.CSType + " " + p.Name;
}
return String.Join (", ", result, 0, i);
}
public string CallWithoutOptionals ()
{
if (parms.Count == 0)
return String.Empty;
var result = new string [parms.Count];
int i = 0;
foreach (Parameter p in parms) {
result [i] = p.PassAs != "" ? p.PassAs + " " : "";
if (p.IsOptional && p.PassAs == String.Empty)
result [i++] += (p.Generatable is StructGen || p.Generatable is BoxedGen) ? (p.CSType + ".Zero") : "null";
else
result [i++] += p.Name;
}
return String.Join (", ", result);
}
}
}