Opentk/Source/Bind/Structures/Delegate.cs

931 lines
32 KiB
C#
Raw Normal View History

2007-08-01 23:14:39 +02:00
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
2007-08-01 11:27:57 +02:00
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
2007-08-01 23:14:39 +02:00
using System.IO;
2007-08-01 11:27:57 +02:00
namespace Bind.Structures
{
/// <summary>
/// Represents an opengl function.
/// The return value, function name, function parameters and opengl version can be retrieved or set.
/// </summary>
public class Delegate
{
2007-08-01 23:14:39 +02:00
internal static DelegateCollection Delegates;
private static bool delegatesLoaded;
2007-08-10 22:16:05 +02:00
#region internal static void Initialize(string glSpec, string glSpecExt)
internal static void Initialize(string glSpec, string glSpecExt)
2007-08-01 23:14:39 +02:00
{
if (!delegatesLoaded)
{
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, glSpec))
2007-08-01 23:14:39 +02:00
{
Delegates = Bind.MainClass.Generator.ReadDelegates(sr);
}
if (!String.IsNullOrEmpty(glSpecExt))
{
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, glSpecExt))
{
foreach (Delegate d in Bind.MainClass.Generator.ReadDelegates(sr).Values)
{
Utilities.Merge(Delegates, d);
}
}
}
2007-08-01 23:14:39 +02:00
delegatesLoaded = true;
}
}
2007-08-10 22:16:05 +02:00
#endregion
2007-08-01 11:27:57 +02:00
#region --- Constructors ---
public Delegate()
{
Parameters = new ParameterCollection();
}
public Delegate(Delegate d)
{
this.Category = !String.IsNullOrEmpty(d.Category) ? new string(d.Category.ToCharArray()) : "";
2007-08-01 11:27:57 +02:00
//this.Extension = !String.IsNullOrEmpty(d.Extension) ? new string(d.Extension.ToCharArray()) : "";
this.Name = new string(d.Name.ToCharArray());
2007-08-01 23:14:39 +02:00
//this.NeedsWrapper = d.NeedsWrapper;
2007-08-01 11:27:57 +02:00
this.Parameters = new ParameterCollection(d.Parameters);
2007-08-01 23:14:39 +02:00
this.ReturnType = new Type(d.ReturnType);
2007-08-01 11:27:57 +02:00
this.Version = !String.IsNullOrEmpty(d.Version) ? new string(d.Version.ToCharArray()) : "";
//this.Unsafe = d.Unsafe;
}
#endregion
#region --- Properties ---
#region public bool CLSCompliant
/// <summary>
/// Gets the CLSCompliant property. True if the delegate is not CLSCompliant.
/// </summary>
public bool CLSCompliant
{
get
{
if (Unsafe)
return false;
if (!ReturnType.CLSCompliant)
return false;
foreach (Parameter p in Parameters)
{
if (!p.CLSCompliant)
return false;
}
return true;
}
}
#endregion
#region public string Category
private string _category;
public string Category
{
get { return _category; }
set { _category = value; }
}
#endregion
#region public bool NeedsWrapper
/// <summary>
/// Indicates whether this function needs to be wrapped with a Marshaling function.
/// This flag is set if a function contains an Array parameter, or returns
/// an Array or string.
/// </summary>
public bool NeedsWrapper
{
2007-08-01 23:14:39 +02:00
//get { return _needs_wrapper; }
//set { _needs_wrapper = value; }
get
{
// TODO: Add special cases for (Get)ShaderSource.
if (ReturnType.WrapperType != WrapperTypes.None)
return true;
foreach (Parameter p in Parameters)
{
if (p.WrapperType != WrapperTypes.None)
return true;
}
return false;
}
2007-08-01 11:27:57 +02:00
}
#endregion
#region public virtual bool Unsafe
/// <summary>
/// True if the delegate must be declared as 'unsafe'.
/// </summary>
public virtual bool Unsafe
{
//get { return @unsafe; }
//set { @unsafe = value; }
get
{
if (ReturnType.Pointer)
return true;
foreach (Parameter p in Parameters)
{
if (p.Pointer)
{
return true;
}
}
return false;
}
}
#endregion
#region public Parameter ReturnType
2007-08-01 23:14:39 +02:00
Type _return_type = new Type();
2007-08-01 11:27:57 +02:00
/// <summary>
/// Gets or sets the return value of the opengl function.
/// </summary>
2007-08-01 23:14:39 +02:00
public Type ReturnType
2007-08-01 11:27:57 +02:00
{
get { return _return_type; }
set
{
2007-08-01 23:14:39 +02:00
_return_type = Type.Translate(value);
2007-08-01 11:27:57 +02:00
}
}
#endregion
2007-08-01 23:14:39 +02:00
#region public virtual string Name
2007-08-01 11:27:57 +02:00
string _name;
/// <summary>
/// Gets or sets the name of the opengl function.
/// </summary>
2007-08-01 23:14:39 +02:00
public virtual string Name
2007-08-01 11:27:57 +02:00
{
get { return _name; }
set
{
if (!String.IsNullOrEmpty(value))
_name = value.Trim();
}
}
#endregion
#region public ParameterCollection Parameters
ParameterCollection _parameters;
public ParameterCollection Parameters
{
get { return _parameters; }
2007-08-01 23:14:39 +02:00
protected set { _parameters = value; }
2007-08-01 11:27:57 +02:00
}
#endregion
#region public string Version
string _version;
/// <summary>
/// Defines the opengl version that introduced this function.
/// </summary>
public string Version
{
get { return _version; }
set { _version = value; }
}
#endregion
#region public bool Extension
string _extension;
public string Extension
{
//get { return _extension; }
//set { _extension = value; }
get
{
if (!String.IsNullOrEmpty(Name))
{
_extension = Utilities.GetGL2Extension(Name);
return String.IsNullOrEmpty(_extension) ? "Core" : _extension;
}
else
{
return null;
}
}
}
#endregion
#endregion
#region --- Strings ---
#region public string CallString()
public string CallString()
{
StringBuilder sb = new StringBuilder();
sb.Append(Settings.DelegatesClass);
sb.Append(".gl");
sb.Append(Name);
2007-08-01 23:14:39 +02:00
sb.Append(Parameters.CallString());
2007-08-01 11:27:57 +02:00
return sb.ToString();
}
#endregion
#region public string DeclarationString()
public string DeclarationString()
{
StringBuilder sb = new StringBuilder();
sb.Append(Unsafe ? "unsafe " : "");
sb.Append(ReturnType);
sb.Append(" ");
sb.Append(Name);
sb.Append(Parameters.ToString());
return sb.ToString();
}
#endregion
#region override public string ToString()
/// <summary>
/// Gets the string representing the full function declaration without decorations
/// (ie "void glClearColor(float red, float green, float blue, float alpha)"
/// </summary>
override public string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(Unsafe ? "unsafe " : "");
sb.Append("delegate ");
sb.Append(ReturnType);
sb.Append(" ");
sb.Append(Name);
sb.Append(Parameters.ToString());
return sb.ToString();
}
#endregion
2007-08-01 23:14:39 +02:00
public Delegate GetCLSCompliantDelegate()
2007-08-01 11:27:57 +02:00
{
Delegate f = new Delegate(this);
for (int i = 0; i < f.Parameters.Count; i++)
{
2007-08-01 23:14:39 +02:00
f.Parameters[i].CurrentType = f.Parameters[i].GetCLSCompliantType();
2007-08-01 11:27:57 +02:00
}
2007-08-01 23:14:39 +02:00
f.ReturnType.CurrentType = f.ReturnType.GetCLSCompliantType();
2007-08-01 11:27:57 +02:00
return f;
}
#endregion
#region --- Wrapper Creation ---
2007-08-01 23:14:39 +02:00
#region public IEnumerable<Function> CreateWrappers()
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
public IEnumerable<Function> CreateWrappers()
2007-08-01 11:27:57 +02:00
{
2007-08-01 23:14:39 +02:00
if (this.Name.Contains("GetString"))
2007-08-01 11:27:57 +02:00
{
}
List<Function> wrappers = new List<Function>();
if (!NeedsWrapper)
{
// No special wrapper needed - just call this delegate:
Function f = new Function(this);
2007-08-01 23:14:39 +02:00
if (f.ReturnType.CurrentType.ToLower().Contains("void"))
2007-08-01 11:27:57 +02:00
f.Body.Add(String.Format("{0};", f.CallString()));
else
f.Body.Add(String.Format("return {0};", f.CallString()));
wrappers.Add(f);
}
else
{
2007-08-01 23:14:39 +02:00
Function f = WrapReturnType();
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
WrapParameters(new Function((Function)f ?? this), wrappers);
}
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
return wrappers;
}
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
#endregion
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
#region protected Function WrapReturnType()
protected Function WrapReturnType()
{
// We have to add wrappers for all possible WrapperTypes.
Function f;
// First, check if the return type needs wrapping:
switch (this.ReturnType.WrapperType)
{
// If the function returns a string (glGetString) we must manually marshal it
// using Marshal.PtrToStringXXX. Otherwise, the GC will try to free the memory
// used by the string, resulting in corruption (the memory belongs to the
// unmanaged boundary).
case WrapperTypes.StringReturnType:
f = new Function(this);
f.ReturnType.CurrentType = "System.String";
f.Body.Add(
String.Format(
"return System.Runtime.InteropServices.Marshal.PtrToStringAnsi({0});",
this.CallString()
)
);
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
return f; // Only occurs in glGetString, there's no need to check parameters.
// If the function returns a void* (GenericReturnValue), we'll have to return an IntPtr.
// The user will unfortunately need to marshal this IntPtr to a data type manually.
case WrapperTypes.GenericReturnType:
ReturnType.CurrentType = "IntPtr";
ReturnType.Pointer = false;
break;
case WrapperTypes.None:
default:
// No return wrapper needed
break;
2007-08-01 11:27:57 +02:00
}
2007-08-01 23:14:39 +02:00
return null;
2007-08-01 11:27:57 +02:00
}
2007-08-01 23:14:39 +02:00
2007-08-01 11:27:57 +02:00
#endregion
#region protected void WrapParameters(Function function, List<Function> wrappers)
protected static int index = 0;
/// <summary>
/// This function needs some heavy refactoring. I'm ashamed I ever wrote it, but it works...
/// What it does is this: it adds to the wrapper list all possible wrapper permutations
/// for functions that have more than one IntPtr parameter. Example:
/// "void Delegates.f(IntPtr p, IntPtr q)" where p and q are pointers to void arrays needs the following wrappers:
/// "void f(IntPtr p, IntPtr q)"
/// "void f(IntPtr p, object q)"
/// "void f(object p, IntPtr q)"
/// "void f(object p, object q)"
/// </summary>
2007-08-01 23:14:39 +02:00
protected void WrapParameters(Function function, List<Function> wrappers)
2007-08-01 11:27:57 +02:00
{
2007-08-01 23:14:39 +02:00
if (function.Name == "GetString")
2007-08-01 11:27:57 +02:00
{
}
if (index == 0)
{
bool containsPointerParameters = false;
// Check if there are any IntPtr parameters (we may have come here from a ReturnType wrapper
// such as glGetString, which contains no IntPtr parameters)
foreach (Parameter p in function.Parameters)
{
if (p.Pointer)
{
containsPointerParameters = true;
break;
}
}
if (containsPointerParameters)
{
wrappers.Add(DefaultWrapper(function));
}
else
{
2007-08-01 23:14:39 +02:00
if (function.Body.Count == 0)
wrappers.Add(DefaultWrapper(function));
else
wrappers.Add(function);
2007-08-01 11:27:57 +02:00
return;
}
}
if (index >= 0 && index < function.Parameters.Count)
{
Function f;
if (function.Parameters[index].WrapperType == WrapperTypes.None)
{
// No wrapper needed, visit the next parameter
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(function, wrappers);
2007-08-01 11:27:57 +02:00
--index;
}
else
{
switch (function.Parameters[index].WrapperType)
{
case WrapperTypes.ArrayParameter:
// Recurse to the last parameter
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(function, wrappers);
2007-08-01 11:27:57 +02:00
--index;
// On stack rewind, create array wrappers
2007-08-01 23:14:39 +02:00
f = ArrayWrapper(new Function(function), index);
2007-08-01 11:27:57 +02:00
wrappers.Add(f);
// Recurse to the last parameter again, keeping the Array wrappers
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(f, wrappers);
2007-08-01 11:27:57 +02:00
--index;
// On stack rewind, create Ref wrappers.
2007-08-01 23:14:39 +02:00
f = ReferenceWrapper(new Function(function), index);
2007-08-01 11:27:57 +02:00
wrappers.Add(f);
// Keeping the current Ref wrapper, visit all other parameters once more
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(f, wrappers);
2007-08-01 11:27:57 +02:00
--index;
break;
case WrapperTypes.GenericParameter:
// Recurse to the last parameter
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(function, wrappers);
2007-08-01 11:27:57 +02:00
--index;
// On stack rewind, create array wrappers
2007-08-01 23:14:39 +02:00
f = GenericWrapper(new Function(function), index);
2007-08-01 11:27:57 +02:00
wrappers.Add(f);
// Keeping the current Object wrapper, visit all other parameters once more
++index;
2007-08-01 23:14:39 +02:00
WrapParameters(f, wrappers);
2007-08-01 11:27:57 +02:00
--index;
break;
}
}
}
}
#endregion
2007-08-01 23:14:39 +02:00
#region protected Function GenericWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
protected Function GenericWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
{
// Search and replace IntPtr parameters with the known parameter types:
function.Parameters[index].Reference = false;
function.Parameters[index].Array = 0;
function.Parameters[index].Pointer = false;
2007-08-01 23:14:39 +02:00
function.Parameters[index].CurrentType = "object";
2007-08-01 11:27:57 +02:00
function.Parameters[index].Flow = Parameter.FlowDirection.Undefined;
// In the function body we should pin all objects in memory before calling the
// low-level function.
function.Body.Clear();
//function.Body.AddRange(GetBodyWithFixedPins(function));
2007-08-01 23:14:39 +02:00
function.Body.AddRange(function.GetBodyWithPins(false));
2007-08-01 11:27:57 +02:00
return function;
}
#endregion
2007-08-01 23:14:39 +02:00
#region protected Function ReferenceWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
protected Function ReferenceWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
{
// Search and replace IntPtr parameters with the known parameter types:
function.Parameters[index].Reference = true;
function.Parameters[index].Array = 0;
function.Parameters[index].Pointer = false;
// In the function body we should pin all objects in memory before calling the
// low-level function.
function.Body.Clear();
2007-08-01 23:14:39 +02:00
function.Body.AddRange(function.GetBodyWithPins(false));
2007-08-01 11:27:57 +02:00
return function;
}
#endregion
2007-08-01 23:14:39 +02:00
#region protected Function ArrayWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
2007-08-01 23:14:39 +02:00
protected Function ArrayWrapper(Function function, int index)
2007-08-01 11:27:57 +02:00
{
// Search and replace IntPtr parameters with the known parameter types:
function.Parameters[index].Array = 1;
function.Parameters[index].Pointer = false;
function.Parameters[index].Flow = Parameter.FlowDirection.Undefined;
// In the function body we should pin all objects in memory before calling the
// low-level function.
function.Body.Clear();
2007-08-01 23:14:39 +02:00
function.Body.AddRange(function.GetBodyWithPins(false));
2007-08-01 11:27:57 +02:00
return function;
}
#endregion
#region protected Function DefaultWrapper(Function f)
protected Function DefaultWrapper(Function f)
{
2007-08-01 23:14:39 +02:00
bool returns = f.ReturnType.CurrentType.ToLower().Contains("void") && !f.ReturnType.Pointer;
2007-08-01 11:27:57 +02:00
string callString = String.Format(
"{0} {1}{2}; {3}",
Unsafe ? "unsafe {" : "",
returns ? "" : "return ",
2007-08-01 23:14:39 +02:00
this.CallString(),
2007-08-01 11:27:57 +02:00
Unsafe ? "}" : "");
f.Body.Add(callString);
return f;
}
#endregion
2007-08-01 23:14:39 +02:00
#region protected FunctionBody GetBodyWithPins(bool wantCLSCompliance)
2007-08-01 11:27:57 +02:00
/// <summary>
/// Generates a body which calls the specified function, pinning all needed parameters.
/// </summary>
/// <param name="function"></param>
2007-08-01 23:14:39 +02:00
protected FunctionBody GetBodyWithPins(bool wantCLSCompliance)
2007-08-01 11:27:57 +02:00
{
// We'll make changes, but we want the original intact.
2007-08-01 23:14:39 +02:00
//Function function = this as Function ?? new Function(this);
//Function f = new Function(function);
Function f = new Function(this);
2007-08-01 11:27:57 +02:00
f.Body.Clear();
// Unsafe only if
//function.Unsafe = false;
// Add default initliazers for out parameters:
2007-08-01 23:14:39 +02:00
foreach (Parameter p in this.Parameters)
2007-08-01 11:27:57 +02:00
{
if (p.Flow == Parameter.FlowDirection.Out)
{
f.Body.Add(
String.Format(
"{0} = default({1});",
p.Name,
2007-08-01 23:14:39 +02:00
p.GetFullType(Bind.Structures.Type.CSTypes, wantCLSCompliance)
2007-08-01 11:27:57 +02:00
)
);
}
}
// All GCHandles statements will go here. This will allow to place only one opening '{'
// on fixed statements.
int handleStart = f.Body.Count;
// Indicates the index where the last GCHandle statement is. Used to add an unsafe stamement
// (if needed) at exactl that spot, i.e. after the GCHandles but before the fixed statements.
int handleEnd = f.Body.Count;
// True if at least on GCHandle is allocated. Used to remove the try { } finally { }
// block if no handle has been allocated.
bool handleAllocated = false;
// True if function body contains at least one fixed statement. Add a statement-level
// unsafe block if true (and the function is not unsafe at the function-level).
bool fixedAllocated = false;
// Obtain pointers by pinning the parameters
foreach (Parameter p in f.Parameters)
{
if (p.NeedsPin)
{
// Use GCHandle to obtain pointer to generic parameters and 'fixed' for arrays.
// This is because fixed can only take the address of fields, not managed objects.
if (p.WrapperType == WrapperTypes.GenericParameter)
{
f.Body.Insert(
handleStart,
String.Format(
"{0} {1} = {0}.Alloc({2}, System.Runtime.InteropServices.GCHandleType.Pinned);",
"System.Runtime.InteropServices.GCHandle",
p.Name + "_ptr",
p.Name
)
);
// Note! The following line modifies f.Parameters, *not* function.Parameters
p.Name = "(void*)" + p.Name + "_ptr.AddrOfPinnedObject()";
handleAllocated = true;
handleEnd++;
}
else
{
f.Body.Add(
String.Format(
" fixed ({0}* {1} = {2})",
2007-08-01 23:14:39 +02:00
wantCLSCompliance && !p.CLSCompliant ?
p.GetCLSCompliantType() :
p.CurrentType,
2007-08-01 11:27:57 +02:00
p.Name + "_ptr",
p.Array > 0 ? p.Name : "&" + p.Name
)
);
p.Name = p.Name + "_ptr";
fixedAllocated = true;
}
}
}
2007-08-01 23:14:39 +02:00
if (!this.Unsafe)
2007-08-01 11:27:57 +02:00
{
f.Body.Insert(handleEnd, "unsafe");
f.Body.Insert(handleEnd + 1, "{");
}
if (handleAllocated)
{
f.Body.Add(" try");
}
f.Body.Add(" {");
// Add delegate call:
2007-08-01 23:14:39 +02:00
if (f.ReturnType.CurrentType.ToLower().Contains("void"))
2007-08-01 11:27:57 +02:00
f.Body.Add(String.Format(" {0};", f.CallString()));
else
2007-08-01 23:14:39 +02:00
f.Body.Add(String.Format(" {0} {1} = {2};", f.ReturnType.CurrentType, "retval", f.CallString()));
2007-08-01 11:27:57 +02:00
// Assign out parameters:
2007-08-01 23:14:39 +02:00
foreach (Parameter p in this.Parameters)
2007-08-01 11:27:57 +02:00
{
if (p.Flow == Parameter.FlowDirection.Out)
{
// Check each out parameter. If it has been pinned, get the Target of the GCHandle.
// Otherwise, nothing needs be done.
if (p.NeedsPin)
{
if (p.WrapperType == WrapperTypes.GenericParameter)
{
f.Body.Add(
String.Format(
" {0} = ({1}){2}.Target;",
p.Name,
2007-08-01 23:14:39 +02:00
p.CurrentType,
2007-08-01 11:27:57 +02:00
p.Name + "_ptr"
)
);
}
else
{
f.Body.Add(
String.Format(
" {0} = *{0}_ptr;",
p.Name
)
);
}
}
}
}
// Return:
2007-08-01 23:14:39 +02:00
if (!f.ReturnType.CurrentType.ToLower().Contains("void"))
2007-08-01 11:27:57 +02:00
{
f.Body.Add(" return retval;");
}
if (handleAllocated)
{
f.Body.Add(" }");
f.Body.Add(" finally");
f.Body.Add(" {");
2007-08-01 23:14:39 +02:00
foreach (Parameter p in this.Parameters)
2007-08-01 11:27:57 +02:00
{
// Free all allocated GCHandles
if (p.NeedsPin)
{
if (p.WrapperType == WrapperTypes.GenericParameter)
f.Body.Add(String.Format(" {0}_ptr.Free();", p.Name));
//else
// f.Body.Add("}");
}
}
}
f.Body.Add(" }");
2007-08-01 23:14:39 +02:00
if (!this.Unsafe)
2007-08-01 11:27:57 +02:00
{
f.Body.Add("}");
}
return f.Body;
}
#endregion
#endregion
2007-08-01 23:14:39 +02:00
/// <summary>
/// Translates the opengl return type to the equivalent C# type.
/// </summary>
/// <param name="d">The opengl function to translate.</param>
/// <remarks>
/// First, we use the official typemap (gl.tm) to get the correct type.
/// Then we override this, when it is:
/// 1) A string (we have to use Marshal.PtrToStringAnsi, to avoid heap corruption)
/// 2) An array (translates to IntPtr)
/// 3) A generic object or void* (translates to IntPtr)
/// 4) A GLenum (translates to int on Legacy.Tao or GL.Enums.GLenum otherwise).
/// Return types must always be CLS-compliant, because .Net does not support overloading on return types.
/// </remarks>
protected virtual void TranslateReturnType()
{
if (Bind.Structures.Type.GLTypes.ContainsKey(ReturnType.CurrentType))
ReturnType.CurrentType = Bind.Structures.Type.GLTypes[ReturnType.CurrentType];
if (Bind.Structures.Type.CSTypes.ContainsKey(ReturnType.CurrentType))
ReturnType.CurrentType = Bind.Structures.Type.CSTypes[ReturnType.CurrentType];
if (ReturnType.CurrentType.ToLower().Contains("void") && ReturnType.Pointer)
{
ReturnType.WrapperType = WrapperTypes.GenericReturnType;
}
if (ReturnType.CurrentType.ToLower().Contains("string"))
{
ReturnType.CurrentType = "IntPtr";
ReturnType.WrapperType = WrapperTypes.StringReturnType;
}
if (ReturnType.CurrentType.ToLower().Contains("object"))
{
ReturnType.CurrentType = "IntPtr";
ReturnType.WrapperType |= WrapperTypes.GenericReturnType;
}
if (ReturnType.CurrentType.Contains("GLenum"))
{
if (Settings.Compatibility == Settings.Legacy.None)
ReturnType.CurrentType =
String.Format("{0}.{1}",
Settings.GLEnumsClass,
Settings.CompleteEnumName);
2007-08-01 23:14:39 +02:00
else
ReturnType.CurrentType = "int";
}
if (ReturnType.CurrentType.ToLower().Contains("bool"))
{
if (Settings.Compatibility == Settings.Legacy.Tao)
ReturnType.CurrentType = "int";
else
{
}
//ReturnType.WrapperType = WrapperTypes.ReturnsBool;
}
ReturnType.CurrentType = ReturnType.GetCLSCompliantType();
}
protected virtual void TranslateParameters()
{
if (this.Name.Contains("MultiTexCoord1"))
{
}
for (int i = 0; i < Parameters.Count; i++)
{
Parameters[i] = Parameter.Translate(Parameters[i], this.Category);
// Special cases: glLineStipple and gl(Get)ShaderSource:
// Check for LineStipple (should be unchecked)
if (Parameters[i].CurrentType == "UInt16" && Name.Contains("LineStipple"))
{
Parameters[i].WrapperType = WrapperTypes.UncheckedParameter;
}
if (Name.Contains("ShaderSource") && Parameters[i].CurrentType.ToLower().Contains("string"))
{
// Special case: these functions take a string[]
//IsPointer = true;
Parameters[i].Array = 1;
}
}
}
internal void Translate()
{
TranslateReturnType();
TranslateParameters();
List<Function> wrappers = (List<Function>)CreateWrappers();
// If the function is not CLS-compliant (e.g. it contains unsigned parameters)
// we need to create a CLS-Compliant overload. However, we should only do this
// iff the opengl function does not contain unsigned/signed overloads itself
// to avoid redefinitions.
foreach (Function f in wrappers)
{
2007-08-02 00:28:54 +02:00
Bind.Structures.Function.Wrappers.AddChecked(f);
if (this.Name.Contains("Bitmap"))
2007-08-01 23:14:39 +02:00
{
}
bool createCLS = !f.CLSCompliant;
2007-08-02 00:28:54 +02:00
/*string nameWithoutExtension = Utilities.StripGL2Extension(f.Name).TrimEnd('v');
2007-08-01 23:14:39 +02:00
createCLS &=
String.IsNullOrEmpty(f.TrimmedName) ||
2007-08-02 00:28:54 +02:00
nameWithoutExtension.Substring(nameWithoutExtension.Length - 3).Contains("u") &&
!nameWithoutExtension.Substring(nameWithoutExtension.Length - 3).Contains("b");*/
2007-08-01 23:14:39 +02:00
if (createCLS)
{
2007-08-02 00:28:54 +02:00
Function clsFunction = f.GetCLSCompliantFunction();
if (clsFunction != null)
Bind.Structures.Function.Wrappers.AddChecked(clsFunction);
2007-08-01 23:14:39 +02:00
}
}
}
2007-08-01 11:27:57 +02:00
}
2007-08-01 23:14:39 +02:00
#region class DelegateCollection : Dictionary<string, Delegate>
2007-08-01 11:27:57 +02:00
class DelegateCollection : Dictionary<string, Delegate>
{
public void Add(Delegate d)
{
if (!this.ContainsKey(d.Name))
{
this.Add(d.Name, d);
}
else
{
Trace.WriteLine(String.Format(
"Spec error: function {0} redefined, ignoring second definition.",
d.Name));
}
}
}
2007-08-01 23:14:39 +02:00
#endregion
2007-08-01 11:27:57 +02:00
}