Newsgroups: comp.parallel.pvm
From: Richard Kilgore <rkilgore@lore.ece.utexas.edu>
Subject: Re: Passing C++ objects
Organization: The University of Texas at Austin
Date: Thu, 29 Feb 1996 12:27:04 -0600
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <3135EFF8.32CCFA24@lore.ece.utexas.edu>

> The examples for passing data between processes have shown how it is
> possible to pass variables and arrays of specific types.  How is this
> accomplished when you need to send a object, containing several data types
> of which some may be dynamically allocated,  to another process.
> 
> The worst case could be that the object needs the functionallity to send
> and recieve its variable/array components such that it could reconstruct
> itself.
> 

I usually find it useful to create pack() and unpack() methods for any
class whose objects I wish to pass over the network.  pack() is a normal
(or virtual) method, and unpack() is a static method that returns a pointer
to a new object.  I give a small example below.

	Have fun!

	- rick


class Foo
{
public:
  Foo(int a, double b): _a(a), _b(b) {;}

  virtual void pack();
  static Foo* unpack();

private:
  int _a;
  double _b;
};


void Foo::pack()
{ pvm_pkint(&_a, 1, 1);
  pvm_pkdouble(&_b, 1, 1);
}


Foo* Foo::unpack()
{ int a;
  double b;
  pvm_upkint(&a, 1, 1);
  pvm_upkdouble(&b, 1, 1);

  return new Foo(a, b);
}


-- 
Richard B. Kilgore
Grad Student, University of Texas at Austin
WWW URL: http://lore.ece.utexas.edu/~rkilgore/
E-mail rkilgore@lore.ece.utexas.edu

