4

Intermediate C++ Programmer, using C++17, gcc.

Premise:

Right now I am working on a CLI interface for a more advanced program.

To eliminate the need for both code duplication and readability, I want to write a Macro and/or Template where the programmer may quickly specify the parameters of a function call so that a checkArgs() function will be automatically called with variables depending on the declaration of the function.

Example:

For example:

__Command( String funcName, size_t Min, size_t Max, E_StrType Types[])

Defines a new command for the Interface, if the entered command matches funcName, it will call funcName with the required parameters, if the check passes.

  • funcName = The name of the function
  • Min = Minimum amount of arguments, 0 for optional.
  • Max = Maximum amount of Arguments, 0 for none, unless min != 0, then it is unlimited (variable)
  • Types[], a list of types, in order from first to last, that the function requires. The last item on this list becomes the one used for every new argument if Max is 0.

example call:

__Command( testMsg, 1, 2, { E_StrType_String, E_StrType_size_t = 1 } )

which will generate a function with this signature:

testMsg( String Args )

later a:

__DefineCommand( testMsg )
{ code body }

Will generate a call to checkArgs( 1, 2, { String, size_t } ) in the definition of the function, in addition to whatever the programmer puts into the implementation of that function.

I already have a way of checking a string "type".

tl;dr

I am just looking to see how functions can be programatically generated like that, and optionally, if there is any way to provide compile-time checking to make sure the arguments to the macro are valid.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Kyararos
  • 41
  • 1
  • 5
    Unrelated: Don't use a double underscore. They are reserved. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Feb 22 '19 at 23:30
  • "*CLI interface*" Are you talking about a command-line interface or something else? – Nicol Bolas Feb 23 '19 at 05:49
  • command-line. also wow I did interface twice lol. actually it is a real-time console that displays a log in real time, but also renders a comamnd field that the user types characters into. I am just looking for an automated way to specify the properties of a command without creating a higher-level system to incur runtime bloat. – Kyararos Feb 23 '19 at 21:48

0 Answers0