Opentk/Source/Bind/Structures/Parameter.cs

423 lines
12 KiB
C#
Raw Normal View History

#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
2007-08-01 09:27:57 +00:00
namespace Bind.Structures
{
#region Parameter class
/// <summary>
/// Represents a single parameter of an opengl function.
/// </summary>
2007-08-01 21:14:39 +00:00
public class Parameter : Type
{
#region Constructors
/// <summary>
/// Creates a new Parameter without type and name.
/// </summary>
public Parameter()
2007-08-01 21:14:39 +00:00
:base()
{
}
/// <summary>
/// Creates a new parameter from the parameters passed (deep copy).
/// </summary>
/// <param name="p">The parameter to copy from.</param>
public Parameter(Parameter p)
2007-08-01 21:14:39 +00:00
: base(p)
{
if (p == null)
return;
2007-08-01 09:27:57 +00:00
this.Name = !String.IsNullOrEmpty(p.Name) ? new string(p.Name.ToCharArray()) : "";
this.Unchecked = p.Unchecked;
this.UnmanagedType = p.UnmanagedType;
2007-08-01 09:27:57 +00:00
this.Flow = p.Flow;
}
#endregion
2007-08-01 21:14:39 +00:00
#region public string Name
string _name;
/// <summary>
/// Gets or sets the name of the parameter.
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
#endregion
#region UnmanagedType property
UnmanagedType _unmanaged_type;
/// <summary>
/// Gets or sets the name of the parameter.
/// </summary>
2007-08-01 21:14:39 +00:00
private UnmanagedType UnmanagedType
{
get { return _unmanaged_type; }
set { _unmanaged_type = value; }
}
#endregion
2007-08-01 21:14:39 +00:00
#region public FlowDirection Flow
/// <summary>
/// Enumarates the possible flows of a parameter (ie. is this parameter
/// used as input or as output?)
/// </summary>
public enum FlowDirection
{
Undefined = 0,
In,
Out
}
FlowDirection _flow;
/// <summary>
/// Gets or sets the flow of the parameter.
/// </summary>
public FlowDirection Flow
{
get { return _flow; }
set { _flow = value; }
}
#endregion
2007-08-01 09:27:57 +00:00
#region public bool NeedsPin
public bool NeedsPin
{
get { return
2007-08-01 21:14:39 +00:00
(Array > 0 || Reference || CurrentType == "object") &&
!CurrentType.ToLower().Contains("string");
}
2007-08-01 09:27:57 +00:00
}
#endregion
#region public bool Unchecked
private bool _unchecked;
public bool Unchecked
{
get { return _unchecked; }
set { _unchecked = value; }
}
#endregion
2007-08-01 09:27:57 +00:00
#region override public string ToString()
override public string ToString()
2007-08-01 09:27:57 +00:00
{
return ToString(false);
}
#endregion
#region public string ToString(bool taoCompatible)
public string ToString(bool taoCompatible)
{
StringBuilder sb = new StringBuilder();
2007-08-01 09:27:57 +00:00
//if (UnmanagedType == UnmanagedType.AsAny && Flow == FlowDirection.In)
// sb.Append("[MarshalAs(UnmanagedType.AsAny)] ");
2007-08-01 09:27:57 +00:00
//if (UnmanagedType == UnmanagedType.LPArray)
// sb.Append("[MarshalAs(UnmanagedType.LPArray)] ");
//if (Flow == FlowDirection.Out && !Array && !(Type == "IntPtr"))
// sb.Append("out ");
2007-08-01 21:14:39 +00:00
if (Flow == FlowDirection.Out)
sb.Append("[Out] ");
else if (Flow == FlowDirection.Undefined)
sb.Append("[In, Out] ");
2007-08-01 09:27:57 +00:00
if (Reference)
{
if (Flow == FlowDirection.Out)
sb.Append("out ");
else
sb.Append("ref ");
}
2007-08-01 09:27:57 +00:00
if (taoCompatible && Settings.Compatibility == Settings.Legacy.Tao)
{
if (Pointer)
{
sb.Append("IntPtr");
}
else
{
2007-08-01 21:14:39 +00:00
sb.Append(CurrentType);
2007-08-01 09:27:57 +00:00
if (Array > 0)
sb.Append("[]");
}
}
else
{
2007-08-01 21:14:39 +00:00
sb.Append(CurrentType);
2007-08-01 09:27:57 +00:00
if (Pointer)
sb.Append("*");
if (Array > 0)
sb.Append("[]");
}
2007-08-01 09:27:57 +00:00
if (!String.IsNullOrEmpty(Name))
{
sb.Append(" ");
sb.Append(Utilities.Keywords.Contains(Name) ? "@" + Name : Name);
}
return sb.ToString();
}
2007-08-01 09:27:57 +00:00
#endregion
2007-08-01 21:14:39 +00:00
#region internal static Parameter Translate(Parameter par, string Category)
2007-08-01 21:14:39 +00:00
internal static Parameter Translate(Parameter par, string Category)
{
Enum @enum;
string s;
Parameter p = new Parameter(par);
// Translate enum types
if (Enum.GLEnums.TryGetValue(p.CurrentType, out @enum) && @enum.Name != "GLenum")
{
if (Settings.Compatibility == Settings.Legacy.Tao)
p.CurrentType = "int";
else
p.CurrentType = p.CurrentType.Insert(0, String.Format("{0}.", Settings.GLEnumsClass));
}
else if (Bind.Structures.Type.GLTypes.TryGetValue(p.CurrentType, out s))
{
// Check if the parameter is a generic GLenum. If yes,
// check if a better match exists:
if (s.Contains("GLenum") && !String.IsNullOrEmpty(Category))
{
if (Settings.Compatibility == Settings.Legacy.None)
{
// Better match: enum.Name == function.Category (e.g. GL_VERSION_1_1 etc)
if (Enum.GLEnums.ContainsKey(Category))
{
p.CurrentType = String.Format("{0}.{1}", Settings.GLEnumsClass, Category);
}
else
{
p.CurrentType = String.Format("{0}.{1}", Settings.GLEnumsClass, Settings.CompleteEnumName);
2007-08-01 21:14:39 +00:00
}
}
else
{
p.CurrentType = "int";
}
}
else
{
// This is not enum, default translation:
p.CurrentType = s;
p.CurrentType =
Bind.Structures.Type.CSTypes.ContainsKey(p.CurrentType) ?
Bind.Structures.Type.CSTypes[p.CurrentType] : p.CurrentType;
}
}
//if (CSTypes.ContainsKey(p.CurrentType))
// p.CurrentType = CSTypes[p.CurrentType];
// Translate pointer parameters
if (p.Pointer)
{
p.WrapperType = WrapperTypes.ArrayParameter;
if (p.CurrentType.ToLower().Contains("char") || p.CurrentType.ToLower().Contains("string"))
{
// char* or string -> [In] String or [Out] StringBuilder
p.CurrentType =
p.Flow == Parameter.FlowDirection.Out ?
"System.Text.StringBuilder" :
"System.String";
p.Pointer = false;
p.WrapperType = WrapperTypes.None;
}
else if (p.CurrentType.ToLower().Contains("void"))
{
p.WrapperType = WrapperTypes.GenericParameter;
}
}
if (p.CurrentType.ToLower().Contains("bool"))
{
// Is this actually used anywhere?
p.WrapperType = WrapperTypes.BoolParameter;
}
return p;
}
#endregion
}
#endregion
#region ParameterCollection class
/// <summary>
/// Holds the parameter list of an opengl function.
/// </summary>
public class ParameterCollection : List<Parameter>
{
#region Constructors
public ParameterCollection()
{
}
public ParameterCollection(ParameterCollection pc)
{
foreach (Parameter p in pc)
{
this.Add(new Parameter(p));
}
}
#endregion
#region override public string ToString()
/// <summary>
2007-08-01 09:27:57 +00:00
/// Gets the parameter declaration string.
/// </summary>
/// <returns>The parameter list of an opengl function in the form ( [parameters] )</returns>
override public string ToString()
2007-08-01 09:27:57 +00:00
{
2007-08-01 21:14:39 +00:00
return ToString(false);
2007-08-01 09:27:57 +00:00
}
#endregion
2007-08-01 21:14:39 +00:00
#region public string ToString(bool taoCompatible)
2007-08-01 09:27:57 +00:00
/// <summary>
/// Gets the parameter declaration string.
/// </summary>
/// <param name="getCLSCompliant">If true, all types will be replaced by their CLSCompliant C# equivalents</param>
/// <param name="CSTypes">The list of C# types equivalent to the OpenGL types.</param>
/// <returns>The parameter list of an opengl function in the form ( [parameters] )</returns>
2007-08-01 21:14:39 +00:00
public string ToString(bool taoCompatible)
{
StringBuilder sb = new StringBuilder();
sb.Append("(");
if (this.Count > 0)
{
foreach (Parameter p in this)
{
2007-08-01 09:27:57 +00:00
if (taoCompatible)
{
sb.Append(p.ToString(true));
}
else
{
sb.Append(p.ToString());
}
sb.Append(", ");
}
sb.Replace(", ", ")", sb.Length - 2, 2);
}
else
sb.Append(")");
return sb.ToString();
}
2007-08-01 09:27:57 +00:00
#endregion
2007-08-01 21:14:39 +00:00
public string CallString()
{
return CallString(false);
}
public string CallString(bool taoCompatible)
{
StringBuilder sb = new StringBuilder();
sb.Append("(");
if (this.Count > 0)
{
foreach (Parameter p in this)
{
if (p.Unchecked)
sb.Append("unchecked((" + p.CurrentType + ")");
if (p.CurrentType != "object")
{
if (p.CurrentType.ToLower().Contains("string"))
{
sb.Append(String.Format(
"({0}{1})",
p.CurrentType,
(p.Array > 0) ? "[]" : ""));
}
else
{
sb.Append(String.Format(
"({0}{1})",
p.CurrentType,
(p.Pointer || p.Array > 0 || p.Reference) ? "*" : ""));
}
}
sb.Append(
Utilities.Keywords.Contains(p.Name) ? "@" + p.Name : p.Name
);
if (p.Unchecked)
sb.Append(")");
sb.Append(", ");
}
sb.Replace(", ", ")", sb.Length - 2, 2);
}
else
{
sb.Append(")");
}
return sb.ToString();
}
public bool ContainsType(string type)
{
foreach (Parameter p in this)
2007-08-01 21:14:39 +00:00
if (p.CurrentType == type)
return true;
return false;
}
}
#endregion
}