diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-10-05 10:02:08 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-10-06 08:15:14 +0200 |
commit | 49ea32064a28675a735fd270d07fd6ea8e7e03b6 (patch) | |
tree | bad9b6df6bd2c95f0f5c633b9ca4c4a2273661f9 | |
parent | e7d5bad5ae083da12c3ec4a4a8bdc8b42447a242 (diff) |
use more uniqueptr in SdGlobalResourceContainer
Change-Id: I2cfd478032066c7e4cd930d098a086711b99934f
Reviewed-on: https://gerrit.libreoffice.org/61431
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx | 2 | ||||
-rw-r--r-- | sd/source/ui/tools/SdGlobalResourceContainer.cxx | 35 |
2 files changed, 13 insertions, 24 deletions
diff --git a/sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx b/sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx index 34f12a6dc546..6409bff17d07 100644 --- a/sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx +++ b/sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx @@ -66,7 +66,7 @@ public: When in doubt, use the shared_ptr variant of this method. */ - void AddResource (::std::unique_ptr<SdGlobalResource> && pResource); + void AddResource (::std::unique_ptr<SdGlobalResource> pResource); /** Add a resource to the container. By using a shared_ptr and releasing it only when the SgGlobalResourceContainer is destroyed diff --git a/sd/source/ui/tools/SdGlobalResourceContainer.cxx b/sd/source/ui/tools/SdGlobalResourceContainer.cxx index b6408cdf04be..3eab1474f43d 100644 --- a/sd/source/ui/tools/SdGlobalResourceContainer.cxx +++ b/sd/source/ui/tools/SdGlobalResourceContainer.cxx @@ -68,8 +68,7 @@ private: /** All instances of SdGlobalResource in this vector are owned by the container and will be destroyed when the container is destroyed. */ - typedef ::std::vector<SdGlobalResource*> ResourceList; - ResourceList maResources; + std::vector<std::unique_ptr<SdGlobalResource>> maResources; typedef ::std::vector<std::shared_ptr<SdGlobalResource> > SharedResourceList; SharedResourceList maSharedResources; @@ -89,27 +88,17 @@ SdGlobalResourceContainer& SdGlobalResourceContainer::Instance() //===== SdGlobalResourceContainer ============================================= void SdGlobalResourceContainer::AddResource ( - ::std::unique_ptr<SdGlobalResource> && pResource) + ::std::unique_ptr<SdGlobalResource> pResource) { ::osl::MutexGuard aGuard (mpImpl->maMutex); - Implementation::ResourceList::iterator iResource; - iResource = ::std::find ( - mpImpl->maResources.begin(), - mpImpl->maResources.end(), - pResource.get()); - if (iResource == mpImpl->maResources.end()) - mpImpl->maResources.push_back(pResource.get()); - else - { - // Because the given resource is a unique_ptr it is highly unlikely - // that we come here. But who knows? - SAL_WARN ( "sd.tools", - "SdGlobalResourceContainer:AddResource(): Resource added twice."); - } - // We can not put the unique_ptr into the vector so we release the - // unique_ptr and document that we take ownership explicitly. - pResource.release(); + assert( std::none_of( + mpImpl->maResources.begin(), + mpImpl->maResources.end(), + [&](const std::unique_ptr<SdGlobalResource>& p) { return p == pResource; }) + && "duplicate resource?"); + + mpImpl->maResources.push_back(std::move(pResource)); } void SdGlobalResourceContainer::AddResource ( @@ -162,14 +151,14 @@ SdGlobalResourceContainer::~SdGlobalResourceContainer() // container. This is because a resource A added before resource B // may have been created due to a request of B. Thus B depends on A and // should be destroyed first. - Implementation::ResourceList::reverse_iterator iResource; - for (iResource = mpImpl->maResources.rbegin(); + for (auto iResource = mpImpl->maResources.rbegin(); iResource != mpImpl->maResources.rend(); ++iResource) { - delete *iResource; + iResource->reset(); } + // The SharedResourceList has not to be released manually. We just // assert resources that are still held by someone other than us. Implementation::SharedResourceList::reverse_iterator iSharedResource; |