forked from mirrors/linphone-iphone
88 lines
2.4 KiB
C++
88 lines
2.4 KiB
C++
/*
|
|
* clonable-object.cpp
|
|
* Copyright (C) 2010-2017 Belledonne Communications SARL
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include "clonable-object-p.h"
|
|
|
|
#include "clonable-object.h"
|
|
|
|
// =============================================================================
|
|
|
|
using namespace std;
|
|
|
|
LINPHONE_BEGIN_NAMESPACE
|
|
|
|
// TODO: Use atomic counter?
|
|
|
|
void ClonableObjectPrivate::ref () {
|
|
++nRefs;
|
|
}
|
|
|
|
void ClonableObjectPrivate::unref () {
|
|
if (--nRefs == 0) {
|
|
delete mPublic;
|
|
delete this;
|
|
}
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
L_OBJECT_IMPL(ClonableObject);
|
|
|
|
ClonableObject::ClonableObject (ClonableObjectPrivate &p) : mPrivate(&p) {
|
|
// Q-pointer must be empty. It's a constructor that takes a new private data.
|
|
L_ASSERT(!mPrivate->mPublic);
|
|
|
|
mPrivate->mPublic = new remove_pointer<decltype(mPrivate->mPublic)>::type();
|
|
(*mPrivate->mPublic)[mPrivate] = this;
|
|
mPrivate->ref();
|
|
}
|
|
|
|
ClonableObject::ClonableObject (const ClonableObjectPrivate &p) {
|
|
// Cannot access to Q-pointer. It's a copy constructor from private data.
|
|
L_ASSERT(!mPrivate);
|
|
|
|
setRef(p);
|
|
}
|
|
|
|
ClonableObject::~ClonableObject () {
|
|
mPrivate->mPublic->erase(mPrivate);
|
|
mPrivate->unref();
|
|
}
|
|
|
|
void ClonableObject::setRef (const ClonableObjectPrivate &p) {
|
|
// Q-pointer must exist if private data is defined.
|
|
L_ASSERT(!mPrivate || mPrivate->mPublic);
|
|
|
|
// Nothing, same reference.
|
|
if (&p == mPrivate)
|
|
return;
|
|
|
|
// Unref previous private data.
|
|
if (mPrivate) {
|
|
mPrivate->mPublic->erase(mPrivate);
|
|
mPrivate->unref();
|
|
}
|
|
|
|
// Add and reference new private data.
|
|
mPrivate = const_cast<ClonableObjectPrivate *>(&p);
|
|
(*mPrivate->mPublic)[mPrivate] = this;
|
|
mPrivate->ref();
|
|
}
|
|
|
|
LINPHONE_END_NAMESPACE
|