2009-02-22 10:43:35 +00:00
|
|
|
#region --- License ---
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
2009-08-21 20:28:14 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Globalization;
|
2009-10-14 22:49:04 +00:00
|
|
|
using System.Linq;
|
2009-02-22 10:43:35 +00:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace Bind.Structures
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an opengl constant in C# format. Both the constant name and value
|
|
|
|
/// can be retrieved or set. The value can be either a number, another constant
|
|
|
|
/// or an alias to a constant
|
|
|
|
/// </summary>
|
2013-11-01 08:18:53 +00:00
|
|
|
class Constant : IComparable<Constant>
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
static StringBuilder translator = new StringBuilder();
|
|
|
|
|
2009-08-11 14:08:18 +00:00
|
|
|
#region PreviousName
|
|
|
|
|
2011-07-20 10:10:33 +00:00
|
|
|
string original_name;
|
2009-08-11 14:08:18 +00:00
|
|
|
|
|
|
|
// Gets the name prior to translation.
|
2011-07-20 10:10:33 +00:00
|
|
|
public string OriginalName
|
2009-08-11 14:08:18 +00:00
|
|
|
{
|
2011-07-20 10:10:33 +00:00
|
|
|
get { return original_name; }
|
|
|
|
private set { original_name = value; }
|
2009-08-11 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#region public string Name
|
|
|
|
|
|
|
|
string _name;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name of the opengl constant (eg. GL_LINES).
|
|
|
|
/// Undergoes processing unless the Settings.Legacy.NoAdvancedEnumProcessing flag is set.
|
|
|
|
/// </summary>
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return _name; }
|
|
|
|
set
|
|
|
|
{
|
2010-10-12 16:46:08 +00:00
|
|
|
if (String.IsNullOrEmpty(value))
|
|
|
|
throw new ArgumentNullException("value");
|
2011-07-20 10:10:33 +00:00
|
|
|
|
|
|
|
if (OriginalName == null)
|
|
|
|
OriginalName = _name;
|
|
|
|
|
2010-10-12 16:46:08 +00:00
|
|
|
_name = value;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public string Value
|
|
|
|
|
|
|
|
string _value;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the value of the opengl constant (eg. 0x00000001).
|
|
|
|
/// </summary>
|
|
|
|
public string Value
|
|
|
|
{
|
2010-10-12 16:46:08 +00:00
|
|
|
get
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2010-10-12 16:46:08 +00:00
|
|
|
if (String.IsNullOrEmpty(value))
|
|
|
|
throw new ArgumentNullException("value");
|
2009-08-11 14:08:18 +00:00
|
|
|
|
2010-10-12 16:46:08 +00:00
|
|
|
_value = value;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public string Reference
|
|
|
|
|
|
|
|
string _reference;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a string indicating the OpenGL enum reference by this constant.
|
|
|
|
/// Can be null.
|
|
|
|
/// </summary>
|
|
|
|
public string Reference
|
|
|
|
{
|
|
|
|
get { return _reference; }
|
|
|
|
set
|
|
|
|
{
|
2013-10-26 23:30:45 +00:00
|
|
|
_reference = value;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public bool Unchecked
|
|
|
|
|
|
|
|
public bool Unchecked
|
|
|
|
{
|
2010-10-12 16:46:08 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
// Check if the value is a number larger than Int32.MaxValue.
|
|
|
|
ulong number;
|
|
|
|
string test = Value;
|
|
|
|
return UInt64.TryParse(test.ToLower().Replace("0x", String.Empty),
|
|
|
|
NumberStyles.AllowHexSpecifier, null, out number) &&
|
|
|
|
number > Int32.MaxValue;
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an empty Constant.
|
|
|
|
/// </summary>
|
|
|
|
public Constant()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a Constant with the given name and value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The Name of the Constant.</param>
|
|
|
|
/// <param name="value">The Type of the Constant.</param>
|
|
|
|
public Constant(string name, string value)
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
Value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Replces the Value of the given constant with the value referenced by the [c.Reference, c.Value] pair.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="c">The Constant to translate</param>
|
|
|
|
/// <param name="enums">The list of enums to check.</param>
|
|
|
|
/// <param name="auxEnums">The list of auxilliary enums to check.</param>
|
2009-05-29 15:57:01 +00:00
|
|
|
/// <returns>True if the reference was found; false otherwise.</returns>
|
2013-10-26 23:30:45 +00:00
|
|
|
public static bool TranslateConstantWithReference(Constant c, EnumCollection enums)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2010-10-12 16:46:08 +00:00
|
|
|
if (c == null)
|
|
|
|
throw new ArgumentNullException("c");
|
|
|
|
if (enums == null)
|
|
|
|
throw new ArgumentNullException("enums");
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
if (!String.IsNullOrEmpty(c.Reference))
|
|
|
|
{
|
2013-10-28 13:07:45 +00:00
|
|
|
// Resolve the referenced Constant. Be careful
|
|
|
|
// to avoid loops in the definitions.
|
|
|
|
Constant reference = c;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
reference =
|
|
|
|
enums.ContainsKey(reference.Reference) &&
|
|
|
|
enums[reference.Reference].ConstantCollection.ContainsKey(reference.Value) ?
|
|
|
|
enums[reference.Reference].ConstantCollection[reference.Value] : null;
|
|
|
|
} while (reference != null && reference.Reference != null && reference.Reference != c.Reference);
|
|
|
|
|
|
|
|
// If we haven't managed to locate the reference, do
|
|
|
|
// a brute-force search through all enums.
|
|
|
|
if (reference == null || reference.Reference != null)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2013-10-28 13:07:45 +00:00
|
|
|
reference = enums.Values.Select(e =>
|
|
|
|
e.ConstantCollection.Values.FirstOrDefault(t =>
|
|
|
|
t.Reference == null && t.Name == c.Name))
|
|
|
|
.FirstOrDefault(t => t != null);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2013-10-28 13:07:45 +00:00
|
|
|
|
|
|
|
// Resolve the value for this Constant
|
|
|
|
if (reference != null)
|
2010-11-19 11:12:02 +00:00
|
|
|
{
|
2013-10-28 13:07:45 +00:00
|
|
|
c.Value = reference.Value;
|
|
|
|
c.Reference = null;
|
|
|
|
return true;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-05-29 15:57:01 +00:00
|
|
|
else
|
|
|
|
{
|
2013-10-28 13:07:45 +00:00
|
|
|
Trace.WriteLine(String.Format("[Warning] Failed to resolve token: {0}", c));
|
2009-05-29 15:57:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-05-29 15:57:01 +00:00
|
|
|
return true;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 08:18:53 +00:00
|
|
|
#region ToString
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
2013-11-01 08:18:53 +00:00
|
|
|
return
|
|
|
|
String.Format("{0} = {1}((int){2}{3})",
|
|
|
|
Name,
|
|
|
|
Unchecked ? "unchecked" : String.Empty,
|
|
|
|
!String.IsNullOrEmpty(Reference) ? Reference + "." : String.Empty,
|
|
|
|
Value);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2011-07-20 10:10:33 +00:00
|
|
|
|
|
|
|
#region IComparable <Constant>Members
|
|
|
|
|
|
|
|
public int CompareTo(Constant other)
|
|
|
|
{
|
|
|
|
int ret = Value.CompareTo(other.Value);
|
|
|
|
if (ret == 0)
|
|
|
|
return Name.CompareTo(other.Name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|