X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffiltered.h;fp=source%2Ffiltered.h;h=057517547c5ea7aa9afdfd80a9f78ff2292bfa75;hp=0000000000000000000000000000000000000000;hb=dd318ef67b0c683c8306da0abf806e99cff346e9;hpb=a1375220342e0fc87c8c7065059dc8d49633f95b 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