PortString

Below is an example of a slightly more complex port type, the PortString class. This is used in a later example.
class PortString {
private:
  char *val, *origVal;
  int len, room;

  void makeRoom(int len);

public:
  PortString();
  PortString(char *v);
  PortString(const PortString &source);
  PortString& operator=(const PortString &source);
  PortString& operator= (const char *buf);
  int operator==(const PortString& s) const;

  static int registrationID()  { return 354; }
  static HPCxx_String typeID() { return "examples.MessageKnobPort"; }
  operator char *() { return val; }
  ~PortString();

  friend void hpcxx_pack(HPCxx_Buffer&, PortString*, int);
  friend void hpcxx_unpack(HPCxx_Buffer&, PortString*, int);
  friend ostream& operator<< (ostream &, const PortString &);

};

ostream &operator<< (ostream &, const PortString &);



PortString::PortString() : len(0), room(0)
{
  origVal = val = new (SHM) char [1];
  val[0] = '\0';
}

PortString::PortString(char* v)
{
  len = strlen(v);
  val = new (SHM) char[len+1];
  room = len;
  strncpy(val,v,len);
  val[len] = '\0';
}

PortString::PortString(const PortString &source) 
{
  len = source.len;
  val = new (SHM) char[len+1];
  room = len;
  strncpy(val,source.val,len);
  val[len] = '\0';
}

PortString& PortString::operator=(const PortString &source) 
{
  len = source.len;
  makeRoom(len);
  strncpy(val,source.val,len);
  val[len] = '\0';
  return *this;
}

PortString& PortString::operator=(const char *buf) 
{
  len = strlen(buf);
  makeRoom(len);
  strncpy(val,buf,len);
  val[len] = '\0';
  return *this;
}

void PortString::makeRoom(int l)
{
  if (l > room) {
    deleteSHM(val);
    val = new (SHM) char[l+1];
    room = l;
  }
}

int PortString::operator==(const PortString& str) const
{
  return !strcmp(val,str.val);
}

PortString::~PortString()
{
  deleteSHM(val);
}

ostream &operator<<(ostream  &output, const PortString  &buffer) 
{
  output << buffer.val;
  return output;
}

void hpcxx_pack(HPCxx_Buffer& b, PortString* s, int)  
{
  HPCxx_String typeID = s->typeID();
  hpcxx_byte twenty(20);

  hpcxx_pack(b,&twenty,1);
  hpcxx_pack(b,&typeID,1);

  hpcxx_byte *two = new hpcxx_byte();
  two->byte = 2;
  hpcxx_pack(b, two, 1);                   // 2 indicates a final object
  hpcxx_pack(b, &(s->len), 1);
  if (s->len > 0) {
    hpcxx_pack(b, s->val, s->len);
  }
}

void hpcxx_unpack(HPCxx_Buffer& b, PortString* s, int) 
{
    // NexusRMI packs in the following format
    // byte 2: indicates a final object
    // int length: indicates the number of characters that will follow
    // length of characters
    int l;
    hpcxx_byte *two = new hpcxx_byte();
    hpcxx_unpack(b, two, 1);
    hpcxx_unpack(b, &l, 1);
    s->len = l;
    s->makeRoom(l);
    if (l > 0) {
	 hpcxx_unpack(b, s->val, l);
    }
    s->val[l] = '\0';
}


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

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