On this page:

The OutputStream API in C++

An OutputStream is created using the following functions:

{zcode:cpp}
namespace Ice {
    OutputStreamPtr createOutputStream(const CommunicatorPtr& communicator);
 
    OutputStreamPtr createOutputStream(constCommunicatorPtr& communicator,
                                       const EncodingVersion& version);
}
{zcode}

You can optionally specify an encoding version for the stream, otherwise the stream uses the communicator's default encoding version.

The OutputStream class is shown below.

{zcode:cpp}
namespace Ice {
    class OutputStream : ... {
    public:
        virtual CommunicatorPtr communicator() const = 0;

        virtual void write(bool) = 0;
        virtual void write(Byte) = 0;
        virtual void write(Short) = 0;
        virtual void write(Int) = 0;
        virtual void write(Long) = 0;
        virtual void write(Float) = 0;
        virtual void write(Double) = 0;
        virtual void write(const std::string& v, bool convert = true) = 0;
        virtual void write(const char* v, bool convert = true) = 0;
        virtual void write(const std::wstring&) = 0;
        virtual void write(const std::vector<bool>&) = 0;

        virtual void write(const bool* begin, const bool* end) = 0;
        virtual void write(const Byte* begin, const Byte* end) = 0;
        virtual void write(const Short* begin, const Short* end) = 0;
        virtual void write(const Int* begin, const Int* end) = 0;
        virtual void write(const Long* begin, const Long* end) = 0;
        virtual void write(const Float* begin, const Float* end) = 0;
        virtual void write(const Double* begin, const Double* end) = 0;    

        virtual void write(const std::vector<std::string>& v, bool convert) = 0;
 
        void writeEnum(Int v, Int maxValue) { ... }
 
        virtual bool writeOptional(Int tag, OptionalFormat fmt) = 0;
 
        template<typename T> inline void 
        write(const T& v) { ... }

        virtual void writeSize(Int sz) = 0;
        virtual void startSize() = 0;
        virtual void endSize() = 0;

        virtual void writeProxy(const ObjectPrx& v) = 0;

        template<typename T> inline void
        write(const IceInternal::ProxyHandle<T>& v) { ... }

        virtual void writeObject(const ObjectPtr&) = 0;

        template<typename T> inline void
        write(const IceInternal::Handle<T>& v) { ... }

        virtual void writeException(const UserException& e) = 0;
 
        virtual void startObject(const SlicedDataPtr& sd) = 0;
        virtual void endObject() = 0;

        virtual void startException(const SlicedDataPtr& sd) = 0;
        virtual void endException() = 0;

        virtual void startSlice(const std::string& typeId, bool last) = 0;
        virtual void endSlice() = 0;

        virtual void startEncapsulation(const EncodingVersion& v, FormatType fmt) = 0;
        virtual void startEncapsulation() = 0;
        virtual void endEncapsulation() = 0;

        virtual EncodingVersion getEncoding() const = 0;

        virtual void writePendingObjects() = 0;

        virtual void finished(std::vector<Byte>& v) = 0;
        virtual std::pair<const Byte*, const Byte*> finished() = 0;
 
        virtual size_type pos() = 0;
        virtual void rewrite(Int v, size_type pos) = 0;

        virtual void reset(bool) = 0;
    };
}
{zcode}

Inserting into an OutputStream in C++

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

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

{zcode:cpp}
out = Ice::createOutputStream(communicator);
Ice::Double d = 3.14;
out->write(d);
string s = "Hello";
out->write(s);
{zcode}

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

{zcode:cpp}
out = Ice::createOutputStream(communicator);
IntSeq s = ...;
out->write(s);
 
ComplexType c = ...;
out->write(c);
{zcode}

Inserting Sequences of Built-In Types using Zero-Copy in C++

OutputStream provides a number of overloads that accept a pair of pointers. For example, you can insert a sequence of bytes as follows:

{zcode:cpp}
out = Ice::createOutputStream(communicator);
vector<Ice::Byte> data = ...;
out->write(&v[0], &v[v.size()]);
{zcode}

The same insertion technique works for the other built-in integral and floating-point types, such int and double. Insertion in this way can avoid an additional data copy during marshaling if the internal representation of the data in memory is the same as the on-the-wire representation. (Note that the two pointers must point at a contiguous block of memory.)

 

Other OutputStream Member Functions in C++

The remaining member functions of OutputStream have the following semantics:

See Also