Writing C++ Code for Primitives

Knowledge of C++ is required when reading this section. Furthermore, reading Infrastructure for Primitive Definition is highly recommended, since it explains some of the more generic and useful classes defined in the MLDesigner kernel. Many of these can be useful in primitives.

C++ code segments are an important part of any primitive definition. They can appear in the setup, begin, go, wrapup, constructor, destructor, exectime, header, code and method items of primitive source code. These items all include a body of arbitrary C++ code, enclosed by curly braces, { and }. In all but the code and header items, the C++ code between braces defines the body of a method of the primitive class. Methods can access any member of the class, including ports for input and output, parameters, and members defined with the public, protected or private items.

The Structure of an MLDesigner Primitive

In general, the task of an MLDesigner primitive is to receive input particles and/or produce output particles. In addition, there may be side effects (reading or writing files, displaying graphs, or even updating shared data structures). As for all C++ objects, the constructor is called when the primitive is created, and the destructor is called when it is destroyed. In addition, the setup and begin methods - if any - are called every time a new simulation run is started, the go method (which always exists except for primitives like BlackHole and Null) is called each time a primitive is executed, and the wrapup and cleanup methods are called after the simulation run completes.

 

Reading Inputs and Writing Outputs

The exact mechanism for references to input and output ports depends somewhat on the domain. This is because primitives in the domain XXX use objects of class InXXXPort and OutXXXPort (derived from PortHole) for input and output, respectively. The examples we use here are for the SDF domain. See the appropriate domain chapter for variations that apply to other domains.

Parameters

A parameter is defined by the defparameter item. The primitive can use a parameter to store data values, remembering them from one invocation to another.

Array Parameter

The ArrayState classes (FloatArrayState, IntArrayState, ComplexArrayState, FixArrayState and StringArrayState) are used to store arrays of data.

Programming Examples

Preventing Memory Leaks in C++ Code