Code sharing
To share code between different operators in a toolkit, use one of the supported sharing methods.
The first method is to create a library that is used by the operators. This kind of sharing applies to non-generated code. The other two methods involve sharing code generator code in the form of Perl modules and code generator templates.
Sharing Perl code across operators can be achieved by creating a Perl module in a common directory and using it from the code generator templates of the operators. For instance, consider the operators FileSource and FileSink whose code generator templates are in their respective directories. To share Perl code between these operators, create a third directory that is called Common and put a Perl module that is named AdapterHelper.pm in this directory. Then, use this module from within code generator templates, as follows:
// Code generator template for FileSink
<%
# Get the directory of the code generator template for this operator
my $myDir = $model->getContext()->getOperatorDirectory();
# Put the Common directory into the Perl include path
unshift @INC, dirname($myDir) . "/Common";
require AdapterHelper; # Use the helper module from the Common directory
AdapterHelper::verifySinkModel($model); # Use a function from the helper module
%>
Sharing code generator templates across operators can be achieved
by creating a code generator template in a common location and including
it from the operator code generator templates. Use the @include
directive,
as follows:
<% # Perl Code %>
// C++ code
@include "../relative/file.cgt"
@include "/absolute/file.cgt"
The @include
directive supports both absolute
and relative paths. Relative paths are rooted at the directory that contains
the code generator template that uses the @include
directive.