[Box Backup-commit] COMMIT r3253 - box/trunk/lib/common

subversion at boxbackup.org subversion at boxbackup.org
Tue Feb 11 10:29:08 GMT 2014


Author: chris
Date: 2014-02-11 10:29:08 +0000 (Tue, 11 Feb 2014)
New Revision: 3253

Modified:
   box/trunk/lib/common/MemBlockStream.cpp
   box/trunk/lib/common/MemBlockStream.h
Log:
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.

Modified: box/trunk/lib/common/MemBlockStream.cpp
===================================================================
--- box/trunk/lib/common/MemBlockStream.cpp	2014-02-11 10:29:01 UTC (rev 3252)
+++ box/trunk/lib/common/MemBlockStream.cpp	2014-02-11 10:29:08 UTC (rev 3253)
@@ -52,6 +52,26 @@
 // --------------------------------------------------------------------------
 //
 // 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

Modified: box/trunk/lib/common/MemBlockStream.h
===================================================================
--- box/trunk/lib/common/MemBlockStream.h	2014-02-11 10:29:01 UTC (rev 3252)
+++ box/trunk/lib/common/MemBlockStream.h	2014-02-11 10:29:08 UTC (rev 3253)
@@ -29,6 +29,7 @@
 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 @@
 	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;




More information about the Boxbackup-commit mailing list