]> git.tdb.fi Git - libs/core.git/blob - source/filtered.h
Add put, getline and get to Filtered to make use of possible optimizations in the...
[libs/core.git] / source / filtered.h
1 /* $Id$
2
3 This file is part of libmspio
4 Copyright © 2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_IO_FILTERED_H_
9 #define MSP_IO_FILTERED_H_
10
11 namespace Msp {
12 namespace IO {
13
14 template<typename B, typename F>
15 class Filtered: public B
16 {
17 private:
18         struct Activator
19         {
20                 Filtered &f;
21
22                 Activator(Filtered &f_): f(f_) { f.active=true; }
23                 ~Activator() { f.active=false; }
24         };
25
26         F filter;
27         bool active;
28
29 public:
30         Filtered(): filter(*this), active(false) { }
31         ~Filtered() { active=true; }
32
33         template<typename A0>
34         Filtered(A0 a0): B(a0), filter(*this), active(false) { }
35
36         template<typename A0, typename A1>
37         Filtered(A0 a0, A1 a1): B(a0, a1), filter(*this), active(false) { }
38
39         virtual unsigned put(char c) { return filter.put(c); }
40         virtual bool getline(std::string &l) { return filter.getline(l); }
41         virtual int get() { return filter.get(); }
42
43         F &get_filter() { return filter; }
44 protected:
45         virtual unsigned do_write(const char *b, unsigned s)
46         {
47                 if(!active)
48                 {
49                         Activator a(*this);
50                         return filter.write(b, s);
51                 }
52                 else
53                         return B::do_write(b, s);
54         }
55
56         virtual unsigned do_read(char *b, unsigned s)
57         {
58                 if(!active)
59                 {
60                         Activator a(*this);
61                         return filter.read(b, s);
62                 }
63                 else
64                         return B::do_read(b, s);
65         }
66 };
67
68 } // namespace IO
69 } // namespace Msp
70
71 #endif