On this page:

Initializing an OutputStream in C++

The OutputStream class provides a number of overloaded constructors:

namespace Ice
{
    class OutputStream
    {
    public:
        OutputStream();

        OutputStream(const std::shared_ptr<Communicator>& communicator);
 
        OutputStream(const std::shared_ptr<Communicator>& communicator, const EncodingVersion& version);

        OutputStream(const std::shared_ptr<Communicator>& communicator, const EncodingVersion& version,
                 const std::pair<const Byte*, const Byte*>& buf);
    };
} 
namespace Ice
{
    class OutputStream
    {
    public:
        OutputStream();

        OutputStream(const CommunicatorPtr& communicator);
 
        OutputStream(const CommunicatorPtr& communicator, const EncodingVersion& version);

        OutputStream(const CommunicatorPtr& communicator, const EncodingVersion& version,
                     const std::pair<const Byte*, const Byte*>& buf);
    };
} 

The constructors optionally accept the following arguments:

We recommend supplying a communicator instance. The stream inspects the communicator's settings to configure several of its own default settings, but you can optionally configure these settings manually using functions that we'll describe later.

If you omit an encoding version, the stream uses the default encoding version of the communicator (if provided) or the most recent encoding version.

Instances of OutputStream can be allocated statically or dynamically.

If a communicator instance is not available at the time you construct the stream, you can optionally supply it later using one of the overloaded initialize functions:

class OutputStream
{
public:
    void initialize(const std::shared_ptr<Communicator>& communicator);
    void initialize(const std::shared_ptr<Communicator>& communicator, const EncodingVersion& version);
    ...
};
class OutputStream
{
public:
    void initialize(const CommunicatorPtr& communicator);
    void initialize(const CommunicatorPtr& communicator, const EncodingVersion& version);
    ...
};

Invoking initialize causes the stream to re-initialize its settings based on the configuration of the given communicator.

Use the following function to manually configure the stream:

class OutputStream
{
public:
    void setFormat(FormatType);
    ...
};

For instances of Slice classes, the format determines how the slices of an instance are encoded. If the stream is initialized with a communicator, this setting defaults to the value of Ice.Default.SlicedFormat, otherwise the setting defaults to the compact format.

Inserting into an OutputStream in C++

OutputStream provides a number of overloaded write member functions that allow you to insert any parameter into the stream simply by calling write.

For example, you can insert a double value followed by a string into a stream as follows:

Ice::OutputStream out(communicator);
Ice::Double d = 3.14;
out.write(d);
string s = "Hello";
out.write(s);

Likewise, you can insert a sequence of built-in type, or a complex, with the same syntax:

Ice::OutputStream out(communicator);
IntSeq s = ...;
out.write(s);
 
ComplexType c = ...;
out.write(c);

With the C++11 mapping, you can also write a list of parameters in one call with writeAll. For example, the previous sample can be rewritten as:

Ice::OutputStream out(communicator);
IntSeq s = ...;
ComplexType c = ...;
out.writeAll(s, c);

Here are the functions for inserting data into an stream:

class OutputStream : ... 
{
public:

    void write(bool);
    void write(Byte);
    void write(short);
    void write(int);
    void write(long long int);
    void write(float);
    void write(double);
    void write(const std::string&, bool = true);
    void write(const char*, size_t, bool = true);
    void write(const char*, bool = true);
    void write(const std::string*, const std::string*, bool = true);
 
    void write(const std::wstring&);
    void write(const std::wstring*, const std::wstring*);
 
    template<typename T> void write(const std::vector<T>& v);

    template<typename T> void write(const T* begin, const T* end);

    void writeBlob(const std::vector<Byte>&);
    void writeBlob(const Byte* v, size_type sz);

    void writeEnum(int v, int maxValue);
 
    bool writeOptional(int tag, OptionalFormat fmt);

    template<typename T> void write(int tag, const Optional<T>& v);

    template<typename T> void write(const T& v);
 
    // Insert a proxy of the base type
    void writeProxy(const std::shared_ptr<ObjectPrx>&);

