object.cpp 1.83 KB
Newer Older
1 2
/*
 * object.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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.
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20
#include "logger/logger.h"
21 22 23 24 25 26
#include "object-p.h"

#include "object.h"

// =============================================================================

27 28
#define GET_SHARED_FROM_THIS_FATAL_ERROR "Object was not created with `ObjectFactory::create`."

29 30
using namespace std;

31 32
LINPHONE_BEGIN_NAMESPACE

33 34 35 36
Object::Object (ObjectPrivate &p) : mPrivate(&p) {
	mPrivate->mPublic = this;
}

37 38 39 40
Object::~Object () {
	delete mPrivate;
}

41
shared_ptr<Object> Object::getSharedFromThis () {
42
	return const_pointer_cast<Object>(static_cast<const Object *>(this)->getSharedFromThis());
43 44 45
}

shared_ptr<const Object> Object::getSharedFromThis () const {
46 47 48 49 50 51 52 53 54 55 56
	shared_ptr<const Object> object;

	try {
		object = mPrivate->weak.lock();
		if (!object)
			lFatal() << GET_SHARED_FROM_THIS_FATAL_ERROR;
	} catch (const exception &) {
		lFatal() << GET_SHARED_FROM_THIS_FATAL_ERROR;
	}

	return object;
57 58 59 60 61 62 63
}

void ObjectFactory::setPublic (const shared_ptr<Object> &object) {
	L_ASSERT(object);
	ObjectPrivate *d = object->getPrivate();
	d->mPublic = object.get();
	d->weak = object;
64
}
65 66

LINPHONE_END_NAMESPACE