28 CHAPTER 30 Extending ColdFusion with CFX
Listing 30.5 (continued)
// Output optional debug info
if ( pRequest->Debug() )
{
pRequest->WriteDebug( “Debug info...” ) ;
}
}
// Catch Cold Fusion exceptions & re-raise them
catch( CCFXException* e )
{
pRequest->ReThrowException( e ) ;
}
// Catch ALL other exceptions and throw them as
// Cold Fusion exceptions (DO NOT REMOVE! --
// this prevents the server from crashing in
// case of an unexpected exception)
catch( ... )
{
pRequest->ThrowException(
“Error occurred in tag CFX_COMPUTESTANDARDDEVIATION”,
“Unexpected error occurred while processing tag.” ) ;
}
}
Writing the C++ Code
At this point, you could compile and register the new tag using the steps described in the next sec-
tion. If you do so, the code generated by the Wizard will simply display a “Hello, World” type of
message when the tag is actually used in a ColdFusion page. Of course, to actually give your new
tag its functionality, you need to remove the line that outputs the “Hello, World” message and add
your own C++ code that accomplishes whatever task you want the tag to perform.
The code example we’ll be discussing in this chapter creates a CFX tag called
<CFX_Compute
StandardDeviation>
. The tag accepts a query recordset and computes the standard deviation of the
numbers in one of the query’s columns. Tools like Excel provide built-in functions for computing
standard deviations and other common statistical tasks, but CFML does not. The problem can be
solved fairly easily with C++, so it makes for a good CFX tag example.
NOTE
If you’re not familiar with what a standard deviation is, it’s a statistical measure commonly used for analyzing data. Basically, the stan-
dard deviation can be thought of as a way of describing the similarity of a set of numbers. You’re no doubt already familiar the concept
of a mathematical average (also called the mean), right? Well, the standard deviation measures how much the individual numbers
tend to deviate from the average. A set of numbers that fluctuates wildly from number to number will have a higher standard deviation;
numbers that fluctuate less will have a lower standard deviation. If all the numbers are the same (which is another way of saying that
they all conform exactly to the average), the standard deviation is zero. In any case, the standard deviation (and the related concept
of variance) forms the foundation of many strategies for analyzing trends in data, so this CFX tag can put you on the path of creating
pages that perform relatively sophisticated data analysis.
Commenti su questo manuale