Use namespaces instead of structs, since C++ does not support partial structures (which are necessary for the core/compatibility split).

This commit is contained in:
the_fiddler 2010-12-06 22:32:47 +00:00
parent fed983f51f
commit 9664a2a57e

View file

@ -44,6 +44,16 @@ namespace Bind
readonly char[] numbers = "0123456789".ToCharArray(); readonly char[] numbers = "0123456789".ToCharArray();
const string AllowDeprecated = "ALLOW_DEPRECATED_GL"; const string AllowDeprecated = "ALLOW_DEPRECATED_GL";
const string DigitPrefix = "T"; // Prefix for identifiers that start with a digit const string DigitPrefix = "T"; // Prefix for identifiers that start with a digit
const string OutputFileCpp = "glcore++.cpp";
const string OutputFileHeader = "glcore++.h";
const string OutputFileHeaderCompat = "glcompat++.h";
const string OutputFileHeaderEnums = "glenums++.h";
BindStreamWriter sw_h = new BindStreamWriter(Path.GetTempFileName());
BindStreamWriter sw_cpp = new BindStreamWriter(Path.GetTempFileName());
BindStreamWriter sw_h_compat = new BindStreamWriter(Path.GetTempFileName());
BindStreamWriter sw_cpp_compat = new BindStreamWriter(Path.GetTempFileName());
BindStreamWriter sw_h_enums = new BindStreamWriter(Path.GetTempFileName());
#region WriteBindings #region WriteBindings
@ -52,7 +62,7 @@ namespace Bind
WriteBindings(generator.Delegates, generator.Wrappers, generator.Enums); WriteBindings(generator.Delegates, generator.Wrappers, generator.Enums);
} }
void WriteBindings(DelegateCollection delegates, FunctionCollection wrappers, EnumCollection enums) void WriteBindings(DelegateCollection delegates, FunctionCollection wrappers, EnumCollection enums)
{ {
Console.WriteLine("Writing bindings to {0}", Settings.OutputPath); Console.WriteLine("Writing bindings to {0}", Settings.OutputPath);
if (!Directory.Exists(Settings.OutputPath)) if (!Directory.Exists(Settings.OutputPath))
@ -68,102 +78,183 @@ namespace Bind
Settings.DefaultOutputNamespace = "OpenTK"; Settings.DefaultOutputNamespace = "OpenTK";
string temp_header_file = Path.GetTempFileName(); // Enums
string temp_cpp_file = Path.GetTempFileName(); using (var sw = sw_h_enums)
using (BindStreamWriter sw = new BindStreamWriter(temp_header_file))
{ {
sw.WriteLine("#ifndef GLDEFPP_H");
sw.WriteLine("#define GLDEFPP_H");
sw.WriteLine("#pragma once");
WriteLicense(sw);
sw.WriteLine("namespace {0}", Settings.OutputNamespace);
sw.WriteLine("{");
sw.Indent();
WriteEnums(sw, enums); WriteEnums(sw, enums);
sw.Flush();
sw.WriteLine("struct {0}", Settings.GLClass); sw.Close();
sw.WriteLine("{");
sw.Indent();
WriteWrappers(sw, wrappers, Type.CSTypes);
sw.Unindent();
sw.WriteLine("};");
sw.Unindent();
sw.WriteLine("}");
sw.WriteLine("#endif");
} }
using (BindStreamWriter sw = new BindStreamWriter(temp_cpp_file)) // Core definitions
using (var sw = sw_h)
{ {
WriteLicense(sw); WriteDefinitions(sw, enums, wrappers, Type.CSTypes, false);
sw.Flush();
sw.WriteLine("namespace {0}", Settings.OutputNamespace); sw.Close();
sw.WriteLine("{");
sw.Indent();
WriteLoader(sw, wrappers, Type.CSTypes);
sw.Unindent();
sw.WriteLine("}");
} }
string output_header = Path.Combine(Settings.OutputPath, "gldef++.h"); // Compatibility definitions
string output_cpp = Path.Combine(Settings.OutputPath, "gldef++.cpp"); using (var sw = sw_h_compat)
if (File.Exists(output_header)) {
File.Delete(output_header); WriteDefinitions(sw, enums, wrappers, Type.CSTypes, true);
File.Move(temp_header_file, output_header); sw.Flush();
if (File.Exists(output_cpp)) sw.Close();
File.Delete(output_cpp); }
File.Move(temp_cpp_file, output_cpp);
// Core & compatibility declarations
using (var sw = sw_cpp)
{
WriteDeclarations(sw, wrappers, Type.CSTypes);
sw.Flush();
sw.Close();
}
string output_header = Path.Combine(Settings.OutputPath, OutputFileHeader);
string output_cpp = Path.Combine(Settings.OutputPath, OutputFileCpp);
string output_header_compat = Path.Combine(Settings.OutputPath, OutputFileHeaderCompat);
string output_header_enums = Path.Combine(Settings.OutputPath, OutputFileHeaderEnums);
Move(sw_h.File, output_header);
Move(sw_cpp.File, output_cpp);
Move(sw_h_compat.File, output_header_compat);
Move(sw_h_enums.File, output_header_enums);
}
void Move(string file, string dest)
{
if (File.Exists(dest))
File.Delete(dest);
File.Move(file, dest);
} }
#endregion #endregion
#region WriteLoader #region WriteDefinitions
void WriteLoader(BindStreamWriter sw, FunctionCollection wrappers, void WriteDefinitions(BindStreamWriter sw,
Dictionary<string, string> CSTypes) EnumCollection enums, FunctionCollection wrappers,
Dictionary<string, string> CSTypes, bool deprecated_only)
{ {
sw.WriteLine("#pragma once");
if (!deprecated_only)
{
sw.WriteLine("#ifndef GLCOREPP_H");
sw.WriteLine("#define GLCOREPP_H");
}
else
{
sw.WriteLine("#ifndef GLCOMPATPP_H");
sw.WriteLine("#define GLCOMPATPP_H");
}
sw.WriteLine();
WriteLicense(sw);
sw.WriteLine("namespace {0}", Settings.OutputNamespace);
sw.WriteLine("{");
sw.Indent();
sw.WriteLine("namespace {0}", Settings.GLClass);
sw.WriteLine("{");
sw.Indent();
foreach (string extension in wrappers.Keys)
{
if (extension != "Core")
{
sw.WriteLine("namespace {0}", extension);
sw.WriteLine("{");
sw.Indent();
}
// Avoid multiple definitions of the same function
Delegate last_delegate = null;
// Write delegates
sw.WriteLine("namespace Delegates");
sw.WriteLine("{");
sw.Indent();
var functions = wrappers[extension].Where(f => f.Deprecated == deprecated_only);
last_delegate = null;
foreach (var f in functions)
{
WriteDelegate(sw, f.WrappedDelegate, ref last_delegate);
}
sw.Unindent();
sw.WriteLine("};");
// Write wrappers
sw.WriteLine("void Init();");
last_delegate = null;
foreach (var f in functions)
{
if (last_delegate == f.WrappedDelegate)
continue;
last_delegate = f.WrappedDelegate;
var parameters = f.WrappedDelegate.Parameters.ToString()
.Replace("String[]", "String*")
.Replace("[OutAttribute]", String.Empty);
sw.WriteLine("inline {0} {1}{2}", f.WrappedDelegate.ReturnType,
f.TrimmedName, parameters);
sw.WriteLine("{");
sw.Indent();
WriteMethodBody(sw, f);
sw.Unindent();
sw.WriteLine("}");
}
if (extension != "Core")
{
sw.Unindent();
sw.WriteLine("};");
}
}
sw.Unindent();
sw.WriteLine("};");
sw.Unindent();
sw.WriteLine("}");
sw.WriteLine("#endif");
}
#endregion
#region WriteDeclarations
void WriteDeclarations(BindStreamWriter sw, FunctionCollection wrappers,
Dictionary<string, string> CSTypes)
{
sw.WriteLine("#ifdef GLPP_INTERNAL_COMPILE_DECLARATIONS");
WriteLicense(sw);
sw.WriteLine("namespace {0}", Settings.OutputNamespace);
sw.WriteLine("{");
sw.Indent();
sw.WriteLine("using namespace Internals;"); sw.WriteLine("using namespace Internals;");
// Used to avoid multiple declarations of the same function // Used to avoid multiple declarations of the same function
Delegate last_delegate = null; Delegate last_delegate = null;
// Declare all functions // Declare all functions (deprecated and core).
// This is necessary for projects that wish to move from
// deprecated APIs to core piece-by-piece.
foreach (var ext in wrappers.Keys) foreach (var ext in wrappers.Keys)
{ {
var forward_compatible = wrappers[ext].Where(f => !f.Deprecated);
var deprecated = wrappers[ext].Where(f => f.Deprecated);
last_delegate = null; last_delegate = null;
foreach (var current in new IEnumerable<Function>[] { forward_compatible, deprecated }) var functions = wrappers[ext];
foreach (var function in functions)
{ {
if (current == deprecated) if (function.WrappedDelegate == last_delegate)
{ continue;
sw.WriteLine("#ifdef {0}", AllowDeprecated); last_delegate = function.WrappedDelegate;
sw.Indent();
}
foreach (var function in current) string path = GetNamespace(ext);
{ sw.WriteLine("{0}::Delegates::p{1} {0}::Delegates::{1} = 0;", path, function.Name);
if (function.WrappedDelegate == last_delegate)
continue;
last_delegate = function.WrappedDelegate;
string path = GetNamespace(ext);
sw.WriteLine("{0}::Delegates::p{1} {0}::Delegates::{1} = 0;", path, function.Name);
}
if (current == deprecated)
{
sw.Unindent();
sw.WriteLine("#endif");
}
} }
} }
sw.WriteLine(); sw.WriteLine();
@ -177,37 +268,25 @@ namespace Bind
sw.WriteLine("{"); sw.WriteLine("{");
sw.Indent(); sw.Indent();
var forward_compatible = wrappers[ext].Where(f => !f.Deprecated); last_delegate = null;
var deprecated = wrappers[ext].Where(f => f.Deprecated); var functions = wrappers[ext];
foreach (var function in functions)
foreach (var current in new IEnumerable<Function>[] { forward_compatible, deprecated })
{ {
if (current == deprecated) if (function.WrappedDelegate == last_delegate)
{ continue;
sw.WriteLine("#ifdef {0}", AllowDeprecated); last_delegate = function.WrappedDelegate;
sw.Indent();
}
last_delegate = null; sw.WriteLine("{0}::Delegates::{1} = ({0}::Delegates::p{1})GetAddress(\"gl{1}\");",
foreach (var function in wrappers[ext]) path, function.WrappedDelegate.Name);
{
if (function.WrappedDelegate == last_delegate)
continue;
last_delegate = function.WrappedDelegate;
sw.WriteLine("{0}::Delegates::{1} = ({0}::Delegates::p{1})GetAddress(\"gl{1}\");",
path, function.WrappedDelegate.Name);
}
if (current == deprecated)
{
sw.Unindent();
sw.WriteLine("#endif");
}
} }
sw.Unindent(); sw.Unindent();
sw.WriteLine("}"); sw.WriteLine("}");
} }
sw.Unindent();
sw.WriteLine("}");
sw.WriteLine("#endif");
} }
static string GetNamespace(string ext) static string GetNamespace(string ext)
@ -220,6 +299,67 @@ namespace Bind
#endregion #endregion
#region ISpecWriter Members
#region WriteEnums
public void WriteEnums(BindStreamWriter sw, EnumCollection enums)
{
sw.WriteLine("#pragma once");
sw.WriteLine("#ifndef GLENUMSPP_H");
sw.WriteLine("#define GLENUMSPP_H");
sw.WriteLine();
WriteLicense(sw);
sw.WriteLine("namespace {0}", Settings.OutputNamespace);
sw.WriteLine("{");
sw.Indent();
foreach (Enum @enum in enums.Values)
{
sw.WriteLine("struct {0} : Enumeration<{0}>", @enum.Name);
sw.WriteLine("{");
sw.Indent();
sw.WriteLine("inline {0}(int value) : Enumeration<{0}>(value) {{ }}", @enum.Name);
sw.WriteLine("enum");
sw.WriteLine("{");
sw.Indent();
foreach (var c in @enum.ConstantCollection.Values)
{
// C++ doesn't have the concept of "unchecked", so remove this.
if (!c.Unchecked)
sw.WriteLine("{0},", c);
else
sw.WriteLine("{0},", c.ToString().Replace("unchecked", String.Empty));
}
sw.Unindent();
sw.WriteLine("};");
sw.Unindent();
sw.WriteLine("};");
sw.WriteLine();
}
sw.Unindent();
sw.WriteLine("}");
sw.WriteLine("#endif");
}
#endregion
#region WriteTypes
public void WriteTypes(BindStreamWriter sw, Dictionary<string, string> CSTypes)
{
sw.WriteLine();
foreach (string s in CSTypes.Keys)
{
sw.WriteLine("typedef {0} {1};", s, CSTypes[s]);
}
}
#endregion
#region WriteDelegates #region WriteDelegates
public void WriteDelegates(BindStreamWriter sw, DelegateCollection delegates) public void WriteDelegates(BindStreamWriter sw, DelegateCollection delegates)
@ -237,7 +377,7 @@ namespace Bind
.Replace("String[]", "String*") .Replace("String[]", "String*")
.Replace("[OutAttribute]", String.Empty); .Replace("[OutAttribute]", String.Empty);
sw.WriteLine("typedef {0} (*p{1}){2};", d.ReturnType, d.Name, parameters); sw.WriteLine("typedef {0} (*p{1}){2};", d.ReturnType, d.Name, parameters);
sw.WriteLine("static p{0} {0};", d.Name); sw.WriteLine("extern p{0} {0};", d.Name);
} }
} }
@ -257,98 +397,12 @@ namespace Bind
public void WriteWrappers(BindStreamWriter sw, FunctionCollection wrappers, public void WriteWrappers(BindStreamWriter sw, FunctionCollection wrappers,
Dictionary<string, string> CSTypes) Dictionary<string, string> CSTypes)
{ {
foreach (string extension in wrappers.Keys) throw new NotSupportedException();
{
if (extension != "Core")
{
sw.WriteLine("struct {0}", extension);
sw.WriteLine("{");
sw.Indent();
}
// Avoid multiple definitions of the same function
Delegate last_delegate = null;
// Write forward-compatible functions
var forward_compatible = wrappers[extension].Where(f => !f.Deprecated);
var deprecated = wrappers[extension].Where(f => f.Deprecated);
// Write delegates
sw.WriteLine("private:");
sw.WriteLine("struct Delegates");
sw.WriteLine("{");
sw.Indent();
foreach (var current in new IEnumerable<Function>[] { forward_compatible, deprecated })
{
if (current == deprecated)
{
sw.WriteLine("#ifdef {0}", AllowDeprecated);
sw.Indent();
}
last_delegate = null;
foreach (var f in current)
{
WriteDelegate(sw, f.WrappedDelegate, ref last_delegate);
}
if (current == deprecated)
{
sw.Unindent();
sw.WriteLine("#endif");
}
}
sw.Unindent();
sw.WriteLine("};");
// Write wrappers
sw.WriteLine("public:");
sw.WriteLine("static void Init();");
foreach (var current in new IEnumerable<Function>[] { forward_compatible, deprecated })
{
if (current == deprecated)
{
sw.WriteLine("#ifdef {0}", AllowDeprecated);
sw.Indent();
}
last_delegate = null;
foreach (var f in current)
{
if (last_delegate == f.WrappedDelegate)
continue;
last_delegate = f.WrappedDelegate;
var parameters = f.WrappedDelegate.Parameters.ToString()
.Replace("String[]", "String*")
.Replace("[OutAttribute]", String.Empty);
sw.WriteLine("static inline {0} {1}{2}", f.WrappedDelegate.ReturnType,
f.TrimmedName, parameters);
sw.WriteLine("{");
sw.Indent();
WriteMethodBody(sw, f);
sw.Unindent();
sw.WriteLine("}");
}
if (current == deprecated)
{
sw.Unindent();
sw.WriteLine("#endif");
}
}
if (extension != "Core")
{
sw.Unindent();
sw.WriteLine("};");
}
}
} }
static void WriteMethodBody(BindStreamWriter sw, Function f) static void WriteMethodBody(BindStreamWriter sw, Function f)
{ {
var callstring = f.Parameters.CallString() var callstring = f.Parameters.CallString()
.Replace("String[]", "String*"); .Replace("String[]", "String*");
if (f.ReturnType != null && !f.ReturnType.ToString().ToLower().Contains("void")) if (f.ReturnType != null && !f.ReturnType.ToString().ToLower().Contains("void"))
sw.WriteLine("return Delegates::{0}{1};", f.WrappedDelegate.Name, callstring); sw.WriteLine("return Delegates::{0}{1};", f.WrappedDelegate.Name, callstring);
@ -421,48 +475,6 @@ namespace Bind
#endregion #endregion
#region WriteTypes
public void WriteTypes(BindStreamWriter sw, Dictionary<string, string> CSTypes)
{
sw.WriteLine();
foreach (string s in CSTypes.Keys)
{
sw.WriteLine("typedef {0} {1};", s, CSTypes[s]);
}
}
#endregion
#region WriteEnums
public void WriteEnums(BindStreamWriter sw, EnumCollection enums)
{
foreach (Enum @enum in enums.Values)
{
sw.WriteLine("struct {0} : Enumeration<{0}>", @enum.Name);
sw.WriteLine("{");
sw.Indent();
sw.WriteLine("inline {0}(int value) : Enumeration<{0}>(value) {{ }}", @enum.Name);
sw.WriteLine("enum");
sw.WriteLine("{");
sw.Indent();
foreach (var c in @enum.ConstantCollection.Values)
{
// C++ doesn't have the concept of "unchecked", so remove this.
if (!c.Unchecked)
sw.WriteLine("{0},", c);
else
sw.WriteLine("{0},", c.ToString().Replace("unchecked", String.Empty));
}
sw.Unindent();
sw.WriteLine("};");
sw.Unindent();
sw.WriteLine("};");
sw.WriteLine();
}
}
#endregion #endregion
#region WriteLicense #region WriteLicense