OutputStreamToOStream.h

// OutputStreamToOStream.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2008

#pragma once
#ifndef Ceda_cxUtils_OutputStreamToOStream_H
#define Ceda_cxUtils_OutputStreamToOStream_H

/*
OutputStreamToOStream adapts a IOutputStream so it looks like an xostream.  This allows 
functions that normally output to an xostream to in fact send the text to an IOutputStream.
*/

#include "cxUtils.h"
#include "xchar.h"
#include "StreamInterfaces.h"
#include <streambuf>
#include <ostream>
#include <stdio.h>

#ifdef _MSC_VER
    // struct 'X' needs to have dll-interface to be used by clients of class 'Y'
    #pragma warning(disable:4251)

    // non dll-interface struct 'X' used as base for dll-interface class 'Y'
    #pragma warning(disable:4275)
#endif

namespace ceda
{

///////////////////////////////////////////////////////////////////////////////////////////////////
// OutputStreamAsStreambuf

class cxUtils_API OutputStreamAsStreambuf : public std::basic_streambuf<xchar>
{
public:
    OutputStreamAsStreambuf(IOutputStream& outputStream);
    ~OutputStreamAsStreambuf();

    // Implementation of streambuf
    virtual int_type overflow(int_type nCh = EOF);
    virtual int_type underflow();
    virtual int sync();

private:
    enum { STREAM_BUFFER_SIZE = 1024 };

    IOutputStream& m_outputStream;
    xchar m_buffer[STREAM_BUFFER_SIZE];
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// OutputStreamToOStream

// Looks like an ostream, and sends all the text onto a given IOutputStream
class cxUtils_API OutputStreamToOStream : public std::basic_ostream<xchar>
{
public:
    OutputStreamToOStream(IOutputStream& outputStream);

private:
    OutputStreamAsStreambuf m_streamBuf;
};

} // namespace ceda

#endif // include guard