Helper class generation
When you develop generic operators, you often need to emit
a class, which contains member variables with a list of types. The
SPL code generation framework provides an emitClass
function
within the SPL::CodeGen
module for this purpose.
Examples include emitting a class for holding partitionBy
expressions
for windowed operators or emitting a class to create a hash key out
of a list of expression values.
The emitClass
function takes three parameters. The first one is the operator
instance model. The second one is the name of the class, and the third one is the list of types
wanted for the member variables. The function returns the name of the emitted class, which has a
unique name that is computed from the second parameter. In summary, given a name and a list of
types, the emitClass
function emits a C++ class that contains member variables with
the specified types. These member variables are public and are named as
field<index>_
, where the indexing follows the order of types that are passed in.
Importantly, the emitted class can be hashed, and thus it can be stored in an SPL map or set or any
std::tr1
hash-based container. The class also has empty, copy, and member
initializer constructors, an assignment operator, and equality and non-equality comparison
operators. The emitClass
function must be used in a _h.cgt
file, before the headerPrologue
. The following is an example use:
// This is an <opname>_h.cgt file
<%
my $partitionByParam = $model->getParameterByName("partitionBy");
my @partitionByTypes = SPL::CodeGen::getParameterCppTypes($partitionByParam);
my $partitionCppType = SPL::CodeGen::emitClass($model, "PartitionBy", @partitionByTypes);
...
SPL::CodeGen::headerPrologue($model);
%>
class MY_OPERATOR : public MY_BASE_OPERATOR {
public:
typedef <%=$partitionCppType%> PartitionByType;
...
}