Add Filtered class
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2008 08:50:23 +0000 (08:50 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2008 08:50:23 +0000 (08:50 +0000)
Make do_write and do_read protected everywhere so Filtered can use the base class versions

source/buffered.h
source/console.h
source/file.h
source/filtered.h [new file with mode: 0644]
source/pipe.h

index 092be19e8f0ad00241d67f915d0931b2697cc9ce..8d52d1dc9983276d434dfa208037716bf08190c0 100644 (file)
@@ -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);
 };
index 439ee7b8a9ae0a53bdb974d4453a296314cfae42..06fa603c495a7f99950492088d32ae4b8d2d4598 100644 (file)
@@ -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:
index f2271d02623c439dcd1385af35beb4d4c4fc841f..df5105daaade7f83a1496f80bb8476cbd75eb40a 100644 (file)
@@ -9,6 +9,8 @@ Distributed under the LGPL
 
 #include <string>
 #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<File, Buffered> BufferedFile;
+
 } // namespace IO
 } // namespace Msp
 
diff --git a/source/filtered.h b/source/filtered.h
new file mode 100644 (file)
index 0000000..0575175
--- /dev/null
@@ -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<typename B, typename F>
+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<typename A0>
+       Filtered(A0 a0): B(a0), filter(*this), active(false) { }
+
+       template<typename A0, typename A1>
+       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
index e8e1f5358a344671100b9eddcc3bd5e95efe45b6..2ad83f87e9d3cde7a02c22b73033e32d7c113d43 100644 (file)
@@ -31,6 +31,7 @@ private:
        char       *buf_next;
 #endif
 
+protected:
        unsigned do_write(const char *, unsigned);
        unsigned do_read(char *, unsigned);
 };