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
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-01-23 18:35:06 +01:00
parent f910312a2d
commit 9848c5aa28
2 changed files with 58 additions and 22 deletions

View file

@ -456,29 +456,60 @@ obs::source::source(obs_source_t* source, bool track_ownership, bool add_referen
connect_signals(); connect_signals();
} }
obs::source& obs::source::operator=(const source& ref) obs::source::source(source const& other)
{ {
if (this != &ref) { this->self = other.self;
if (self) {
if (track_ownership) { this->track_ownership = other.track_ownership;
obs_source_release(self); if (this->track_ownership) {
} obs_source_addref(this->self);
}
self = ref.self;
track_ownership = ref.track_ownership;
if (track_ownership) {
obs_source_addref(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; return *this;
} }
obs::source& obs::source::operator=(source&& ref) noexcept obs::source::source(source&& other)
{ {
if (this != &ref) { this->self = other.self;
self = ref.self; this->track_ownership = other.track_ownership;
ref.self = nullptr; 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; return *this;
} }

View file

@ -77,10 +77,15 @@ namespace obs {
source(obs_source_t* source, bool track_ownership = true, bool add_reference = false); 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(); obs_source_type type();
void* type_data(); void* type_data();
@ -108,17 +113,17 @@ namespace obs {
util::event<obs::source*, bool> enable; util::event<obs::source*, bool> enable;
util::event<obs::source*, bool> push_to_mute_changed; util::event<obs::source*, bool> push_to_mute_changed;
util::event<obs::source*, long long> push_to_mute_delay; util::event<obs::source*, long long> push_to_mute_delay;
util::event<obs::source*, bool> push_to_talk_changed; util::event<obs::source*, bool> push_to_talk_changed;
util::event<obs::source*, long long> push_to_talk_delay; util::event<obs::source*, long long> push_to_talk_delay;
util::event<obs::source*, std::string, std::string> rename; util::event<obs::source*, std::string, std::string> rename;
util::event<obs::source*> update_properties; util::event<obs::source*> update_properties;
util::event<obs::source*, long long> update_flags; util::event<obs::source*, long long> update_flags;
util::event<obs::source*, bool> mute; util::event<obs::source*, bool> mute;
util::event<obs::source*, double&> volume; util::event<obs::source*, double&> volume;
util::event<obs::source*, long long&> audio_sync; util::event<obs::source*, long long&> audio_sync;
util::event<obs::source*, long long&> audio_mixers; util::event<obs::source*, long long&> audio_mixers;