EncoderFactory returns a unique_ptr

This commit is contained in:
badaix 2019-12-29 21:06:02 +01:00
parent e9a5a36855
commit 3b9c2db79b
2 changed files with 8 additions and 22 deletions

View file

@ -37,9 +37,8 @@ using namespace std;
namespace encoder namespace encoder
{ {
Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const std::unique_ptr<Encoder> EncoderFactory::createEncoder(const std::string& codecSettings) const
{ {
Encoder* encoder;
std::string codec(codecSettings); std::string codec(codecSettings);
std::string codecOptions; std::string codecOptions;
if (codec.find(":") != std::string::npos) if (codec.find(":") != std::string::npos)
@ -48,35 +47,21 @@ Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
codec = utils::string::trim_copy(codec.substr(0, codec.find(":"))); codec = utils::string::trim_copy(codec.substr(0, codec.find(":")));
} }
if (codec == "pcm") if (codec == "pcm")
encoder = new PcmEncoder(codecOptions); return std::make_unique<PcmEncoder>(codecOptions);
#if defined(HAS_OGG) && defined(HAS_VORBIS) && defined(HAS_VORBIS_ENC) #if defined(HAS_OGG) && defined(HAS_VORBIS) && defined(HAS_VORBIS_ENC)
else if (codec == "ogg") else if (codec == "ogg")
encoder = new OggEncoder(codecOptions); return std::make_unique<OggEncoder>(codecOptions);
#endif #endif
#if defined(HAS_FLAC) #if defined(HAS_FLAC)
else if (codec == "flac") else if (codec == "flac")
encoder = new FlacEncoder(codecOptions); return std::make_unique<FlacEncoder>(codecOptions);
#endif #endif
#if defined(HAS_OPUS) #if defined(HAS_OPUS)
else if (codec == "opus") else if (codec == "opus")
encoder = new OpusEncoder(codecOptions); return std::make_unique<OpusEncoder>(codecOptions);
#endif #endif
else
{
throw SnapException("unknown codec: " + codec);
}
return encoder; throw SnapException("unknown codec: " + codec);
/* try
{
encoder->init(NULL, format, codecOptions);
}
catch (const std::exception& e)
{
cout << "Error: " << e.what() << "\n";
return 1;
}
*/
} }
} // namespace encoder } // namespace encoder

View file

@ -2,6 +2,7 @@
#define ENCODER_FACTORY_H #define ENCODER_FACTORY_H
#include "encoder.hpp" #include "encoder.hpp"
#include <memory>
#include <string> #include <string>
namespace encoder namespace encoder
@ -11,7 +12,7 @@ class EncoderFactory
{ {
public: public:
// EncoderFactory(const std::string& codecSettings); // EncoderFactory(const std::string& codecSettings);
Encoder* createEncoder(const std::string& codecSettings) const; std::unique_ptr<Encoder> createEncoder(const std::string& codecSettings) const;
}; };
} // namespace encoder } // namespace encoder