Skip to content

Commit 736531b

Browse files
committed
Registrable: add support for constructor
1 parent b653daf commit 736531b

File tree

1 file changed

+106
-9
lines changed

1 file changed

+106
-9
lines changed

src/Generator/Generators/Registrable/Lua/Sol/LuaSolSources.cs

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ public virtual void GenerateClassDeclDeclarationList(Class @class, DetachmentOpt
519519
{
520520
if (detachment == DetachmentOption.Off)
521521
{
522+
GenerateConstructors(@class, @class.Constructors);
522523
GenerateClassDeclFunctions(@class);
523524
GenerateClassDeclVariables(@class);
524525
}
@@ -834,11 +835,79 @@ public override bool VisitVariableDecl(Variable variable)
834835

835836
#endregion
836837

838+
#region Constructor
839+
840+
public virtual bool NeedExpansionForConstructors(Class @class, IEnumerable<Method> constructors)
841+
{
842+
return false;
843+
}
844+
845+
public virtual void GenerateConstructors(Class @class, IEnumerable<Method> constructors)
846+
{
847+
var isDetach = GenerationContext.PeekIsDetach();
848+
849+
List<Method> filteredConstructors = constructors.Where((method) => CanGenerateConstructor(method)).ToList();
850+
if (filteredConstructors.Any())
851+
{
852+
Method constructor = filteredConstructors.First();
853+
string constructorBindingContext = NamingStrategy.GetBindingContext(constructor, GenerationContext);
854+
string constructorContextualName = NamingStrategy.GetContextualName(constructor, GenerationContext, FQNOption.IgnoreNone);
855+
856+
if (isDetach == DetachmentOption.Forced || isDetach == Utils.FindDetachmentOption(constructor))
857+
{
858+
859+
if (isDetach != DetachmentOption.Off)
860+
{
861+
Write($"{constructorBindingContext}[\"new\"] = ");
862+
}
863+
else
864+
{
865+
WriteLine(",");
866+
Write($"\"new\", ");
867+
}
868+
if (NeedExpansionForConstructors(@class, constructors))
869+
{
870+
Write("::sol::factories(");
871+
Indent();
872+
for (int i = 0; i < filteredConstructors.Count; i++)
873+
{
874+
if (i > 0)
875+
{
876+
Write(",");
877+
}
878+
NewLine();
879+
GenerateConstructor(@class, filteredConstructors[i], true);
880+
}
881+
Unindent();
882+
WriteLine(")");
883+
}
884+
else
885+
{
886+
Write("::sol::constructors<");
887+
Indent();
888+
for (int i = 0; i < filteredConstructors.Count; i++)
889+
{
890+
if (i > 0)
891+
{
892+
Write(",");
893+
}
894+
NewLine();
895+
GenerateConstructor(@class, filteredConstructors[i], false);
896+
}
897+
Unindent();
898+
NewLine();
899+
Write(">()");
900+
}
901+
if (isDetach != DetachmentOption.Off)
902+
{
903+
WriteLine(";");
904+
}
905+
}
906+
}
907+
}
908+
837909
public virtual bool CanGenerateConstructor(Method method)
838910
{
839-
// if not self:isNonTemplateAllowed(context) then
840-
// return true
841-
// end
842911
if (AlreadyVisited(method))
843912
{
844913
return false;
@@ -847,20 +916,48 @@ public virtual bool CanGenerateConstructor(Method method)
847916
{
848917
return false;
849918
}
919+
else if (!NonTemplateAllowed)
920+
{
921+
return false;
922+
}
850923
return method.IsGenerated;
851924
}
852925

853-
public virtual void GenerateConstructors(Class @class, IEnumerable<Method> constructors)
926+
public virtual void GenerateConstructor(Class @class, Method constructor, bool doExpand)
854927
{
855-
var isDetach = GenerationContext.PeekIsDetach();
856-
857-
if (isDetach == DetachmentOption.Forced)
928+
if (doExpand)
858929
{
859-
var filteredConstructors = constructors.Where((method) => CanGenerateConstructor(method));
860-
foreach (var constructor in constructors)
930+
// TODO: Implement when ready
931+
}
932+
else
933+
{
934+
Write(NamingStrategy.GetCppContext(constructor, GenerationContext, FQNOption.IgnoreNone));
935+
Write("(");
936+
var needsComma = false;
937+
foreach (var parameter in constructor.Parameters)
938+
{
939+
if (needsComma)
940+
{
941+
Write(", ");
942+
}
943+
else
944+
{
945+
needsComma = true;
946+
}
947+
Write(parameter.Type.Visit(new CppTypePrinter(Context)));
948+
}
949+
if (constructor.IsVariadic)
861950
{
951+
if (needsComma)
952+
{
953+
Write(", ");
954+
}
955+
Write("...");
862956
}
957+
Write(")");
863958
}
864959
}
960+
961+
#endregion
865962
}
866963
}

0 commit comments

Comments
 (0)