Commit 6caf117b authored by Ronan's avatar Ronan

feat(Object): check weak ptr when `getSharedFromThis` is called

parent 459d54c7
......@@ -16,12 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logger/logger.h"
#include "object-p.h"
#include "object.h"
// =============================================================================
#define GET_SHARED_FROM_THIS_FATAL_ERROR "Object was not created with `ObjectFactory::create`."
using namespace std;
LINPHONE_BEGIN_NAMESPACE
......@@ -33,11 +36,21 @@ Object::~Object () {
Object::Object (ObjectPrivate &p) : mPrivate(&p) {}
shared_ptr<Object> Object::getSharedFromThis () {
return mPrivate->weak.lock();
return const_pointer_cast<Object>(static_cast<const Object *>(this)->getSharedFromThis());
}
shared_ptr<const Object> Object::getSharedFromThis () const {
return mPrivate->weak.lock();
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;
}
void ObjectFactory::setPublic (const shared_ptr<Object> &object) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment