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();
}
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;
}
obs::source& obs::source::operator=(source&& ref) noexcept
{
if (this != &ref) {
self = ref.self;
ref.self = nullptr;
// 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::source(source&& other)
{
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;
}

View File

@ -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();