Opentk/Source/Bind/Structures/Enum.cs

147 lines
4.3 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;
2007-08-01 23:14:39 +02:00
using System.IO;
2007-08-01 11:27:57 +02:00
namespace Bind.Structures
{
#region class Enum
public class Enum
{
2007-08-01 23:14:39 +02:00
internal static EnumCollection GLEnums;
internal static EnumCollection AuxEnums;
2007-08-01 23:14:39 +02:00
private static bool enumsLoaded;
internal static void Initialize(string enumFile, string enumextFile, string auxFile)
{
Initialize(enumFile, enumextFile);
using (System.IO.StreamReader sr = new System.IO.StreamReader(Path.Combine(Settings.InputPath, auxFile)))
{
AuxEnums = Bind.MainClass.Generator.ReadEnums(sr);
}
}
internal static void Initialize(string enumFile, string enumextFile)
2007-08-01 23:14:39 +02:00
{
if (!enumsLoaded)
{
if (!String.IsNullOrEmpty(enumFile))
2007-08-01 23:14:39 +02:00
{
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumFile))
{
GLEnums = Bind.MainClass.Generator.ReadEnums(sr);
}
2007-08-01 23:14:39 +02:00
}
if (!String.IsNullOrEmpty(enumextFile))
2007-08-01 23:14:39 +02:00
{
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumextFile))
2007-08-01 23:14:39 +02:00
{
foreach (Bind.Structures.Enum e in Bind.MainClass.Generator.ReadEnums(sr).Values)
{
//enums.Add(e.Name, e);
Utilities.Merge(GLEnums, e);
}
2007-08-01 23:14:39 +02:00
}
}
enumsLoaded = true;
}
}
2007-08-01 11:27:57 +02:00
public Enum()
{ }
public Enum(string name)
{
Name = name;
}
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
System.Collections.Hashtable _constant_collection = new System.Collections.Hashtable();
public System.Collections.Hashtable ConstantCollection
{
get { return _constant_collection; }
set { _constant_collection = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
2007-08-01 11:27:57 +02:00
sb.AppendLine("public enum " + Name);
sb.AppendLine("{");
foreach (Constant c in ConstantCollection.Values)
{
2007-08-01 11:27:57 +02:00
sb.Append(" ");
sb.Append(c.ToString());
sb.AppendLine(",");
}
sb.AppendLine("}");
return sb.ToString();
}
}
#endregion
#region class EnumCollection
class EnumCollection : Dictionary<string, Enum>
{
2007-08-01 23:14:39 +02:00
internal void AddRange(EnumCollection enums)
2007-08-01 11:27:57 +02:00
{
2007-08-01 23:14:39 +02:00
foreach (Enum e in enums.Values)
2007-08-01 11:27:57 +02:00
{
2007-08-01 23:14:39 +02:00
Utilities.Merge(this, e);
}
2007-08-01 23:14:39 +02:00
}
2007-08-01 23:14:39 +02:00
internal void Translate()
{
foreach (Enum e in this.Values)
{
foreach (Constant c in e.ConstantCollection.Values)
{
// There are cases when a value is an aliased constant, with no enum specified.
// (e.g. FOG_COORD_ARRAY_TYPE = GL_FOG_COORDINATE_ARRAY_TYPE)
// In this case try searching all enums for the correct constant to alias (stupid opengl specs).
if (String.IsNullOrEmpty(c.Reference) && !Char.IsDigit(c.Value[0]))
{
foreach (Enum @enum in this.Values)
{
// Skip generic GLenum
if (@enum.Name == "GLenum")
continue;
if (@enum.ConstantCollection.ContainsKey(c.Value))
{
c.Reference = @enum.Name;
}
}
}
}
}
}
}
#endregion
}