In some situations it may be necessary for an application to intercept the insertion and extraction of Ice objects. For example, the Ice extension for PHP is implemented using Ice for C++ but represents Ice objects as native PHP objects. The PHP extension accomplishes this by manually encoding and decoding Ice objects as directed by the data encoding rules. However, the extension obviously cannot pass a native PHP object to the C++ stream function for writing objects. To bridge this gap between object systems, Ice supplies the helper classes described below.
On this page:
The ObjectWriter
base class facilitates the insertion of objects to a stream:
{zcode:java} package Ice; public abstract class ObjectWriter extends ObjectImpl { public abstract void write(OutputStream out); // ... } {zcode} |
A foreign Ice object is inserted into a stream using the following technique:
ObjectWriter
. This class wraps the foreign object and implements the write
member function.writeObject
. (This is possible because ObjectWriter
derives from Ice.Object
.) Eventually, the write
member function is invoked on the wrapper instance.write
encodes the object's state as directed by the data encoding rules for classes.It is the application's responsibility to ensure that there is a one-to-one mapping between foreign Ice objects and wrapper objects. This is necessary in order to ensure the proper encoding of object graphs.
The ObjectReader
class facilitates the extraction of objects from a stream:
{zcode:java} package Ice; public abstract class ObjectReader extends ObjectImpl { public abstract void read(InputStream in); // ... } public interface CompactIdResolver { String resolve(int id); } {zcode} |
Extracting the state of a foreign Ice object is more complicated than insertion:
ObjectReader
. An instance of this class represents a foreign Ice object.ReadObjectCallback
interface. The implementation of invoke
expects its argument to be either null or an instance of the wrapper class as returned by the object factory.readObject
.read
on the wrapper class. The implementation of read
decodes the object's state as directed by the data encoding rules for classes.readObject
is invoked, passing the instance of the wrapper object. All other callback objects representing the same instance in the stream (in case of object graphs) are invoked with the same wrapper object.If your class definitions use compact type IDs, you must also supply an implementation of CompactIdResolver
when initializing the communicator. This object is responsible for translating numeric type IDs into their string equivalents.