Browse Source

Improve RegistrableGeneratorOptions + prepare boilerplate code

pull/1808/head
Deadlocklogic 2 years ago
parent
commit
11b37ba40f
  1. 8
      src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerator.cs
  2. 2
      src/Generator/Generators/Registrable/Lua/Sol/LuaSolGeneratorOptions.cs
  3. 44
      src/Generator/Generators/Registrable/RegistrableGenerator.cs
  4. 17
      src/Generator/Generators/Registrable/RegistrableGeneratorOptions.cs
  5. 19
      src/Generator/Generators/Registrable/RegistrableModuleHeader.cs
  6. 19
      src/Generator/Generators/Registrable/RegistrableModuleSource.cs

8
src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerator.cs

@ -12,21 +12,19 @@ namespace CppSharp.Generators.Registrable.Lua.Sol @@ -12,21 +12,19 @@ namespace CppSharp.Generators.Registrable.Lua.Sol
{
}
protected override LuaSolGeneratorOptions CreateOptions(RegistrableGenerator<LuaSolGeneratorOptions, LuaSolHeaders, LuaSolSources> generator)
protected override LuaSolGeneratorOptions CreateOptions()
{
return new LuaSolGeneratorOptions(this);
}
protected override LuaSolHeaders CreateHeader(RegistrableGenerator<LuaSolGeneratorOptions, LuaSolHeaders, LuaSolSources> generator, IEnumerable<TranslationUnit> units)
protected override LuaSolHeaders CreateHeader(IEnumerable<TranslationUnit> units)
{
return new LuaSolHeaders(this, units);
}
protected override LuaSolSources CreateSource(RegistrableGenerator<LuaSolGeneratorOptions, LuaSolHeaders, LuaSolSources> generator, IEnumerable<TranslationUnit> units)
protected override LuaSolSources CreateSource(IEnumerable<TranslationUnit> units)
{
return new LuaSolSources(this, units);
}
public override bool SetupPasses() => true;
}
}

