From e755f01e631e832e3ef529049888d62af38d2b38 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 6 Jan 2011 03:24:28 +0100 Subject: Change decoder/encoder API to provide decode/encode functions with a read callback function. --- encoder.h | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'encoder.h') diff --git a/encoder.h b/encoder.h index f68d51c..d000ffc 100644 --- a/encoder.h +++ b/encoder.h @@ -4,23 +4,40 @@ #include #include #include +#include #include #include class EncoderBase { + friend class EncoderFilter; + + protected: + typedef boost::function ReadFunc; + + virtual size_t encode(ReadFunc read, uint8_t *output, size_t output_size) = 0; + public: typedef boost::shared_ptr p; virtual ~EncoderBase() {} - - virtual size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; - virtual size_t flush(uint8_t *output, size_t output_size) = 0; }; //! Input filter to hold an encoder in a filter chain. class EncoderFilter : public boost::iostreams::multichar_input_filter { private: EncoderBase::p encoder; + + //! Functor binding a source to a read function. + template + 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 p; @@ -28,16 +45,7 @@ class EncoderFilter : public boost::iostreams::multichar_input_filter { template 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; - std::streamsize size = encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); - // no more data, flush encoder - if(src_read == 0 && size == 0) { - size = encoder->flush((uint8_t*)s, n); - } - return size; + return encoder->encode(ReadFunc(src), (uint8_t*)s, n); }; }; -- cgit v1.2.3