]> git.tdb.fi Git - libs/core.git/commitdiff
Put the check_access function in Base
authorMikko Rasa <tdb@tdb.fi>
Tue, 14 Jun 2011 08:18:18 +0000 (11:18 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 14 Jun 2011 08:18:18 +0000 (11:18 +0300)
source/io/base.cpp
source/io/base.h
source/io/console.cpp
source/io/file.cpp
source/io/file.h
source/io/memory.cpp
source/io/memory.h

index 926440a9c4575da477b37e4ad60eea3593ef86a4..fe706fd5b3cf5cad6b7c5b52e5f229992d5ac933 100644 (file)
@@ -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();
index 8e4d4a547243ec86a867c6fce4c2ba8122c8f11b..9447c66fc9a6551dcdcb5644c9333b882de7dcec 100644 (file)
@@ -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;
 
index 442bbb0f7dc0dbcd70b1cd0da62e74f247f8c162..b695e3c33a90c14bfe521b1e57ad3dc3c08390d1 100644 (file)
@@ -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)
index a91a927fbc4ac5b5c76ba65fcaa341a8571b6e99..cc3f0a9e4da52d40fc9893efb984801b1a09d672 100644 (file)
@@ -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 &>(handle), 0, S_CUR);
 }
 
-void File::check_access(Mode m) const
-{
-       if(!handle || (m && !(mode&m)))
-               throw invalid_access(m);
-}
-
 } // namespace IO
 } // namespace Msp
index cd547b37545cb23b6f0c3842c512a7cf13847905..6c7169718872a4fb5b8128723fd5ebcb28ba711c 100644 (file)
@@ -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)
index 4a6296b44a907321a5e04a585e229523ff7caf86..6ccf84654764cbe8086a3ac841330580d302dcd1 100644 (file)
@@ -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<unsigned>(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
index ca92a7588a7ab406498b691682e7bfcc7be03d5b..809c372ff2dcb56d2b4f75a7aa90b6b7fe433c0b 100644 (file)
@@ -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