Re: maximum cases in a switch ?
Lynn McGuire wrote:
Incredibly easy. Here is some of the method:
DataDescriptor aDD_GEN_CALVAPPRE (GEN_CALVAPPRE, "CALVAPPRE",
"Calculate Vapor Pressure for all streams", true, false,
false,
SYM_Enumerated, nil, nil, nil, nil, nil, SYM_BOOLEAN,
SYM_DGenInit, 420, nil, nil);
DataDescriptor * GenGroup::descriptor (int aSymbol, int version)
{
switch (aSymbol)
{
case GEN_CALVAPPRE:
return & aDD_GEN_CALVAPPRE;
case GEN_CALSONVEL:
return & aDD_GEN_CALSONVEL;
case GEN_ONLYWRITESTREAMBOXONFIRSTSHEET:
return & aDD_GEN_ONLYWRITESTREAMBOXONFIRSTSHEET;
...
}
// default return
return NULL;
}
I would strongly recommend a meta data table.
static const DataDescriptor DispatchTable[] =
{ DataDescriptor(GEN_CALVAPPRE, "CALVAPPRE",
"Calculate Vapor Pressure for all streams", true, false, false,
SYM_Enumerated, nil, nil, nil, nil, nil, SYM_BOOLEAN,
SYM_DGenInit, 420, nil, nil),
DataDescriptor(GEN_CALSONVEL, ...),
...
};
If the DataDescriptors are ordered by the key aSymbol, you could use a
binary search to locate a DataDescriptor in the table.
Alternatively class DataDescriptor could hold a static repository of
type std::map<int,DataDescriptor*> with all it's instances. The
constructor of DataDescriptor could register it's own instance in the
repository. Then the method GenGroup::descriptor reduces to a hash
lookup in the repository. No more than a dozen code lines instead of
some thousands in the switch case. And the advantage is that you can
keep the code for the DataDescriptor definitions without changes. You
only have to delete the large switch.
This is a 470,000 line C++ Win32 user interface for a highly
successful calculation engine.
Maybe, but keep in mind that compilers are not well tested with this
kind of code. I triggered internal compiler errors with too large
functions from time to time (large window procedures).
This code is bulletproof and
very extensible.
The two above solutions too.
Marcel