diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-01-06 03:24:28 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-01-06 03:24:28 +0100 |
commit | e755f01e631e832e3ef529049888d62af38d2b38 (patch) | |
tree | 1f2b8c8b888157cdd4c598541ebb8f26e9cc078f /encoder.h | |
parent | 70f111b184928feab0c94f762954b5ec83a441c6 (diff) |
Change decoder/encoder API to provide decode/encode functions with a read callback function.
Diffstat (limited to 'encoder.h')
-rw-r--r-- | encoder.h | 34 |
1 files changed, 21 insertions, 13 deletions
@@ -4,23 +4,40 @@ #include <boost/iostreams/concepts.hpp> #include <boost/iostreams/operations.hpp> #include <boost/shared_ptr.hpp> +#include <boost/function.hpp> #include <iostream> #include <string> class EncoderBase { + friend class EncoderFilter; + + protected: + typedef boost::function<std::size_t (char*, std::size_t)> ReadFunc; + + virtual size_t encode(ReadFunc read, uint8_t *output, size_t output_size) = 0; + public: typedef boost::shared_ptr<EncoderBase> 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<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<EncoderFilter> p; @@ -28,16 +45,7 @@ class EncoderFilter : 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; - 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<Source>(src), (uint8_t*)s, n); }; }; |