Extender Question C++ - Optional Parameters

Started by JTaylor, October 04, 2019, 01:57:18 PM

Previous topic - Next topic

JTaylor

How would I create Functions with optional parameters in an Extender?    I know how to create an overloaded function but not sure how this gets handled in the Command Map.  I know it speaks of overrides in the comments as well and guessing that is related.   An example would be appreciated.  Thanks.

Jim

td

In addons.h you will notice

Code (c) Select
   LONG  param1;                        // (funct ? hi(reserved) lo(hibyte(num-optional-args) lobyte(num-required-args)))   (constant ? value)

in the command table definition.   Note the  hibyte(num-optional-args)

In the SDK's C++ template example the command table at the top of the Math64.cpp example file has a comment line that describe each column in the template implementation of a command table. 

Code (c) Select
/////////////////   Member Ptr        Function Name     Name Chars  Required Args Optional Args       Arg Types...

Note the "Optional Args".

In the SDK's old legacy C++ example the macro NP is defined as follows in WILExtender.h

Code (c) Select
/********************************************************************
* Name: NP
*
* Purpose: Use to setting COMMAND_MAP_FUNCTION macro's _NUM_PARAMS argument
*          which sets the number of required and optional function parameters.
*
* Arguments:_OPTIONAL  = number of optional parameters
*           _REQUIRED  = number of required parametsers
*
* Example: // Set parameter 1 as required and parameter 2 as optional.
*          COMMAND_MAP_FUNCTION( PT(1,UNITYPE)|PT(2,INTTYPE), NP(1,1),.....
*         
********************************************************************/
#define NP(_OPTIONAL, _REQUIRED) ( (((_OPTIONAL)<<8)&0x0000ff00)|((_REQUIRED)&0x000000ff) )


"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Almost forgot.  The SDK's Math42.cpp file has the following description of the bit between the braces in the template version of the command table.

Code (c) Select
//     The bit between the braces contains the descriptions of the WIL
//     functions and constants implemented by your extender.  Each
//     comma separated entry has the following format for functions:
//
//                       Member Ptr       Function Name  Name Chars  Required Args  Optional Args     Arg Types...
//     EntryT<CSpiffy>(&CSpiffy::Handy,   _T("sphandy"),     7,           2,            1,         PTStr,PTStr,PTInt),
//
//     The above command table entry identifies a WIL function named
//     "spHandy" that  takes 2 required and 1 optional parameter.  The
//     first 2 parameters are of type string and the third parameter is
//     of type integer. The member pointer is the class member that
//     implements the functionality exposed by the "spHandy" WIL
//     functions.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Excellent.   Will take a look and see what I can make happen.  Thank you VERY much. 

Jim

td

It is all in the SDK.  It is just a matter of taking the time to stare at it for a while.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Yeah...Can't say I have read every single line but did make an honest effort at reading through the comments before asking.  Plus, due to my level of ignorance I might very well have seen what I needed but just didn't recognized it.

Thanks again.

Jim

td

Reading unfamiliar source code can be labor-intensive. Requiring a bit of concentration and exercising of the short term memory.  It is, however, good mental calisthenics for those of us of a "certain" age (not that you are necessarily included in that group.)   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Sorry to bother you again but before I spend anymore hours on this I wanted to ask a silly question...

Are you able to open the VS2017 SDK project (what is posted) and successfully compile and run it?

I was having trouble with my project.  Got to where I could compile and then it was giving me a "Run-Time Check Failure #2 - Stack around the variable szKey was corrupted and nothing I did helped.   So, I decided I would open the Math64 project and compile it and make sure it worked and start my project again.   Encountered the same problem after I changed some things such as including the clocale.h header and such so it eliminated the errors and I could compile.    Got to a point where I could compile but would get that error and if I said ignore it would actually run the WIL script but could never make work properly.  Got to a point where it would tell me the Extender was invalid.    So, I decided to create a new project and bring in the source files and see if I could make that work.   After fixing some of the above mentioned things and eliminating most errors I am still getting errors that I cannot make go away.   I have tried VS2017 and VS2019 with similar results.

Hoping you will test the posted solution so I know whether the problem is me or something with the project.   I assumed it would just Build and run.

Also, if open to ideas that might help some other novice...

The column headers for the command map.  Perhaps change "Name Chars" to "Func.Name Len", assuming that is what it is supposed to be.  That is what I concluded after reading through the source anyway.  I think I will like the new structure better than the old, if I ever get it working.  The optional arguments are more obvious :-)


/////////////////   Member Ptr        Function Name     Name Chars  Required Args Optional Args       Arg Types...

Thanks.

Jim

td

I have no problems building and running the "Math64" extender straight out of the SDK. Executed first try from either the debugger or from WinBatch Studio.  You do need to specify a command target (WinBatch.exe) and command argument (a WinBatch script) to use the debugger.  I used MSFT's Visual Studio 2019 to compile and test.  Since the default WinBatch executable is marked uiAccess TRUE I do have the registry tweaked to force Visual Studio 2019 to always run as an elevated admin.  This is just a quick and dirty shortcut to simplify my life because I don't run WinBatch from a protected directory when I an debugging but there are other ways I could make VS happy.  The easiest is using one of the versions of the WinBatch executables with uiAccess set to FALSE and execution level set to asInvoker.

The column title for the number of characters in the function name could be better but it definitely should not include a word or abbreviation that suggests the word "length".  Text string length is anathema to those of us who work with mixed Unicode-ANSI source code. The number of characters and string length means two different things in that environment.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Okay.  I think I have it working.   Debug DLL was generating an error but when I changed to Release it went away.

I still cannot import project and it work without changes though.   Here is what I had to do.  I assume it is because of some configuration differences that you have and I don't.

I imported the project and told it to use the latest SDK I have installed and to use Toolset 142.

It told me that "stdafx.h" was not found so I changed

#include <stdafx.h>            to             #include "stdafx.h"

That resolved that error but then it told me that LC_NUMERIC was not defined so I added

#include <locale.h>

At this point I was able to Build a usable DLL.

Thanks again.

Jim

td

I was about to mention that there are a couple of changes to the source that reflect changes that have been made to the next SDK release - whenever that is.

One is to add the line

#include <locale>    // Standard Library locales."

to the stdafx.h file and the other is to change the line

#include <stdafx.h>  // Standard MFST Pre-compiled header. Only include stuff that doesn't change much.

to

#include "stdafx.h"  // Standard MFST Pre-compiled header. Only include stuff that doesn't change much.

in the math64.cpp file.

These changes eliminate some potential solution and installation based dependency but may create others for some.

It would be best to use #include <locale> instead of #include <locale.h> when you are using other C++ standard library templates like the extender does.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

I changed the locale include as noted.

WOOHOO!!!   Got it working and with optional parameters.   Now the work begins.  Plus I have to decide if it is worth the trouble converting my CommControl extender to the new version???   That thing has a LOT of functions.   Something for a rainy day I guess.

Thanks again for all the help.

Jim