From dd318ef67b0c683c8306da0abf806e99cff346e9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Aug 2008 08:50:23 +0000 Subject: [PATCH] Add Filtered class Make do_write and do_read protected everywhere so Filtered can use the base class versions --- source/buffered.h | 1 + source/console.h | 2 +- source/file.h | 5 ++++ source/filtered.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++ source/pipe.h | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 source/filtered.h diff --git a/source/buffered.h b/source/buffered.h index 092be19..8d52d1d 100644 --- a/source/buffered.h +++ b/source/buffered.h @@ -35,6 +35,7 @@ public: unsigned get_current_size() const; private: void set_op(Mode); +protected: unsigned do_write(const char *, unsigned); unsigned do_read(char *, unsigned); }; diff --git a/source/console.h b/source/console.h index 439ee7b..06fa603 100644 --- a/source/console.h +++ b/source/console.h @@ -40,7 +40,7 @@ public: void set_line_buffer(bool); virtual Handle get_event_handle(); -private: +protected: virtual unsigned do_write(const char *, unsigned); virtual unsigned do_read(char *, unsigned); public: diff --git a/source/file.h b/source/file.h index f2271d0..df5105d 100644 --- a/source/file.h +++ b/source/file.h @@ -9,6 +9,8 @@ Distributed under the LGPL #include #include "base.h" +#include "buffered.h" +#include "filtered.h" #include "seek.h" namespace Msp { @@ -47,6 +49,7 @@ private: Handle handle; void check_access(Mode) const; +protected: virtual unsigned do_write(const char *, unsigned); virtual unsigned do_read(char *, unsigned); }; @@ -60,6 +63,8 @@ inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n) inline File::CreateMode operator~(File::CreateMode m) { return File::CreateMode(~(int)m); } +typedef Filtered BufferedFile; + } // namespace IO } // namespace Msp diff --git a/source/filtered.h b/source/filtered.h new file mode 100644 index 0000000..0575175 --- /dev/null +++ b/source/filtered.h @@ -0,0 +1,66 @@ +/* $Id$ + +This file is part of libmspio +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_IO_FILTERED_H_ +#define MSP_IO_FILTERED_H_ + +namespace Msp { +namespace IO { + +template +class Filtered: public B +{ +private: + struct Activator + { + Filtered &f; + + Activator(Filtered &f_): f(f_) { f.active=true; } + ~Activator() { f.active=false; } + }; + + F filter; + bool active; + +public: + Filtered(): filter(*this), active(false) { } + + template + Filtered(A0 a0): B(a0), filter(*this), active(false) { } + + template + Filtered(A0 a0, A1 a1): B(a0, a1), filter(*this), active(false) { } + + F &get_filter() { return filter; } +protected: + virtual unsigned do_write(const char *b, unsigned s) + { + if(!active) + { + Activator a(*this); + return filter.write(b, s); + } + else + return B::do_write(b, s); + } + + virtual unsigned do_read(char *b, unsigned s) + { + if(!active) + { + Activator a(*this); + return filter.read(b, s); + } + else + return B::do_read(b, s); + } +}; + +} // namespace IO +} // namespace Msp + +#endif diff --git a/source/pipe.h b/source/pipe.h index e8e1f53..2ad83f8 100644 --- a/source/pipe.h +++ b/source/pipe.h @@ -31,6 +31,7 @@ private: char *buf_next; #endif +protected: unsigned do_write(const char *, unsigned); unsigned do_read(char *, unsigned); }; -- 2.45.2