diff options
Diffstat (limited to 'decoder.h')
-rw-r--r-- | decoder.h | 28 |
1 files changed, 21 insertions, 7 deletions
@@ -4,21 +4,39 @@ #include <boost/iostreams/concepts.hpp> #include <boost/iostreams/operations.hpp> #include <boost/shared_ptr.hpp> +#include <boost/function.hpp> #include <string> class DecoderBase { + friend class DecoderFilter; + + protected: + typedef boost::function<std::size_t (char*, std::size_t)> ReadFunc; + + virtual size_t decode(ReadFunc read, uint8_t *output, size_t output_size) = 0; + public: typedef boost::shared_ptr<DecoderBase> p; - virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; virtual ~DecoderBase() {} - }; //! Input filter to hold a decoder in a filter chain. class DecoderFilter : public boost::iostreams::multichar_input_filter { private: DecoderBase::p decoder; + + //! Functor binding a source to a read function. + template<class T> + struct ReadFunc { + T& s; + + ReadFunc(T& s_) : s(s_) {} + + std::size_t operator()(char* buf, std::size_t buf_size) { + return boost::iostreams::read(s, buf, buf_size); + } + }; public: typedef boost::shared_ptr<DecoderFilter> p; @@ -26,11 +44,7 @@ class DecoderFilter : public boost::iostreams::multichar_input_filter { template<typename Source> std::streamsize read(Source& src, char *s, std::streamsize n) { - char src_data[0x2000]; - std::streamsize src_read = boost::iostreams::read(src, src_data, 0x2000); - if(src_read < 0) - src_read = 0; - return decoder->decode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + return decoder->decode(ReadFunc<Source>(src), (uint8_t*)s, n); } }; |