From 6489961f38e4ad49833b425b0316c29f1a717d31 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Mon, 27 Jan 2014 20:09:25 +0000 Subject: NO-JIRA: Minor refactor to improve code safety: calling shared_from_this on creation. Previous anti-pattern: Classes need to call shared_from_this during creation, but can't call it in the ctor so had a separate initiailize function that the user was required to call immediately after the constructor. Possible for user to forget. Improved pattern: Introduce public static create() functions to call constructor and initialize, make constructor and initialize private. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1561828 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/ha/QueueReplicator.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'cpp/src/qpid/ha/QueueReplicator.cpp') diff --git a/cpp/src/qpid/ha/QueueReplicator.cpp b/cpp/src/qpid/ha/QueueReplicator.cpp index 1300819eb7..ffb40fdf22 100644 --- a/cpp/src/qpid/ha/QueueReplicator.cpp +++ b/cpp/src/qpid/ha/QueueReplicator.cpp @@ -108,6 +108,14 @@ class QueueReplicator::QueueObserver : public broker::QueueObserver { boost::shared_ptr queueReplicator; }; +boost::shared_ptr QueueReplicator::create( + HaBroker& hb, boost::shared_ptr q, boost::shared_ptr l) +{ + boost::shared_ptr qr(new QueueReplicator(hb, q, l)); + qr->initialize(); + return qr; +} + QueueReplicator::QueueReplicator(HaBroker& hb, boost::shared_ptr q, boost::shared_ptr l) @@ -144,9 +152,7 @@ QueueReplicator::QueueReplicator(HaBroker& hb, QueueReplicator::~QueueReplicator() {} -// This must be called immediately after the constructor. -// It has to be separate so we can call shared_from_this(). -void QueueReplicator::activate() { +void QueueReplicator::initialize() { Mutex::ScopedLock l(lock); QPID_LOG(debug, logPrefix << "Created"); if (!queue) return; // Already destroyed -- cgit v1.2.1