mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +00:00
LibAudio: Switch LoaderPlugin to a more traditional constructor pattern
This now prepares all the needed (fallible) components before actually constructing a LoaderPlugin object, so we are no longer filling them in at an arbitrary later point in time.
This commit is contained in:
parent
3cf93d0dd2
commit
c57be0f474
12 changed files with 132 additions and 86 deletions
|
@ -11,26 +11,11 @@
|
|||
|
||||
namespace Audio {
|
||||
|
||||
LoaderPlugin::LoaderPlugin(StringView path)
|
||||
: m_path(path)
|
||||
LoaderPlugin::LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
LoaderPlugin::LoaderPlugin(Bytes buffer)
|
||||
: m_backing_memory(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
MaybeLoaderError LoaderPlugin::initialize()
|
||||
{
|
||||
if (m_backing_memory.has_value())
|
||||
m_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(m_backing_memory.value()));
|
||||
else
|
||||
m_stream = LOADER_TRY(Core::Stream::BufferedFile::create(LOADER_TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Read))));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
|
||||
: m_plugin(move(plugin))
|
||||
{
|
||||
|
@ -38,35 +23,47 @@ Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
|
|||
|
||||
Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(StringView path)
|
||||
{
|
||||
NonnullOwnPtr<LoaderPlugin> plugin = adopt_own(*new WavLoaderPlugin(path));
|
||||
auto initstate0 = plugin->initialize();
|
||||
if (!initstate0.is_error())
|
||||
return plugin;
|
||||
{
|
||||
auto plugin = WavLoaderPlugin::try_create(path);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
plugin = adopt_own(*new FlacLoaderPlugin(path));
|
||||
auto initstate1 = plugin->initialize();
|
||||
if (!initstate1.is_error())
|
||||
return plugin;
|
||||
{
|
||||
auto plugin = FlacLoaderPlugin::try_create(path);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
plugin = adopt_own(*new MP3LoaderPlugin(path));
|
||||
auto initstate2 = plugin->initialize();
|
||||
if (!initstate2.is_error())
|
||||
return plugin;
|
||||
{
|
||||
auto plugin = MP3LoaderPlugin::try_create(path);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
return LoaderError { "No loader plugin available" };
|
||||
}
|
||||
|
||||
Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(Bytes buffer)
|
||||
{
|
||||
NonnullOwnPtr<LoaderPlugin> plugin = adopt_own(*new WavLoaderPlugin(buffer));
|
||||
if (auto initstate = plugin->initialize(); !initstate.is_error())
|
||||
return plugin;
|
||||
plugin = adopt_own(*new FlacLoaderPlugin(buffer));
|
||||
if (auto initstate = plugin->initialize(); !initstate.is_error())
|
||||
return plugin;
|
||||
plugin = adopt_own(*new MP3LoaderPlugin(buffer));
|
||||
if (auto initstate = plugin->initialize(); !initstate.is_error())
|
||||
return plugin;
|
||||
{
|
||||
auto plugin = WavLoaderPlugin::try_create(buffer);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
{
|
||||
auto plugin = FlacLoaderPlugin::try_create(buffer);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
{
|
||||
auto plugin = MP3LoaderPlugin::try_create(buffer);
|
||||
if (!plugin.is_error())
|
||||
return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
||||
}
|
||||
|
||||
return LoaderError { "No loader plugin available" };
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue