From 9848c5aa28e04ccffaa825086591f0fb736e4778 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Wed, 23 Jan 2019 18:35:06 +0100 Subject: [PATCH] obs-source: Implement copy and move constructors and operators There was no proper implementation for all copy and move constructors and operators before, which meant that occasionally the default behavior took over. This is not what we want, as the default behavior doesn't deal well with reference counted pointers. Related: #32, #33 --- source/obs-source.cpp | 63 ++++++++++++++++++++++++++++++++----------- source/obs-source.hpp | 17 +++++++----- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/source/obs-source.cpp b/source/obs-source.cpp index 5b269e14..e0aeb8d9 100644 --- a/source/obs-source.cpp +++ b/source/obs-source.cpp @@ -456,29 +456,60 @@ obs::source::source(obs_source_t* source, bool track_ownership, bool add_referen connect_signals(); } -obs::source& obs::source::operator=(const source& ref) +obs::source::source(source const& other) { - if (this != &ref) { - if (self) { - if (track_ownership) { - obs_source_release(self); - } - } - self = ref.self; - track_ownership = ref.track_ownership; - if (track_ownership) { - obs_source_addref(self); - } + this->self = other.self; + + this->track_ownership = other.track_ownership; + if (this->track_ownership) { + obs_source_addref(this->self); } +} + +obs::source& obs::source::operator=(source const& other) +{ + if (this == &other) { + return *this; + } + + // Release previous source. + if (this->self && this->track_ownership) { + obs_source_release(this->self); + } + + this->self = other.self; + this->track_ownership = other.track_ownership; + if (this->track_ownership) { + obs_source_addref(this->self); + } + return *this; } -obs::source& obs::source::operator=(source&& ref) noexcept +obs::source::source(source&& other) { - if (this != &ref) { - self = ref.self; - ref.self = nullptr; + this->self = other.self; + this->track_ownership = other.track_ownership; + other.self = nullptr; + other.track_ownership = false; +} + +obs::source& obs::source::operator=(source&& other) +{ + if (this == &other) { + return *this; } + + // Release previous source. + if (this->self && this->track_ownership) { + obs_source_release(this->self); + } + + this->self = other.self; + this->track_ownership = other.track_ownership; + other.self = nullptr; + other.track_ownership = false; + return *this; } diff --git a/source/obs-source.hpp b/source/obs-source.hpp index cd9b0183..cb1f4e02 100644 --- a/source/obs-source.hpp +++ b/source/obs-source.hpp @@ -77,10 +77,15 @@ namespace obs { source(obs_source_t* source, bool track_ownership = true, bool add_reference = false); - source& operator=(const source& ref); + public /*copy*/: + source(source const& other); + source& operator=(source const& other); - source& operator=(source&& ref) noexcept; + public /*move*/: + source(source&& other); + source& operator=(source&& other); + public: obs_source_type type(); void* type_data(); @@ -108,17 +113,17 @@ namespace obs { util::event enable; - util::event push_to_mute_changed; + util::event push_to_mute_changed; util::event push_to_mute_delay; - util::event push_to_talk_changed; + util::event push_to_talk_changed; util::event push_to_talk_delay; util::event rename; - util::event update_properties; + util::event update_properties; util::event update_flags; - util::event mute; + util::event mute; util::event volume; util::event audio_sync; util::event audio_mixers;