@@ -519,6 +519,7 @@ public virtual void GenerateClassDeclDeclarationList(Class @class, DetachmentOpt
519
519
{
520
520
if ( detachment == DetachmentOption . Off )
521
521
{
522
+ GenerateConstructors ( @class , @class . Constructors ) ;
522
523
GenerateClassDeclFunctions ( @class ) ;
523
524
GenerateClassDeclVariables ( @class ) ;
524
525
}
@@ -834,11 +835,79 @@ public override bool VisitVariableDecl(Variable variable)
834
835
835
836
#endregion
836
837
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
+
837
909
public virtual bool CanGenerateConstructor ( Method method )
838
910
{
839
- // if not self:isNonTemplateAllowed(context) then
840
- // return true
841
- // end
842
911
if ( AlreadyVisited ( method ) )
843
912
{
844
913
return false ;
@@ -847,20 +916,48 @@ public virtual bool CanGenerateConstructor(Method method)
847
916
{
848
917
return false ;
849
918
}
919
+ else if ( ! NonTemplateAllowed )
920
+ {
921
+ return false ;
922
+ }
850
923
return method . IsGenerated ;
851
924
}
852
925
853
- public virtual void GenerateConstructors ( Class @class , IEnumerable < Method > constructors )
926
+ public virtual void GenerateConstructor ( Class @class , Method constructor , bool doExpand )
854
927
{
855
- var isDetach = GenerationContext . PeekIsDetach ( ) ;
856
-
857
- if ( isDetach == DetachmentOption . Forced )
928
+ if ( doExpand )
858
929
{
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 )
861
950
{
951
+ if ( needsComma )
952
+ {
953
+ Write ( ", " ) ;
954
+ }
955
+ Write ( "..." ) ;
862
956
}
957
+ Write ( ")" ) ;
863
958
}
864
959
}
960
+
961
+ #endregion
865
962
}
866
963
}
0 commit comments