Concat

The following example shows how to construct a simple component. This component will be called ConCat, and it will have two input ports and one output port. All data ports will be of type PortString, which is essentially a String type augmented with the methods necessary for use as a port type. In addition to the two input ports of type PortString, there will be an input port of type ConcatParams, which is a "knob" port. ConcatParams is a class that contains an integer wrapped in the class with the necessary methods to by a port type. This integer tells Concat how many times to concatenate the strings (Concat produces (uw)^n, where u and w are the strings on the data input ports, and n was the parameter received in ConcatParams).

Concat, upon receiving the EXECUTE signal from the CAT, will concatenate the two strings that are currently on it's input ports, and write the result to it's output port.

class Concat : public ComponentBase {
public:
  Concat();

protected:
  RETVAL handleInput(int request);

private:
  InputPort<ConcatParams>  parameterPort;
  InputPort<PortString>    iPort1;
  InputPort<PortString>    iPort2;
  OutputPort<PortString>   oPort;

  ConcatParams             params;
};
 

class MakeConcat : public ComponentCreator {
public:
  ComponentBase* create { return new Concat(); }
}; 


Concat::Concat() : parameterPort(&event,Request::CONFIG),
                   iPort1(&event,Request::EXECUTE),
                   iPort2(&event,Request::EXECUTE)
{
  setReadme("Concat: When executed, outputs the concatenation of the inputs repeated the specified number of times");
  setName("Concat");

  parameterPort.setName("Concat Parameters");
  parameterPort.setReadme("The number of times to repeat the concatenation.");

  iPort[1].setName("Input String #1");
  iPort[1].setReadme("The first string to be concatenated.");
  iPort[2].setName("Input String #2");
  iPort[2].setReadme("The second string to be concatenated.");
  oPort.setName("Output String");
  oPort.setReadme("The string from the input port");

  addInputPort(parameterPort);
  addInputPort(iPort1);
  addInputPort(iPort2);
  addOutputPort(iPort);

  InputPort<ConcatParams>::addExemplar();
  InputPort<PortString>::addExemplar();
}
 
RETVAL Concat::handleInput(int request) 
{
  if (request == Request::CONFIG) {
    if (!parameterPort.dataReady()) {
      cerr << "Nothing on the parameterPort!" << endl;
      return RETVAL::ERROR;
    }
    concatParams.read(params);
    return RETVAL::OK;
  }

  PortString s1,s2;
  // normally, we might check to see if dataReady on the input ports, but
  // in this case, it is OK if one is empty (it will we interpreted as the
  // empty string
  iPort1.read(s1);       
  iPort2.read(s2);
 
  ostrstream t;
  for (int i=0; i<params.times; i++) 
    t << s1 << s2;
  t << '\0';
 
  oPort->write((PortString)t.str());
  executePort.add(outputPort.list[0]);
  return RETVAL::OK;
}


[ IU CS ] [ Extreme! Computing ] [ CAT ] [ HPC++ CAT ]
dastern@extreme.indiana.edu

Last updated: Tue Jan 26 16:38:26 1999