From: Mikko Rasa Date: Thu, 27 Sep 2012 20:41:20 +0000 (+0300) Subject: Add a mode parameter to Memory constructor with non-const pointers X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=20b368c89a98f22243a0d496037ad0c43671eea7;p=libs%2Fcore.git Add a mode parameter to Memory constructor with non-const pointers This makes it possible to explicitly set the mode for a Memory, and in particular to create a write-only Memory. ZlibCompressed does not work with M_RDWR so writing compressed data to memory was impossible before this commit. --- diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 274f6ba..bc14ff1 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -8,14 +8,14 @@ using namespace std; namespace Msp { namespace IO { -Memory::Memory(char *d, unsigned s) +Memory::Memory(char *d, unsigned s, Mode m) { - init(d, d+s, M_RDWR); + init(d, d+s, m); } -Memory::Memory(char *b, char *e) +Memory::Memory(char *b, char *e, Mode m) { - init(b, e, M_RDWR); + init(b, e, m); } Memory::Memory(const char *cd, unsigned s) @@ -49,6 +49,8 @@ unsigned Memory::do_write(const char *buf, unsigned size) unsigned Memory::do_read(char *buf, unsigned size) { + check_access(M_READ); + if(pos==end) { set_eof(); @@ -70,6 +72,8 @@ unsigned Memory::put(char c) bool Memory::getline(string &line) { + check_access(M_READ); + if(pos==end) { set_eof(); @@ -84,6 +88,8 @@ bool Memory::getline(string &line) int Memory::get() { + check_access(M_READ); + if(pos==end) { set_eof(); diff --git a/source/io/memory.h b/source/io/memory.h index 809c372..4ae8eec 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -14,8 +14,8 @@ private: char *pos; public: - Memory(char *, unsigned); - Memory(char *, char *); + Memory(char *, unsigned, Mode = M_RDWR); + Memory(char *, char *, Mode = M_RDWR); Memory(const char *, unsigned); Memory(const char *, const char *); private: