Skip to content

Commit

Permalink
refactor: return nullptr if creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 11, 2023
1 parent 74e786c commit 1c8f804
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 35 deletions.
5 changes: 4 additions & 1 deletion include/rohrkabel/core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace pipewire
~core();

private:
core(std::shared_ptr<pipewire::context>);
core();

private:
void *create(factory) const;
Expand Down Expand Up @@ -80,6 +80,9 @@ namespace pipewire
public:
[[nodiscard]] operator pw_core *() const &;
[[nodiscard]] operator pw_core *() const && = delete;

private:
[[nodiscard]] static std::shared_ptr<core> create(std::shared_ptr<pipewire::context>);
};

template <>
Expand Down
5 changes: 4 additions & 1 deletion include/rohrkabel/registry/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace pipewire
~registry();

protected:
registry(std::shared_ptr<core>);
registry();

public:
template <class Listener = registry_listener>
Expand All @@ -44,6 +44,9 @@ namespace pipewire
public:
[[nodiscard]] operator pw_registry *() const &;
[[nodiscard]] operator pw_registry *() const && = delete;

private:
[[nodiscard]] static std::shared_ptr<registry> create(std::shared_ptr<pipewire::core>);
};

template <>
Expand Down
16 changes: 11 additions & 5 deletions src/context.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "loop.hpp"
#include "context.hpp"
#include "core/core.hpp"
#include "utils/assert.hpp"
#include "utils/check.hpp"

#include <pipewire/pipewire.h>

Expand All @@ -25,7 +25,7 @@ namespace pipewire
{
if (!m_impl->core)
{
m_impl->core = std::shared_ptr<pipewire::core>(new pipewire::core{shared_from_this()});
m_impl->core = core::create(shared_from_this());
}

return m_impl->core;
Expand All @@ -48,13 +48,19 @@ namespace pipewire

std::shared_ptr<context> context::create(std::shared_ptr<main_loop> loop)
{
auto *ctx = pw_context_new(loop->loop(), nullptr, 0);
check(ctx, "Failed to create context");

if (!ctx)
{
return nullptr;
}

auto rtn = std::unique_ptr<context>(new context);

rtn->m_impl->context = pw_context_new(loop->loop(), nullptr, 0);
rtn->m_impl->context = ctx;
rtn->m_impl->loop = std::move(loop);

check(rtn->m_impl->context, "Failed to create context");

return rtn;
}
} // namespace pipewire
30 changes: 21 additions & 9 deletions src/core.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "core/core.hpp"
#include "core/events.hpp"
#include "utils/assert.hpp"
#include "utils/check.hpp"
#include "registry/registry.hpp"

#include "proxy.hpp"
Expand All @@ -22,13 +22,7 @@ namespace pipewire
pw_core_disconnect(m_impl->core);
}

core::core(std::shared_ptr<pipewire::context> context) : m_impl(std::make_unique<impl>())
{
m_impl->core = pw_context_connect(context->get(), nullptr, 0);
m_impl->context = std::move(context);

check(m_impl->core, "Failed to connect core");
}
core::core() : m_impl(std::make_unique<impl>()) {}

void *core::create(factory factory) const
{
Expand Down Expand Up @@ -123,7 +117,7 @@ namespace pipewire
{
if (!m_impl->registry)
{
m_impl->registry = std::shared_ptr<pipewire::registry>(new pipewire::registry{shared_from_this()});
m_impl->registry = pipewire::registry::create(shared_from_this());
}

return m_impl->registry;
Expand All @@ -143,4 +137,22 @@ namespace pipewire
{
return get();
}

std::shared_ptr<core> core::create(std::shared_ptr<pipewire::context> context)
{
auto *core = pw_context_connect(context->get(), nullptr, 0);
check(core, "Failed to connect core");

if (!core)
{
return nullptr;
}

auto rtn = std::unique_ptr<pipewire::core>(new pipewire::core);

rtn->m_impl->core = core;
rtn->m_impl->context = std::move(context);

return rtn;
}
} // namespace pipewire
28 changes: 17 additions & 11 deletions src/loop.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "loop.hpp"
#include "utils/assert.hpp"
#include "utils/check.hpp"

#include <mutex>
#include <memory>

#include <pipewire/pipewire.h>

Expand All @@ -15,11 +18,7 @@ namespace pipewire
pw_main_loop_destroy(m_impl->main_loop);
}

main_loop::main_loop() : m_impl(std::make_unique<impl>())
{
m_impl->main_loop = pw_main_loop_new(nullptr);
check(m_impl->main_loop, "Failed to create main_loop");
}
main_loop::main_loop() : m_impl(std::make_unique<impl>()) {}

void main_loop::quit() const
{
Expand Down Expand Up @@ -48,14 +47,21 @@ namespace pipewire

std::shared_ptr<main_loop> main_loop::create()
{
static auto once = false;
static std::once_flag flag;
std::call_once(flag, [] { pw_init(nullptr, nullptr); });

if (!once)
auto *loop = pw_main_loop_new(nullptr);
check(loop, "Failed to create main_loop");

if (!loop)
{
pw_init(nullptr, nullptr);
once = true;
return nullptr;
}

return std::unique_ptr<main_loop>(new main_loop);
auto rtn = std::unique_ptr<main_loop>(new main_loop);

rtn->m_impl->main_loop = loop;

return rtn;
}
} // namespace pipewire
28 changes: 20 additions & 8 deletions src/registry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "registry/registry.hpp"
#include "utils/assert.hpp"
#include "utils/check.hpp"

#include <pipewire/pipewire.h>

Expand All @@ -21,13 +21,7 @@ namespace pipewire
pw_proxy_destroy(reinterpret_cast<pw_proxy *>(m_impl->registry));
}

registry::registry(std::shared_ptr<pipewire::core> core) : m_impl(std::make_unique<impl>())
{
m_impl->registry = pw_core_get_registry(core->get(), PW_VERSION_REGISTRY, 0);
m_impl->core = std::move(core);

check(m_impl->registry, "Failed to get registry");
}
registry::registry() : m_impl(std::make_unique<impl>()) {}

template <>
registry_listener registry::listen()
Expand All @@ -48,4 +42,22 @@ namespace pipewire
{
return get();
}

std::shared_ptr<registry> registry::create(std::shared_ptr<pipewire::core> core)
{
auto *registry = pw_core_get_registry(core->get(), PW_VERSION_REGISTRY, 0);
check(registry, "Failed to get registry");

if (!registry)
{
return nullptr;
}

auto rtn = std::unique_ptr<pipewire::registry>(new pipewire::registry);

rtn->m_impl->registry = registry;
rtn->m_impl->core = std::move(core);

return rtn;
}
} // namespace pipewire

0 comments on commit 1c8f804

Please sign in to comment.