diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-01-03 20:50:59 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-01-03 20:50:59 +0100 |
commit | 3a3bab26aea5c064c07c307d1e8ccf463edae568 (patch) | |
tree | 8789f9cbec352268858f5c9c6a1d8e2b270a3d76 | |
parent | b3c30578ae1709feb978b72d756d88c17941cad4 (diff) |
Decoder, encoder and transcoder-related classes and factories now uses boost::shared_ptr.
-rw-r--r-- | decoder.cpp | 19 | ||||
-rw-r--r-- | decoder.h | 13 | ||||
-rw-r--r-- | encoder.cpp | 19 | ||||
-rw-r--r-- | encoder.h | 11 | ||||
-rw-r--r-- | music.cpp | 8 | ||||
-rw-r--r-- | transcode.cpp | 6 | ||||
-rw-r--r-- | transcode.h | 6 |
7 files changed, 44 insertions, 38 deletions
diff --git a/decoder.cpp b/decoder.cpp index e80c714..cffb8cf 100644 --- a/decoder.cpp +++ b/decoder.cpp @@ -3,17 +3,18 @@ #include <map> -namespace decoder { - typedef boost::function<DecoderBase*()> DecoderFactory; - std::map<std::string, DecoderFactory> decoder_factories; +DecoderFilter::DecoderFilter(DecoderBase::p decoder_) { + decoder = decoder_; +} + +typedef boost::function<boost::shared_ptr<DecoderBase>()> DecoderFactory; +std::map<std::string, DecoderFactory> decoder_factories; -void init() { +void decoder::init() { mpg123_init(); // initialize the mpg123 library - decoder_factories["mpg123"] = boost::factory<DecoderMpg123*>(); + decoder_factories["mpg123"] = boost::factory<boost::shared_ptr<DecoderMpg123> >(); } -DecoderBase *get_decoder(const std::string& name) { - return decoder_factories[name](); +DecoderFilter::p decoder::get_decoder(const std::string& name) { + return DecoderFilter::p(new DecoderFilter(DecoderBase::p(decoder_factories[name]()))); } - -}; @@ -7,32 +7,35 @@ #include <boost/functional/value_factory.hpp> #include <boost/iostreams/concepts.hpp> #include <boost/iostreams/operations.hpp> +#include <boost/shared_ptr.hpp> #include <string> class DecoderBase { 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; }; class DecoderFilter : public boost::iostreams::multichar_input_filter { private: - DecoderBase& decoder; + DecoderBase::p decoder; public: - DecoderFilter(DecoderBase& decoder_) : decoder(decoder_) {}; + typedef boost::shared_ptr<DecoderFilter> p; + DecoderFilter(DecoderBase::p decoder_); 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((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + } }; namespace decoder { void init(); - DecoderBase *get_decoder(const std::string& name); + DecoderFilter::p get_decoder(const std::string& name); }; #endif diff --git a/encoder.cpp b/encoder.cpp index 9bea59b..d0566fb 100644 --- a/encoder.cpp +++ b/encoder.cpp @@ -3,16 +3,17 @@ #include <map> -namespace encoder { - typedef boost::function<EncoderBase*()> EncoderFactory; - std::map<std::string, EncoderFactory> encoder_factories; - -void init() { - encoder_factories["lame"] = boost::factory<EncoderLame*>(); +EncoderFilter::EncoderFilter(EncoderBase::p encoder_) { + encoder = encoder_; } -EncoderBase *get_encoder(const std::string& name) { - return encoder_factories[name](); +typedef boost::function<boost::shared_ptr<EncoderBase>()> EncoderFactory; +std::map<std::string, EncoderFactory> encoder_factories; + +void encoder::init() { + encoder_factories["lame"] = boost::factory<boost::shared_ptr<EncoderLame> >(); } -}; +EncoderFilter::p encoder::get_encoder(const std::string& name) { + return EncoderFilter::p(new EncoderFilter(EncoderBase::p(encoder_factories[name]()))); +} @@ -7,33 +7,36 @@ #include <boost/functional/value_factory.hpp> #include <boost/iostreams/concepts.hpp> #include <boost/iostreams/operations.hpp> +#include <boost/shared_ptr.hpp> #include <string> class EncoderBase { public: + typedef boost::shared_ptr<EncoderBase> p; 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; }; class EncoderFilter : public boost::iostreams::multichar_input_filter { private: - EncoderBase& encoder; + EncoderBase::p encoder; public: - EncoderFilter(EncoderBase& encoder_) : encoder(encoder_) {}; + typedef boost::shared_ptr<EncoderFilter> p; + EncoderFilter(EncoderBase::p encoder_); 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 encoder.encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + return encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); }; }; namespace encoder { void init(); - EncoderBase *get_encoder(const std::string& name); + EncoderFilter::p get_encoder(const std::string& name); }; #endif @@ -133,12 +133,10 @@ void MusicTrack::render(HTTP::Connection::p req) { delete t; if(req->args.count("decoder") && req->args.count("encoder")) { - DecoderBase *d = decoder::get_decoder(req->args["decoder"]); - EncoderBase *e = encoder::get_encoder(req->args["encoder"]); - Transcoder t(path.string(), req, *d, *e); + DecoderFilter::p d = decoder::get_decoder(req->args["decoder"]); + EncoderFilter::p e = encoder::get_encoder(req->args["encoder"]); + Transcoder t(path.string(), req, d, e); t.run(); - delete d; - delete e; } else { fs::ifstream is(path, std::ios::in | std::ios::binary); is.seekg(0, std::ios::end); diff --git a/transcode.cpp b/transcode.cpp index f3698a3..394f604 100644 --- a/transcode.cpp +++ b/transcode.cpp @@ -3,15 +3,15 @@ #include <boost/iostreams/device/file.hpp> #include <boost/iostreams/filtering_stream.hpp> -Transcoder::Transcoder(std::string p, HTTP::Connection::p r, DecoderBase& d, EncoderBase& e) : path(p), res(r), decoder(d), encoder(e) { +Transcoder::Transcoder(std::string p, HTTP::Connection::p r, DecoderFilter::p d, EncoderFilter::p e) : path(p), res(r), decoder(d), encoder(e) { } void Transcoder::run() { const std::streamsize buffer_size = 0x1000; boost::iostreams::file_source is(path, std::ios::in | std::ios::binary); boost::iostreams::filtering_istream s; - s.push(EncoderFilter(encoder), buffer_size); - s.push(DecoderFilter(decoder), buffer_size); + s.push(*encoder.get(), buffer_size); + s.push(*decoder.get(), buffer_size); s.push(is, buffer_size); res->send_data(s); diff --git a/transcode.h b/transcode.h index 70d0113..56f16c1 100644 --- a/transcode.h +++ b/transcode.h @@ -11,11 +11,11 @@ class Transcoder { private: std::string path; HTTP::Connection::p res; - DecoderBase& decoder; - EncoderBase& encoder; + DecoderFilter::p decoder; + EncoderFilter::p encoder; public: - Transcoder(std::string p, HTTP::Connection::p r, DecoderBase& d, EncoderBase& e); + Transcoder(std::string p, HTTP::Connection::p r, DecoderFilter::p d, EncoderFilter::p e); void run(); }; |