From 1c8f804a4ce2511039681ae304dd6c1a8da31178 Mon Sep 17 00:00:00 2001 From: Curve Date: Wed, 11 Oct 2023 18:54:31 +0200 Subject: [PATCH] refactor: return `nullptr` if creation fails --- include/rohrkabel/core/core.hpp | 5 ++++- include/rohrkabel/registry/registry.hpp | 5 ++++- src/context.cpp | 16 ++++++++----- src/core.cpp | 30 +++++++++++++++++-------- src/loop.cpp | 28 ++++++++++++++--------- src/registry.cpp | 28 ++++++++++++++++------- 6 files changed, 77 insertions(+), 35 deletions(-) diff --git a/include/rohrkabel/core/core.hpp b/include/rohrkabel/core/core.hpp index e14b977..9d43a32 100644 --- a/include/rohrkabel/core/core.hpp +++ b/include/rohrkabel/core/core.hpp @@ -43,7 +43,7 @@ namespace pipewire ~core(); private: - core(std::shared_ptr); + core(); private: void *create(factory) const; @@ -80,6 +80,9 @@ namespace pipewire public: [[nodiscard]] operator pw_core *() const &; [[nodiscard]] operator pw_core *() const && = delete; + + private: + [[nodiscard]] static std::shared_ptr create(std::shared_ptr); }; template <> diff --git a/include/rohrkabel/registry/registry.hpp b/include/rohrkabel/registry/registry.hpp index a280d42..cf0fa5f 100644 --- a/include/rohrkabel/registry/registry.hpp +++ b/include/rohrkabel/registry/registry.hpp @@ -26,7 +26,7 @@ namespace pipewire ~registry(); protected: - registry(std::shared_ptr); + registry(); public: template @@ -44,6 +44,9 @@ namespace pipewire public: [[nodiscard]] operator pw_registry *() const &; [[nodiscard]] operator pw_registry *() const && = delete; + + private: + [[nodiscard]] static std::shared_ptr create(std::shared_ptr); }; template <> diff --git a/src/context.cpp b/src/context.cpp index 83ba2fd..af60b2c 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -1,7 +1,7 @@ #include "loop.hpp" #include "context.hpp" #include "core/core.hpp" -#include "utils/assert.hpp" +#include "utils/check.hpp" #include @@ -25,7 +25,7 @@ namespace pipewire { if (!m_impl->core) { - m_impl->core = std::shared_ptr(new pipewire::core{shared_from_this()}); + m_impl->core = core::create(shared_from_this()); } return m_impl->core; @@ -48,13 +48,19 @@ namespace pipewire std::shared_ptr context::create(std::shared_ptr 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(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 diff --git a/src/core.cpp b/src/core.cpp index 79aab40..9bd0b3f 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -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" @@ -22,13 +22,7 @@ namespace pipewire pw_core_disconnect(m_impl->core); } - core::core(std::shared_ptr context) : m_impl(std::make_unique()) - { - 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()) {} void *core::create(factory factory) const { @@ -123,7 +117,7 @@ namespace pipewire { if (!m_impl->registry) { - m_impl->registry = std::shared_ptr(new pipewire::registry{shared_from_this()}); + m_impl->registry = pipewire::registry::create(shared_from_this()); } return m_impl->registry; @@ -143,4 +137,22 @@ namespace pipewire { return get(); } + + std::shared_ptr core::create(std::shared_ptr 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(new pipewire::core); + + rtn->m_impl->core = core; + rtn->m_impl->context = std::move(context); + + return rtn; + } } // namespace pipewire diff --git a/src/loop.cpp b/src/loop.cpp index 7ef3fbe..eb42b8b 100644 --- a/src/loop.cpp +++ b/src/loop.cpp @@ -1,5 +1,8 @@ #include "loop.hpp" -#include "utils/assert.hpp" +#include "utils/check.hpp" + +#include +#include #include @@ -15,11 +18,7 @@ namespace pipewire pw_main_loop_destroy(m_impl->main_loop); } - main_loop::main_loop() : m_impl(std::make_unique()) - { - 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()) {} void main_loop::quit() const { @@ -48,14 +47,21 @@ namespace pipewire std::shared_ptr 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(new main_loop); + auto rtn = std::unique_ptr(new main_loop); + + rtn->m_impl->main_loop = loop; + + return rtn; } } // namespace pipewire diff --git a/src/registry.cpp b/src/registry.cpp index 2614f29..6ee0ef4 100644 --- a/src/registry.cpp +++ b/src/registry.cpp @@ -1,5 +1,5 @@ #include "registry/registry.hpp" -#include "utils/assert.hpp" +#include "utils/check.hpp" #include @@ -21,13 +21,7 @@ namespace pipewire pw_proxy_destroy(reinterpret_cast(m_impl->registry)); } - registry::registry(std::shared_ptr core) : m_impl(std::make_unique()) - { - 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()) {} template <> registry_listener registry::listen() @@ -48,4 +42,22 @@ namespace pipewire { return get(); } + + std::shared_ptr registry::create(std::shared_ptr 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(new pipewire::registry); + + rtn->m_impl->registry = registry; + rtn->m_impl->core = std::move(core); + + return rtn; + } } // namespace pipewire