Tools and libraries to glue C/C++ APIs to high-level languages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

129 lines
3.9 KiB

using System;
using Cxxi.Types;
namespace Cxxi.Generators.CLI
{
public abstract class CLITextTemplate : TextTemplate
{
protected const string DefaultIndent = " ";
protected const uint MaxIndent = 80;
public ITypePrinter TypePrinter { get; set; }
protected CLITextTemplate(Driver driver, TranslationUnit unit)
: base(driver, unit)
{
TypePrinter = new CLITypePrinter(driver.TypeDatabase, driver.Library);
}
public static string SafeIdentifier(string proposedName)
{
return proposedName;
}
public string QualifiedIdentifier(Declaration decl)
{
return string.Format("{0}::{1}", Library.Name, decl.Name);
}
public void GenerateStart()
{
if (Transform == null)
{
WriteLine("//----------------------------------------------------------------------------");
WriteLine("// This is autogenerated code by cxxi-generator.");
WriteLine("// Do not edit this file or all your changes will be lost after re-generation.");
WriteLine("//----------------------------------------------------------------------------");
if (FileExtension == "cpp")
WriteLine(@"#include ""../interop.h"" // marshalString");
}
else
{
Transform.GenerateStart(this);
}
}
public void GenerateAfterNamespaces()
{
if (Transform != null)
Transform.GenerateAfterNamespaces(this);
}
public void GenerateSummary(string comment)
{
if (string.IsNullOrWhiteSpace(comment))
return;
// Wrap the comment to the line width.
var maxSize = (int)(MaxIndent - CurrentIndent.Count - "/// ".Length);
var lines = StringHelpers.WordWrapLines(comment, maxSize);
WriteLine("/// <summary>");
foreach (string line in lines)
WriteLine(string.Format("/// {0}", line.TrimEnd()));
WriteLine("/// </summary>");
}
public void GenerateInlineSummary(string comment)
{
if (String.IsNullOrWhiteSpace(comment))
return;
WriteLine("/// <summary> {0} </summary>", comment);
}
public void GenerateMethodParameters(Method method)
{
for (var i = 0; i < method.Parameters.Count; ++i)
{
if (method.Conversion == MethodConversionKind.FunctionToInstanceMethod
&& i == 0)
continue;
var param = method.Parameters[i];
Write("{0}", TypePrinter.GetArgumentString(param));
if (i < method.Parameters.Count - 1)
Write(", ");
}
}
public static bool CheckIgnoreMethod(Class @class, Method method)
{
if (method.Ignore) return true;
if (@class.IsAbstract && method.IsConstructor)
return true;
if (@class.IsValueType && method.IsDefaultConstructor)
return true;
if (method.IsCopyConstructor || method.IsMoveConstructor)
return true;
if (method.IsDestructor)
return true;
if (method.OperatorKind == CXXOperatorKind.Equal)
return true;
if (method.Kind == CXXMethodKind.Conversion)
return true;
if (method.Access != AccessSpecifier.Public)
return true;
return false;
}
public static bool CheckIgnoreField(Class @class, Field field)
{
if (field.Ignore) return true;
return false;
}
public abstract override string FileExtension { get; }
public abstract override void Generate();
}
}