Kaydet (Commit) 39f563ca authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr in ComponentContext

no need to store such small movable and ref-counted objects separately

Change-Id: Idf4262a8edbfe07fcb4b96d1025924224b72b5b6
Reviewed-on: https://gerrit.libreoffice.org/61113
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 52d0aca5
...@@ -137,7 +137,7 @@ protected: ...@@ -137,7 +137,7 @@ protected:
, lateInit( lateInit_ ) , lateInit( lateInit_ )
{} {}
}; };
typedef std::unordered_map< OUString, ContextEntry * > t_map; typedef std::unordered_map< OUString, ContextEntry > t_map;
t_map m_map; t_map m_map;
Reference< lang::XMultiComponentFactory > m_xSMgr; Reference< lang::XMultiComponentFactory > m_xSMgr;
...@@ -150,7 +150,6 @@ public: ...@@ -150,7 +150,6 @@ public:
ComponentContext( ComponentContext(
ContextEntry_Init const * pEntries, sal_Int32 nEntries, ContextEntry_Init const * pEntries, sal_Int32 nEntries,
Reference< XComponentContext > const & xDelegate ); Reference< XComponentContext > const & xDelegate );
virtual ~ComponentContext() override;
// XComponentContext // XComponentContext
virtual Any SAL_CALL getValueByName( OUString const & rName ) override; virtual Any SAL_CALL getValueByName( OUString const & rName ) override;
...@@ -177,12 +176,11 @@ public: ...@@ -177,12 +176,11 @@ public:
void ComponentContext::insertByName( void ComponentContext::insertByName(
OUString const & name, Any const & element ) OUString const & name, Any const & element )
{ {
t_map::mapped_type entry( ContextEntry entry(
new ContextEntry(
element, element,
/* lateInit_: */ /* lateInit_: */
name.startsWith( "/singletons/" ) && name.startsWith( "/singletons/" ) &&
!element.hasValue() ) ); !element.hasValue() );
MutexGuard guard( m_mutex ); MutexGuard guard( m_mutex );
std::pair<t_map::iterator, bool> insertion( m_map.emplace( std::pair<t_map::iterator, bool> insertion( m_map.emplace(
name, entry ) ); name, entry ) );
...@@ -202,7 +200,6 @@ void ComponentContext::removeByName( OUString const & name ) ...@@ -202,7 +200,6 @@ void ComponentContext::removeByName( OUString const & name )
"no such element: " + name, "no such element: " + name,
static_cast<OWeakObject *>(this) ); static_cast<OWeakObject *>(this) );
delete iFind->second;
m_map.erase(iFind); m_map.erase(iFind);
} }
...@@ -212,7 +209,7 @@ void ComponentContext::replaceByName( ...@@ -212,7 +209,7 @@ void ComponentContext::replaceByName(
OUString const & name, Any const & element ) OUString const & name, Any const & element )
{ {
MutexGuard guard( m_mutex ); MutexGuard guard( m_mutex );
t_map::const_iterator const iFind( m_map.find( name ) ); t_map::iterator iFind( m_map.find( name ) );
if (iFind == m_map.end()) if (iFind == m_map.end())
throw container::NoSuchElementException( throw container::NoSuchElementException(
"no such element: " + name, "no such element: " + name,
...@@ -220,13 +217,13 @@ void ComponentContext::replaceByName( ...@@ -220,13 +217,13 @@ void ComponentContext::replaceByName(
if (name.startsWith( "/singletons/" ) && if (name.startsWith( "/singletons/" ) &&
!element.hasValue()) !element.hasValue())
{ {
iFind->second->value.clear(); iFind->second.value.clear();
iFind->second->lateInit = true; iFind->second.lateInit = true;
} }
else else
{ {
iFind->second->value = element; iFind->second.value = element;
iFind->second->lateInit = false; iFind->second.lateInit = false;
} }
} }
...@@ -276,13 +273,13 @@ sal_Bool ComponentContext::hasElements() ...@@ -276,13 +273,13 @@ sal_Bool ComponentContext::hasElements()
Any ComponentContext::lookupMap( OUString const & rName ) Any ComponentContext::lookupMap( OUString const & rName )
{ {
ResettableMutexGuard guard( m_mutex ); ResettableMutexGuard guard( m_mutex );
t_map::const_iterator iFind( m_map.find( rName ) ); t_map::iterator iFind( m_map.find( rName ) );
if (iFind == m_map.end()) if (iFind == m_map.end())
return Any(); return Any();
t_map::mapped_type pEntry = iFind->second; ContextEntry& rFindEntry = iFind->second;
if (! pEntry->lateInit) if (! rFindEntry.lateInit)
return pEntry->value; return rFindEntry.value;
// late init singleton entry // late init singleton entry
Reference< XInterface > xInstance; Reference< XInterface > xInstance;
...@@ -351,14 +348,14 @@ Any ComponentContext::lookupMap( OUString const & rName ) ...@@ -351,14 +348,14 @@ Any ComponentContext::lookupMap( OUString const & rName )
iFind = m_map.find( rName ); iFind = m_map.find( rName );
if (iFind != m_map.end()) if (iFind != m_map.end())
{ {
pEntry = iFind->second; ContextEntry & rEntry = iFind->second;
if (pEntry->lateInit) if (rEntry.lateInit)
{ {
pEntry->value <<= xInstance; rEntry.value <<= xInstance;
pEntry->lateInit = false; rEntry.lateInit = false;
return pEntry->value; return rEntry.value;
} }
ret = pEntry->value; ret = rEntry.value;
} }
guard.clear(); guard.clear();
if (ret != xInstance) { if (ret != xInstance) {
...@@ -397,44 +394,35 @@ Reference< lang::XMultiComponentFactory > ComponentContext::getServiceManager() ...@@ -397,44 +394,35 @@ Reference< lang::XMultiComponentFactory > ComponentContext::getServiceManager()
return m_xSMgr; return m_xSMgr;
} }
ComponentContext::~ComponentContext()
{
t_map::const_iterator iPos( m_map.begin() );
t_map::const_iterator const iEnd( m_map.end() );
for ( ; iPos != iEnd; ++iPos )
delete iPos->second;
m_map.clear();
}
void ComponentContext::disposing() void ComponentContext::disposing()
{ {
Reference< lang::XComponent > xTDMgr, xAC; // to be disposed separately Reference< lang::XComponent > xTDMgr, xAC; // to be disposed separately
// dispose all context objects // dispose all context objects
t_map::const_iterator iPos( m_map.begin() ); t_map::iterator iPos( m_map.begin() );
t_map::const_iterator const iEnd( m_map.end() ); t_map::iterator const iEnd( m_map.end() );
for ( ; iPos != iEnd; ++iPos ) for ( ; iPos != iEnd; ++iPos )
{ {
t_map::mapped_type pEntry = iPos->second; ContextEntry& rEntry = iPos->second;
// service manager disposed separately // service manager disposed separately
if (!m_xSMgr.is() || if (!m_xSMgr.is() ||
!iPos->first.startsWith( SMGR_SINGLETON )) !iPos->first.startsWith( SMGR_SINGLETON ))
{ {
if (pEntry->lateInit) if (rEntry.lateInit)
{ {
// late init // late init
MutexGuard guard( m_mutex ); MutexGuard guard( m_mutex );
if (pEntry->lateInit) if (rEntry.lateInit)
{ {
pEntry->value.clear(); // release factory rEntry.value.clear(); // release factory
pEntry->lateInit = false; rEntry.lateInit = false;
continue; continue;
} }
} }
Reference< lang::XComponent > xComp; Reference< lang::XComponent > xComp;
pEntry->value >>= xComp; rEntry.value >>= xComp;
if (xComp.is()) if (xComp.is())
{ {
if ( iPos->first == TDMGR_SINGLETON ) if ( iPos->first == TDMGR_SINGLETON )
...@@ -461,9 +449,6 @@ void ComponentContext::disposing() ...@@ -461,9 +449,6 @@ void ComponentContext::disposing()
// dispose tdmgr; revokes callback from cppu runtime // dispose tdmgr; revokes callback from cppu runtime
try_dispose( xTDMgr ); try_dispose( xTDMgr );
iPos = m_map.begin();
for ( ; iPos != iEnd; ++iPos )
delete iPos->second;
m_map.clear(); m_map.clear();
// Hack to terminate any JNI bridge's AsynchronousFinalizer thread (as JNI // Hack to terminate any JNI bridge's AsynchronousFinalizer thread (as JNI
...@@ -503,15 +488,15 @@ ComponentContext::ComponentContext( ...@@ -503,15 +488,15 @@ ComponentContext::ComponentContext(
if (rEntry.bLateInitService) if (rEntry.bLateInitService)
{ {
// singleton entry // singleton entry
m_map[ rEntry.name ] = new ContextEntry( Any(), true ); m_map.emplace( rEntry.name, ContextEntry( Any(), true ) );
// service // service
m_map[ rEntry.name + "/service" ] = new ContextEntry( rEntry.value, false ); m_map.emplace( rEntry.name + "/service", ContextEntry( rEntry.value, false ) );
// initial-arguments are provided as optional context entry // initial-arguments are provided as optional context entry
} }
else else
{ {
// only value, no late init factory nor string // only value, no late init factory nor string
m_map[ rEntry.name ] = new ContextEntry( rEntry.value, false ); m_map.emplace( rEntry.name, ContextEntry( rEntry.value, false ) );
} }
} }
......
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