From e6b80a40d2f28541bde38e6a079f827bd2f6ac6b Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 26 Oct 2018 18:50:03 +0300 Subject: [PATCH] Use variadic templates to get rid of duplicate code in the class binding utility. --- include/sqrat/sqratClass.h | 352 +------------------------------------ 1 file changed, 8 insertions(+), 344 deletions(-) diff --git a/include/sqrat/sqratClass.h b/include/sqrat/sqratClass.h index 53327300..4525f519 100644 --- a/include/sqrat/sqratClass.h +++ b/include/sqrat/sqratClass.h @@ -768,174 +768,18 @@ public: } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 1 argument (there can only be one constructor of this many arguments for a given name) + /// Binds a constructor with 1 or more arguments (there can only be one constructor of this many arguments for a given name) /// /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) + /// \tparam P Type of arguments of the constructor (must be defined explicitly) /// /// \return The Class itself so the call can be chained /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template + template Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 1, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 2 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 2, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 3 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 3, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 4 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 4, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 5 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 5, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 6 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 6, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 7 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 7, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 8 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// \tparam A8 Type of argument 8 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 8, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 9 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// \tparam A8 Type of argument 8 of the constructor (must be defined explicitly) - /// \tparam A9 Type of argument 9 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& Ctor(const SQChar *name = 0) { - return BindConstructor(A::template iNew, 9, name); + return BindConstructor(A::template iNew, sizeof...(P), name); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -954,11 +798,11 @@ public: } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 2 arguments (there can only be one constructor of this many arguments for a given name) + /// Binds a constructor with 2 or more arguments (there can only be one constructor of this many arguments for a given name) /// /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) + /// \tparam P Type of arguments of the constructor (must be defined explicitly) /// /// \return The Class itself so the call can be chained /// @@ -966,189 +810,9 @@ public: /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template + template Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 2, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 3 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 3, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 4 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 4, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 5 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 5, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 6 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 6, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 7 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 7, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 8 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 8, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 9 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// \tparam A8 Type of argument 8 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 9, name); - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Binds a constructor with 10 arguments (there can only be one constructor of this many arguments for a given name) - /// - /// \param name Name of the constructor as it will appear in Squirrel (default value creates a traditional constructor) - /// - /// \tparam A1 Type of argument 1 of the constructor (must be defined explicitly) - /// \tparam A2 Type of argument 2 of the constructor (must be defined explicitly) - /// \tparam A3 Type of argument 3 of the constructor (must be defined explicitly) - /// \tparam A4 Type of argument 4 of the constructor (must be defined explicitly) - /// \tparam A5 Type of argument 5 of the constructor (must be defined explicitly) - /// \tparam A6 Type of argument 6 of the constructor (must be defined explicitly) - /// \tparam A7 Type of argument 7 of the constructor (must be defined explicitly) - /// \tparam A8 Type of argument 8 of the constructor (must be defined explicitly) - /// \tparam A9 Type of argument 9 of the constructor (must be defined explicitly) - /// - /// \return The Class itself so the call can be chained - /// - /// \remarks - /// The last parameter is implicitly of StackStrF type and is automatically included into the parameter count. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - template - Class& FmtCtor(const SQChar *name = 0) { - return BindConstructor(A::template iFmtNew, 10, name); + return BindConstructor(A::template iFmtNew, sizeof...(P)+1, name); } };