Top Up Prev Next Bottom Contents Index Search

2.6 Programming examples

The following star has no inputs, just an output. The source star generates a linearly increasing or decreasing sequence of float particles on its output. The state value is initialized to define the value of the first output. Each time the star go method fires, the value state is updated to store the next output value. Hence, the attributes of the value state are set so that the state can be overwritten by the star's methods. By default, the star will generate the output sequence 0.0, 1.0, 2.0, etc.


defstar {
name { Ramp }
domain { SDF }
desc {
Generates a ramp signal, starting at "value" (default 0)
with step size "step" (default 1).
}
output {
name { output }
type { float }
}
state {
name { step }
type { float }
default { 1.0 }
desc { Increment from one sample to the next. }
}
state {
name { value }
type { float }
default { 0.0 }
desc { Initial (or latest) value output by Ramp. }
attributes { A_SETTABLE|A_NONCONSTANT }
}
go {
double t = double(value);
output%0 << t;
t += step;
value = t;
}
}
The next example is the Gain star, which multiplies its input by a constant and outputs the result:


defstar {
name { Gain }
domain { SDF }
desc { Amplifier: output is input times "gain" (default 1.0). }
input {
name { input }
type { float }
}
output {
name { output }
type { float }
}
state {
name { gain }
type { float }
default { "1.0" }
desc { Gain of the star. }
}
go {
output%0 << double(gain) * double(input%0);
}
}
The following example of the Printer star illustrates multiple inputs, ANYTYPE inputs, and the use of the print method of the Particle class.


defstar {
name { Printer }
domain { SDF }
inmulti {
name { input }
type { ANYTYPE }
}
state {
name { fileName }
type { string }
default { "<cout>" }
desc { Filename for output. }
}
hinclude { "pt_fstream.h" }
protected {
pt_ofstream *p_out;
}
constructor { p_out = 0;}
destructor { LOG_DEL; delete p_out;}
setup {
delete p_out;
p_out = new pt_ofstream(fileName);
}
go {
pt_ofstream& output = *p_out;
MPHIter nexti(input);
PortHole* p;
while ((p = nexti++) != 0)
output << ((*p)%0).print() << "\t";
output << "\n";
}
}
This star is polymorphic since it can operate on any type of input. Note that the default value of the output filename is < cout>, which causes the output to go to the standard output. This and other aspects of the pt_ofstream output stream class are explained below in "Extended input and output stream classes" on page 3-2. The iterator nexti used to scan the input is explained in "Iterators" on page 3-10.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.