diff options
author | Chris Wilson <chris+github@qwirx.com> | 2014-02-11 10:29:08 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2014-02-11 10:29:08 +0000 |
commit | 8a058d8e37ccd6b4ab538bcf854a71a57f47273c (patch) | |
tree | 811602863390faf0c10adb99d5dad79ac9d51b08 /lib/common | |
parent | 50fc10551f25ce34d64bca9711efbca50996c329 (diff) |
Add a MemBlockStream constructor from a simple string.
Useful for sending simple string messages through Protocols or writing them
to streams. Takes a copy of the string, so you don't need to worry about object
lifetime.
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/MemBlockStream.cpp | 20 | ||||
-rw-r--r-- | lib/common/MemBlockStream.h | 4 |
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/common/MemBlockStream.cpp b/lib/common/MemBlockStream.cpp index 3a43a304..f6b7d07b 100644 --- a/lib/common/MemBlockStream.cpp +++ b/lib/common/MemBlockStream.cpp @@ -52,6 +52,26 @@ MemBlockStream::MemBlockStream(const void *pBuffer, int Size) // -------------------------------------------------------------------------- // // Function +// Name: MemBlockStream::MemBlockStream(const std::string& rMessage) +// Purpose: Convenience constructor for sending a simple string. +// Copies the string, so you can pass a temporary in. +// Created: 2014/01/20 +// +// -------------------------------------------------------------------------- +MemBlockStream::MemBlockStream(const std::string& rMessage) +: mReadPosition(0) +{ + mTempBuffer.Write(rMessage.c_str(), rMessage.size()); + mTempBuffer.SetForReading(); + mpBuffer = mTempBuffer.GetBuffer(); + mBytesInBuffer = rMessage.size(); + ASSERT(mpBuffer != 0); + ASSERT(mBytesInBuffer >= 0); +} + +// -------------------------------------------------------------------------- +// +// Function // Name: MemBlockStream::MemBlockStream(const StreamableMemBlock &) // Purpose: Constructor (doesn't copy block, careful with lifetimes) // Created: 2003/09/05 diff --git a/lib/common/MemBlockStream.h b/lib/common/MemBlockStream.h index 5234525b..f02e57ba 100644 --- a/lib/common/MemBlockStream.h +++ b/lib/common/MemBlockStream.h @@ -29,6 +29,7 @@ class MemBlockStream : public IOStream public: MemBlockStream(); MemBlockStream(const void *pBuffer, int Size); + MemBlockStream(const std::string& rMessage); MemBlockStream(const StreamableMemBlock &rBlock); MemBlockStream(const CollectInBufferStream &rBuffer); MemBlockStream(const MemBlockStream &rToCopy); @@ -46,6 +47,9 @@ public: virtual int GetSize() const { return mBytesInBuffer; } private: + // Use mTempBuffer when we need to hold a copy of the memory block, + // and free it ourselves when done. + CollectInBufferStream mTempBuffer; const char *mpBuffer; int mBytesInBuffer; int mReadPosition; |