StringStream.h

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

#pragma once
#ifndef Ceda_cxUtils_StringStream_H
#define Ceda_cxUtils_StringStream_H

#include "cxUtils.h"
#include "xstring.h"
#include "xostream.h"
#include "PagedBuffer.h"
#include "PagedBufferAsStream.h"

namespace ceda
{

class cxUtils_API StringStream : public xostream
{
    cxNotCloneable(StringStream)
public:
    explicit StringStream(ssize_t pageSize = 4096);
    ~StringStream();

    void GetString(xstring& s, bool clear = true);
    inline xstring GetString()
    {
        xstring s;
        GetString(s);
        return s;
    }
    /*
    operator xstring()
    {
        return GetString();
    }    
    */
    ssize_t GetLength()
    {
        return m_pb.GetSize();
    }

    // Warning: It can be important to call flush() on this xostream before accessing the
    // underlying PagedBuffer.
    PagedBuffer& GetPagedBuffer() { return m_pb; }
    
    void clear() { m_pb.Clear(); }

private:
    PagedBuffer m_pb;
    PagedBufferToOutputStreamAdapter m_outs;
};


//#define cxMakeString(x)  static_cast<ceda::StringStream&>( (ceda::StringStream&) ceda::StringStream() << x)
//#define cxMakeString2(x) ceda::xstring(cxMakeString(x)).c_str()

#define cxMakeString(x)  CEDA_REFERENCE_TEMPORARY(ceda::StringStream, CEDA_REFERENCE_DEFAULT_TEMPORARY(ceda::StringStream) << x).GetString()

#define cxMakeString2(x) cxMakeString(x).c_str()

#define cxMakeString_8(x)  AsString8(cxMakeString(x))
#define cxMakeString2_8(x) cxMakeString_8(x).c_str()

///////////////////////////////////////////////////////////////////////////////////////////////////
// ToString

template <typename T> xstring ToString(const T& x)
{
    StringStream os;
    os << x;
    return os.GetString();
}

inline xstring ToString(const xstring& x)
{
    return x;
}

cxUtils_API xstring ToString(bool);
cxUtils_API xstring ToString(char8);
cxUtils_API xstring ToString(int8);
cxUtils_API xstring ToString(uint8);
cxUtils_API xstring ToString(int16);
cxUtils_API xstring ToString(uint16);
cxUtils_API xstring ToString(int32);
cxUtils_API xstring ToString(uint32);
cxUtils_API xstring ToString(int64);
cxUtils_API xstring ToString(uint64);
cxUtils_API xstring ToString(float32);
cxUtils_API xstring ToString(float64);

} // namespace ceda

#endif // include guard