    // Insert a proxy of a user-defined interface type
    template</* T is-a ObjectPrx */>
    void write(const std::shared_ptr<T>& v);

    // Insert an instance of a Slice class
    template</* T is-a Value */>
    void write(const std::shared_ptr<T>& v);

    void startValue(const std::shared_ptr<SlicedData>& sd);
    void endValue();

    void startException(const std::shared_ptr<SlicedData>& sd);
    void endException();
 
    // Write all parameters in one call
    template<typename T> void writeAll(const T& v);
    template<typename T, typename... Te> void writeAll(const T& v, const Te&... ve);
 
    // Write all optional parameters in one call
    template<typename T> void writeAll(std::initializer_list<int> tags, const Optional<T>& v);
    template<typename T, typename... Te> void writeAll(std::initializer_list<int> tags, const Optional<T>& v, const Optional<Te>&... ve);

    void writeSize(int sz);
    size_type startSize();
    void endSize(size_type pos);
    void rewriteSize(int v, iterator dest);
    void write(int v, iterator dest);
    void rewrite(int v, size_type pos);

    void writeException(const UserException& e);

    void startSlice(const std::string& typeId, int compactId, bool last);
    void endSlice();

    void startEncapsulation(const EncodingVersion& v, FormatType fmt);
    void startEncapsulation();
    void endEncapsulation();
    void writeEmptyEncapsulation(const EncodingVersion& v);
    void writeEncapsulation(const Byte* v, int sz);

    EncodingVersion getEncoding() const;

    void writePendingValues();

    void finished(std::vector<Byte>& v);
    std::pair<const Byte*, const Byte*> finished();

    void resize(size_type sz);

    size_type pos();
    void pos(size_type n);
};
class OutputStream : ... 
{
public:

    void write(bool);
    void write(Byte);
    void write(short);
    void write(int);
    void write(long long int);
    void write(float);
    void write(double);
    void write(const std::string&, bool = true);
    void write(const char*, size_t, bool = true);
    void write(const char*, bool = true);
    void write(const std::string*, const std::string*, bool = true);
 
    void write(const std::wstring&);
    void write(const std::wstring*, const std::wstring*);
 
    template<typename T> void write(const std::vector<T>& v);

    template<typename T> void write(const T* begin, const T* end);

    void writeBlob(const std::vector<Byte>&);
    void writeBlob(const Byte* v, size_type sz);

    void writeEnum(int v, int maxValue);
 
    bool writeOptional(int tag, OptionalFormat fmt);

    template<typename T> void write(int tag, const IceUtil::Optional<T>& v);

    template<typename T> void write(const T& v);
 
    // Insert a proxy of the base interface type
    void write(const ObjectPrx&);

    // Insert a proxy of a user-defined interface type
    template<typename T> void
    write(const ProxyHandle<T>& v);

    // Insert an instance of the base class type
    void write(const ObjectPtr& v);

    // Insert an instance of a user-defined class type
    template<typename T> void
    write(const Handle<T>& v);

    void startValue(const SlicedDataPtr& sd);
    void endValue();

    void startException(const SlicedDataPtr& sd);
    void endException();

    void writeSize(int sz);
    size_type startSize();
    void endSize(size_type pos);
    void rewriteSize(int v, iterator dest);
    void write(int v, iterator dest);
    void rewrite(int v, size_type pos);

    void writeException(const UserException& e);

    void startSlice(const std::string& typeId, int compactId, bool last);
    void endSlice();

    void startEncapsulation(const EncodingVersion& v, FormatType fmt);
    void startEncapsulation();
    void endEncapsulation();
    void writeEmptyEncapsulation(const EncodingVersion& v);
    void writeEncapsulation(const Byte* v, int sz);

    EncodingVersion getEncoding() const;

    void writePendingValues();

    void finished(std::vector<Byte>& v);
    std::pair<const Byte*, const Byte*> finished();

    void resize(size_type sz);

    size_type pos();
    void pos(size_type n);
};

Some of the OutputStream functions need more explanation:

See Also