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