2
src/Generator/Generators/Registrable/Lua/Sol/LuaSolGeneratorOptions.cs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
namespace CppSharp.Generators.Registrable.Lua.Sol
{
public class LuaSolGeneratorOptions : RegistrableGeneratorOptions<LuaSolGenerator>
public class LuaSolGeneratorOptions : TRegistrableGeneratorOptions<LuaSolGenerator>
{
public LuaSolNamingStrategy NamingStrategy;

44
src/Generator/Generators/Registrable/RegistrableGenerator.cs

@ -1,34 +1,64 @@ @@ -1,34 +1,64 @@
using CppSharp.AST;
using System.Collections.Generic;
using System.IO;
namespace CppSharp.Generators.Registrable
{
public abstract class RegistrableGenerator<TOptions, THeader, TSource> : Generator
where TOptions : RegistrableGeneratorOptions
where THeader : CodeGenerator
where TSource : CodeGenerator
{
public TOptions GeneratorOptions { get; }
public TranslationUnit GlobalTranslationUnit { get; private set; }
public TranslationUnit InheritanceTranslationUnit { get; private set; }
// TODO: Implement when Generator interface is cleaner
// public CodeGenerator ModuleHeaderCodeGenerator { get; private set; }
// TODO: Implement when Generator interface is cleaner
//public CodeGenerator ModuleSourceCodeGenerator { get; private set; }
public RegistrableGenerator(BindingContext context) : base(context)
{
GeneratorOptions = CreateOptions(this);
GeneratorOptions = CreateOptions();
}
protected abstract TOptions CreateOptions(RegistrableGenerator<TOptions, THeader, TSource> generator);
protected abstract TOptions CreateOptions();
protected abstract THeader CreateHeader(RegistrableGenerator<TOptions, THeader, TSource> generator, IEnumerable<TranslationUnit> units);
protected abstract THeader CreateHeader(IEnumerable<TranslationUnit> units);
protected abstract TSource CreateSource(RegistrableGenerator<TOptions, THeader, TSource> generator, IEnumerable<TranslationUnit> units);
protected abstract TSource CreateSource(IEnumerable<TranslationUnit> units);
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
{
return new List<CodeGenerator>
{
CreateHeader(this, units),
CreateSource(this, units)
CreateHeader(units),
CreateSource(units)
};
}
public override bool SetupPasses() => true;
// TODO: Should be a better method for this maybe Configure.
public override bool SetupPasses()
{
{
var module = Context.Options.Modules[1];
GlobalTranslationUnit = Context.ASTContext.FindOrCreateTranslationUnit(
Path.Combine(Context.Options.OutputDir, GeneratorOptions.OutputSubDir, "@package", "global.h")
);
GlobalTranslationUnit.Module = module;
}
{
var module = Context.Options.Modules[1];
InheritanceTranslationUnit = Context.ASTContext.FindOrCreateTranslationUnit(
Path.Combine(Context.Options.OutputDir, GeneratorOptions.OutputSubDir, "@package", "inheritance.h")
);
InheritanceTranslationUnit.Module = module;
}
return true;
}
}
}

17
src/Generator/Generators/Registrable/RegistrableGeneratorOptions.cs

@ -9,12 +9,11 @@ namespace CppSharp.Generators.Registrable @@ -9,12 +9,11 @@ namespace CppSharp.Generators.Registrable
Import
}
public abstract class RegistrableGeneratorOptions<TGenerator>
where TGenerator : Generator
public abstract class RegistrableGeneratorOptions
{
public delegate string Delegate(string name);
public TGenerator Generator { get; set; }
public virtual Generator Generator { get; set; }
public virtual string OutputSubDir { get; }
public virtual string RootContextType { get; }
@ -60,7 +59,7 @@ namespace CppSharp.Generators.Registrable @@ -60,7 +59,7 @@ namespace CppSharp.Generators.Registrable
public virtual string DefaultStaticCastFunctionTemplateFullyQualifiedName => null;
public virtual string DefaultDynamicCastFunctionTemplateFullyQualifiedName => null;
public RegistrableGeneratorOptions(TGenerator generator)
public RegistrableGeneratorOptions(Generator generator)
{
Generator = generator;
OutputSubDir = DefaultOutputSubdir;
@ -86,4 +85,14 @@ namespace CppSharp.Generators.Registrable @@ -86,4 +85,14 @@ namespace CppSharp.Generators.Registrable
DynamicCastFunctionTemplateFullyQualifiedName = DefaultDynamicCastFunctionTemplateFullyQualifiedName;
}
}
public abstract class TRegistrableGeneratorOptions<TGenerator> : RegistrableGeneratorOptions
where TGenerator : Generator
{
public override TGenerator Generator => (TGenerator)base.Generator;
public TRegistrableGeneratorOptions(TGenerator generator) : base(generator)
{
}
}
}

19
src/Generator/Generators/Registrable/RegistrableModuleHeader.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
using CppSharp.AST;
using System.Collections.Generic;
namespace CppSharp.Generators.Registrable
{
public class RegistrableModuleHeader<TGenerator> : RegistrableCodeGenerator<TGenerator>
where TGenerator : Generator
{
public RegistrableModuleHeader(TGenerator generator, IEnumerable<TranslationUnit> units) : base(generator, units)
{
}
public override string FileExtension { get; } = "h";
public override void Process()
{
}
}
}

19
src/Generator/Generators/Registrable/RegistrableModuleSource.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
using CppSharp.AST;
using System.Collections.Generic;
namespace CppSharp.Generators.Registrable
{
public class RegistrableModuleSource<TGenerator> : RegistrableCodeGenerator<TGenerator>
where TGenerator : Generator
{
public RegistrableModuleSource(TGenerator generator, IEnumerable<TranslationUnit> units) : base(generator, units)
{
}
public override string FileExtension { get; } = "cpp";
public override void Process()
{
}
}
}
Loading…
Cancel
Save