Opentk/Source/Bind/Structures/Function.cs

369 lines
12 KiB
C#
Raw Normal View History

2007-08-01 21:14:39 +00:00
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
2007-08-01 21:14:39 +00:00
using System.Text.RegularExpressions;
2007-08-01 22:28:54 +00:00
using System.Diagnostics;
2007-08-01 09:27:57 +00:00
namespace Bind.Structures
{
2007-08-20 12:25:48 +00:00
public class Function : Delegate, IEquatable<Function>
{
2007-08-01 21:14:39 +00:00
internal static FunctionCollection Wrappers;
private static bool loaded;
2007-08-10 20:16:05 +00:00
#region internal static void Initialize()
2007-08-01 21:14:39 +00:00
internal static void Initialize()
{
if (!loaded)
{
Wrappers = new FunctionCollection();
loaded = true;
}
}
2007-08-10 20:16:05 +00:00
#endregion
2007-08-01 21:14:39 +00:00
static Regex endings = new Regex(@"((([df]|u?[isb])v?)|v)", RegexOptions.Compiled | RegexOptions.RightToLeft);
static Regex endingsNotToTrim = new Regex("(ib|[tdre]s|[eE]n[vd])", RegexOptions.Compiled | RegexOptions.RightToLeft);
2007-08-01 21:14:39 +00:00
/// <summary>
/// Add a trailing v to functions matching this regex. Used to differntiate between overloads taking both
/// a 'type' and a 'ref type' (such overloads are not CLS Compliant).
/// </summary>
/// <remarks>
/// The default Regex matches no functions. Create a new Regex in Bind.Generator classes to override the default behavior.
/// </remarks>
internal static Regex endingsAddV = new Regex("^0", RegexOptions.Compiled);
2007-08-01 09:27:57 +00:00
#region --- Constructors ---
public Function()
2007-08-01 09:27:57 +00:00
: base()
{
Body = new FunctionBody();
}
2007-08-01 09:27:57 +00:00
public Function(Delegate d)
: base(d)
{
2007-08-01 21:14:39 +00:00
if (d is Function)
this.Body = new FunctionBody((d as Function).Body);
else
this.Body = new FunctionBody();
this.Name = d.Name;
}
#endregion
2007-08-01 21:14:39 +00:00
#region public override bool Unsafe
2007-08-01 09:27:57 +00:00
public override bool Unsafe
{
2007-08-01 09:27:57 +00:00
get
{
2007-08-01 09:27:57 +00:00
if (Settings.Compatibility == Settings.Legacy.Tao)
return false;
2007-08-01 09:27:57 +00:00
return base.Unsafe;
}
}
2007-08-01 21:14:39 +00:00
#endregion
#region public FunctionBody Body
2007-08-01 09:27:57 +00:00
FunctionBody _body;
2007-08-01 09:27:57 +00:00
public FunctionBody Body
{
2007-08-01 09:27:57 +00:00
get { return _body; }
set { _body = value; }
}
#endregion
2007-08-01 21:14:39 +00:00
#region public string TrimmedName
/*
2007-08-01 21:14:39 +00:00
string trimmedName;
/// <summary>
2007-08-01 22:28:54 +00:00
/// Gets or sets the name of the opengl function, trimming the excess 234dfubsiv endings.
2007-08-01 21:14:39 +00:00
/// </summary>
public string TrimmedName
{
get { return trimmedName; }
set
{
if (!String.IsNullOrEmpty(value))
trimmedName = value.Trim();
}
}
*/
public string TrimmedName;
2007-08-01 21:14:39 +00:00
#endregion
#region public override string Name
/// <summary>
/// Gets or sets the name of the opengl function.
/// If no Tao compatibility is set, set TrimmedName to Name, after removing
/// [u][bsifd][v].
/// </summary>
public override string Name
{
get { return base.Name; }
set
{
base.Name = value;
2007-08-20 12:25:48 +00:00
if (Settings.Compatibility == Settings.Legacy.Tao)
{
// If we don't need compatibility with Tao,
// remove the Extension and the overload information from the name
// (Extension == "ARB", "EXT", etc, overload == [u][bsidf][v])
// TODO: Use some regex's here, to reduce clutter.
TrimmedName = value;
2007-08-20 12:25:48 +00:00
}
else
2007-08-01 21:14:39 +00:00
{
2007-08-10 20:16:05 +00:00
TrimmedName = Utilities.StripGL2Extension(value);
//if (TrimmedName.Contains("Uniform2iv"))
//{
// Console.Write("niar");
//}
2007-08-01 21:14:39 +00:00
// Remove overload
/*
2007-08-10 20:16:05 +00:00
for (int i = 3; i >= 1; i--)
2007-08-01 21:14:39 +00:00
{
2007-08-10 20:16:05 +00:00
if (endings.Contains(TrimmedName.Substring(TrimmedName.Length - i)))
{
// If there is a digit before the ending (e.g. 3fv) then we will remove
// the ending (some functions are blacklisted for CLS-Compliance).
// Otherwise, if there is no digit, but it ends with a 'v', do not remove
// the 'v' (CLS-Compliance). If no digit and it ends with a (plural) 's',
// do not remove anything (e.g. glCallLists)
// TODO: Add better handling for CLS-Compliance on ref ('v') functions.
if (Char.IsDigit(TrimmedName[TrimmedName.Length - (i + 1)]))
{
if (!endingsAddV.IsMatch(Name))
2007-08-10 20:16:05 +00:00
{
TrimmedName = TrimmedName.Substring(0, TrimmedName.Length - i);
}
else
{
Console.WriteLine("Function {0} blacklisted from trimming (CLS-Compliance).", Name);
}
}
else if (TrimmedName.EndsWith("v"))
{
TrimmedName = TrimmedName.Substring(0, TrimmedName.Length - i) + "v";
}
return;
}
2007-08-01 21:14:39 +00:00
}
*/
//if (Name.Contains("BooleanIndexed"))
{
}
Match m = endingsNotToTrim.Match(TrimmedName);
if ((m.Index + m.Length) != TrimmedName.Length)
{
// Some endings should not be trimmed, for example: 'b' from Attrib
m = endings.Match(TrimmedName);
if (m.Length > 0 && m.Index + m.Length == TrimmedName.Length)
{ // Only trim endings, not internal matches.
if (m.Value[m.Length - 1] == 'v' && endingsAddV.IsMatch(Name) &&
!Name.StartsWith("Get") && !Name.StartsWith("MatrixIndex"))
{ // Only trim ending 'v' when there is a number
TrimmedName = TrimmedName.Substring(0, m.Index) + "v";
}
else
{
if (!TrimmedName.EndsWith("xedv"))
TrimmedName = TrimmedName.Substring(0, m.Index);
else
TrimmedName = TrimmedName.Substring(0, m.Index + 1);
}
}
}
2007-08-01 21:14:39 +00:00
}
}
}
#endregion
2007-08-01 09:27:57 +00:00
#region public override string ToString()
2007-08-01 09:27:57 +00:00
public override string ToString()
{
StringBuilder sb = new StringBuilder();
2007-08-01 09:27:57 +00:00
sb.Append(Unsafe ? "unsafe " : "");
sb.Append(ReturnType);
sb.Append(" ");
if (Settings.Compatibility == Settings.Legacy.Tao)
{
2007-08-20 12:25:48 +00:00
sb.Append(Settings.FunctionPrefix);
2007-08-01 09:27:57 +00:00
}
2007-08-01 21:14:39 +00:00
sb.Append(!String.IsNullOrEmpty(TrimmedName) ? TrimmedName : Name);
2007-08-01 09:27:57 +00:00
sb.Append(Parameters.ToString(true));
if (Body.Count > 0)
{
2007-08-01 09:27:57 +00:00
sb.AppendLine();
sb.Append(Body.ToString());
}
return sb.ToString();
}
#endregion
2007-08-20 12:25:48 +00:00
#region IEquatable<Function> Members
2007-08-20 12:25:48 +00:00
public bool Equals(Function other)
{
return
!String.IsNullOrEmpty(this.TrimmedName) && !String.IsNullOrEmpty(other.TrimmedName) &&
2007-08-20 12:25:48 +00:00
this.TrimmedName == other.TrimmedName &&
this.Parameters.ToString(true) == other.Parameters.ToString(true);
}
#endregion
}
#region class FunctionBody : List<string>
public class FunctionBody : List<string>
{
public FunctionBody()
{
}
public FunctionBody(FunctionBody fb)
{
foreach (string s in fb)
{
this.Add(s);
}
}
2007-08-20 12:25:48 +00:00
private string indent = "";
public void Indent()
{
indent += " ";
}
public void Unindent()
{
if (indent.Length >= 4)
indent = indent.Substring(4);
}
new public void Add(string s)
{
base.Add(indent + s);
}
new public void AddRange(IEnumerable<string> collection)
{
foreach (string t in collection)
{
this.Add(t);
}
}
public override string ToString()
{
if (this.Count == 0)
return String.Empty;
StringBuilder sb = new StringBuilder(this.Count);
2007-08-01 09:27:57 +00:00
sb.AppendLine("{");
foreach (string s in this)
{
2007-08-01 09:27:57 +00:00
sb.AppendLine(" " + s);
}
2007-08-01 09:27:57 +00:00
sb.AppendLine("}");
return sb.ToString();
}
}
#endregion
2007-08-01 09:27:57 +00:00
2007-08-01 21:14:39 +00:00
#region class FunctionCollection : Dictionary<string, List<Function>>
2007-08-01 09:27:57 +00:00
class FunctionCollection : Dictionary<string, List<Function>>
{
2007-08-20 12:25:48 +00:00
Regex unsignedFunctions = new Regex(@".+(u[dfisb]v?)", RegexOptions.Compiled);
2007-08-01 09:27:57 +00:00
public void Add(Function f)
{
if (!this.ContainsKey(f.Extension))
{
this.Add(f.Extension, new List<Function>());
this[f.Extension].Add(f);
}
else
{
this[f.Extension].Add(f);
}
}
public void AddRange(IEnumerable<Function> functions)
{
foreach (Function f in functions)
{
this.Add(f);
}
}
2007-08-01 22:28:54 +00:00
/// <summary>
/// Adds the function to the collection, if a function with the same
/// name and parameters doesn't already exist.
/// </summary>
/// <param name="f">The Function to add.</param>
public void AddChecked(Function f)
{
if (Bind.Structures.Function.Wrappers.ContainsKey(f.Extension))
{
2007-08-20 12:25:48 +00:00
int index = Bind.Structures.Function.Wrappers[f.Extension].IndexOf(f);
if (index == -1)
2007-08-01 22:28:54 +00:00
{
2007-08-20 12:25:48 +00:00
Bind.Structures.Function.Wrappers.Add(f);
}
else
{
if (unsignedFunctions.IsMatch(Utilities.StripGL2Extension(f.Name)))// &&
//!unsignedFunctions.IsMatch(
// Utilities.StripGL2Extension(Bind.Structures.Function.Wrappers[f.Extension][index].Name)))
{
Bind.Structures.Function.Wrappers[f.Extension].RemoveAt(index);
Bind.Structures.Function.Wrappers[f.Extension].Add(f);
}
2007-08-01 22:28:54 +00:00
}
}
2007-08-20 12:25:48 +00:00
else
{
2007-08-01 22:28:54 +00:00
Bind.Structures.Function.Wrappers.Add(f);
2007-08-20 12:25:48 +00:00
}
2007-08-01 22:28:54 +00:00
}
2007-08-01 09:27:57 +00:00
}
2007-08-01 21:14:39 +00:00
#endregion
}