From: Mikko Rasa Date: Tue, 14 Jun 2011 08:18:18 +0000 (+0300) Subject: Put the check_access function in Base X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=df5ab3d867c51d72344e443e3adb05bfa29a2b53;p=libs%2Fcore.git Put the check_access function in Base --- diff --git a/source/io/base.cpp b/source/io/base.cpp index 926440a..fe706fd 100644 --- a/source/io/base.cpp +++ b/source/io/base.cpp @@ -16,6 +16,12 @@ Base::~Base() signal_deleted.emit(); } +void Base::check_access(Mode m) const +{ + if(!(mode&m)) + throw invalid_access(m); +} + bool Base::getline(string &line) { line.clear(); diff --git a/source/io/base.h b/source/io/base.h index 8e4d4a5..9447c66 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -54,6 +54,8 @@ public: Mode get_mode() const { return mode; } protected: + void check_access(Mode) const; + virtual unsigned do_write(const char *, unsigned) = 0; virtual unsigned do_read(char *, unsigned) = 0; diff --git a/source/io/console.cpp b/source/io/console.cpp index 442bbb0..b695e3c 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -68,8 +68,7 @@ void Console::set_block(bool b) void Console::set_local_echo(bool e) { - if(!(mode&M_READ)) - throw invalid_access(M_READ); + check_access(M_READ); #ifdef WIN32 DWORD m; @@ -85,8 +84,7 @@ void Console::set_local_echo(bool e) void Console::set_line_buffer(bool l) { - if(!(mode&M_READ)) - throw invalid_access(M_READ); + check_access(M_READ); #ifdef WIN32 DWORD m; @@ -103,8 +101,7 @@ void Console::set_line_buffer(bool l) void Console::get_size(unsigned &rows, unsigned &cols) { - if(!(mode&M_WRITE)) - throw invalid_access(M_WRITE); + check_access(M_WRITE); #ifdef WIN32 // XXX Figure out how to do this @@ -120,16 +117,14 @@ void Console::get_size(unsigned &rows, unsigned &cols) unsigned Console::do_write(const char *buf, unsigned len) { - if(!(mode&M_WRITE)) - throw invalid_access(M_WRITE); + check_access(M_WRITE); return sys_write(handle, buf, len); } unsigned Console::do_read(char *buf, unsigned len) { - if(!(mode&M_READ)) - throw invalid_access(M_READ); + check_access(M_READ); unsigned ret = sys_read(handle, buf, len); if(ret==0) diff --git a/source/io/file.cpp b/source/io/file.cpp index a91a927..cc3f0a9 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -93,8 +93,6 @@ File::~File() void File::set_block(bool b) { - check_access(M_NONE); - mode = (mode&~M_NONBLOCK); if(b) mode = (mode|M_NONBLOCK); @@ -147,8 +145,6 @@ void File::sync() SeekOffset File::seek(SeekOffset off, SeekType type) { - check_access(M_NONE); - signal_flush_required.emit(); off = sys_seek(handle, off, type); eof_flag = false; @@ -158,16 +154,8 @@ SeekOffset File::seek(SeekOffset off, SeekType type) SeekOffset File::tell() const { - check_access(M_NONE); - return sys_seek(const_cast(handle), 0, S_CUR); } -void File::check_access(Mode m) const -{ - if(!handle || (m && !(mode&m))) - throw invalid_access(m); -} - } // namespace IO } // namespace Msp diff --git a/source/io/file.h b/source/io/file.h index cd547b3..6c71697 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -54,9 +54,6 @@ public: virtual SeekOffset seek(SeekOffset, SeekType); virtual SeekOffset tell() const; - -private: - void check_access(Mode) const; }; inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n) diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 4a6296b..6ccf846 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -39,7 +39,7 @@ void Memory::init(char *b, char *e, Mode m) unsigned Memory::do_write(const char *buf, unsigned size) { - check_mode(M_WRITE); + check_access(M_WRITE); size = min(size, end-pos); memcpy(pos, buf, size); @@ -63,7 +63,7 @@ unsigned Memory::do_read(char *buf, unsigned size) unsigned Memory::put(char c) { - check_mode(M_WRITE); + check_access(M_WRITE); *pos++ = c; return 1; } @@ -107,11 +107,5 @@ SeekOffset Memory::seek(SeekOffset off, SeekType type) return pos-begin; } -void Memory::check_mode(Mode m) const -{ - if(m==M_WRITE && !(mode&M_WRITE)) - throw invalid_access(M_WRITE); -} - } // namespace IO } // namespace Msp diff --git a/source/io/memory.h b/source/io/memory.h index ca92a75..809c372 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -30,9 +30,6 @@ public: virtual SeekOffset seek(SeekOffset, SeekType); virtual SeekOffset tell() const { return pos-begin; } - -private: - void check_mode(Mode) const; }; } // namespace IO