Example 3

The following example of the SDF primitive Printer illustrates multiple inputs of type anytype and the use of the print method of the Particle class. The destructor method is used here also.

  1. defprimitive
  2. {
  3.   name        { Printer }
  4.   domain      { SDF }
  5.  
  6.   desc
  7.   {
  8.     Print out one sample from each input port per line.
  9.   }
  10.  
  11.   htmldoc
  12.   {
  13.     Print out one sample from each input port per line. The <b>fileName</b> state specifies the file to be written; the special names &lt;stdout&gt; and &lt;cout&gt;, which specify the standard output stream, and &lt;stderr&gt; and &lt;cerr&gt;, which specify the standard error stream, are also supported.
  14.   }
  15.  
  16.   inmulti
  17.   {
  18.     name { input }
  19.     type { anytype }
  20.   }
  21.  
  22.   defparameter
  23.   {
  24.     name        { fileName }
  25.     type        { filename }
  26.     default     {"<stdout>"}
  27.     desc        {"Filename for output."}
  28.   }
  29.  
  30.   defparameter
  31.   {
  32.     name        { Title }
  33.     type        { string }
  34.   }
  35.  
  36.   defparameter
  37.   {
  38.     name        { EndCondition }
  39.     type        { boolean }
  40.     default     {"FALSE"}
  41.     desc        {"If EndCondition is set to TRUE, the simulation will end when NumberOfItems
  42.                    have been consumed or when the number of cycles in Run Length have
  43.                    been executed, whichever comes first."}
  44.   }
  45.  
  46.   defparameter
  47.   {
  48.     name        { NumberOfItems }
  49.     type        {int}
  50.     default     {"1"}
  51.     desc        {"The number of particles comsumed by an input port. If the primitive receives
  52.                    NumberOfItems input particles and the parameter EndCondition is set to TRUE,
  53.                    then the simulation will be finished."}
  54.   }
  55.  
  56.   location { SDF main library }
  57.  
  58.   hinclude {"kernel/pt_fstream.h"}
  59.  
  60.   protected
  61.   {
  62.     pt_ofstream *p_out;
  63.     int numItemsCounter;
  64.     bool mReqEnd;
  65.   }
  66.  
  67.   constructor
  68.   {
  69.     p_out = 0;
  70.   }
  71.  
  72.   setup
  73.   {
  74.     if(EndCondition)
  75.     {
  76.       willRequestEnd();
  77.       if(NumberOfItems < 1)
  78.         Error::abortRun(*this, " the value of the parameter NumberOfItems is less than 1");
  79.     }
  80.     numItemsCounter = 0;
  81.     mReqEnd = false;
  82.        
  83.     // in case file was open from previous run w/o wrapup call
  84.     LOG_DEL; delete p_out;
  85.     LOG_NEW; p_out = new pt_ofstream(fileName);
  86.   }
  87.  
  88.   go
  89.   {
  90.     if(!mReqEnd)
  91.     {
  92.       pt_ofstream& output = *p_out;
  93.       InSDFMPHIter nexti(input);
  94.       InSDFPort* p;
  95.       output << "\nPrint " << fullName() << endl;
  96.       if(!Title.null())
  97.         output << (constchar*)Title << endl;
  98.  
  99.       while((p = nexti++) != 0)
  100.         output << ((*p)%0).print() << "\t";
  101.       output << "\n";
  102.            
  103.       if(EndCondition && ++numItemsCounter == NumberOfItems)
  104.       {
  105.         mReqEnd = true;
  106.         requestEnd();
  107.       }
  108.     }
  109.   }
  110.  
  111.   wrapup
  112.   {
  113.     LOG_DEL; delete p_out;    // flush output
  114.     p_out = 0;
  115.   }
  116.  
  117.   destructor
  118.   {
  119.     LOG_DEL; delete p_out;    // flush output
  120.   }
  121. }
  122.  

This primitive is polymorphic since it can operate on any type of input. Note that the default value of the output filename is <stdout>, which causes the output to go to the standard output in this case the Simulation console. This and other aspects of the pt_ofstream output stream class are explained below in Extended Input and Output Stream Classes.