From 70e5d087fb05baa0b4a43b57364c6bd745859f3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Apr 2018 11:30:10 +0100 Subject: C++17 Remove all boost things now in the standard library from resource pool. --- libadhocutil/resourcePool.h | 30 +++++++++---------- libadhocutil/resourcePool.impl.h | 46 ++++++++++++++--------------- libadhocutil/unittests/Jamfile.jam | 1 - libadhocutil/unittests/testResourcePool.cpp | 14 ++++----- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 26517bf..791737a 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -1,8 +1,8 @@ #ifndef ADHOCUTIL_RESOURCEPOOL_H #define ADHOCUTIL_RESOURCEPOOL_H -#include -#include +#include +#include #include #include #include @@ -20,10 +20,10 @@ namespace AdHoc { class DLL_PUBLIC ResourceHandle { public: /// Handle to an allocated resource, the pool it belongs to and a count of active references. - typedef boost::tuple *, std::atomic> Object; + typedef std::tuple, ResourcePool *, std::atomic> Object; /// Create a reference to a new resource. - ResourceHandle(Object *); + ResourceHandle(const std::shared_ptr &); /// Create a reference to an existing resource. ResourceHandle(const ResourceHandle &); ~ResourceHandle(); @@ -45,7 +45,7 @@ namespace AdHoc { private: DLL_PRIVATE void incRef() const; DLL_PRIVATE void decRef(); - Object * resource; + std::shared_ptr resource; }; /// A fully featured resource pool for sharing and reusing a finite set of @@ -80,25 +80,25 @@ namespace AdHoc { protected: /// Create a new resource instance to add to the pool. - virtual Resource * createResource() const = 0; + virtual std::shared_ptr createResource() const = 0; /// Destroy an existing resource (defaults to delete). - virtual void destroyResource(Resource *) const throw(); + virtual void destroyResource(const std::shared_ptr &) const throw(); /// Test a cached resource is still suitable for use before re-use (defaults to no-op). - virtual void testResource(const Resource *) const; + virtual void testResource(const std::shared_ptr &) const; /// Test a cached resource is still suitable for use on return (defaults to no-op). - virtual void returnTestResource(const Resource *) const; + virtual void returnTestResource(const std::shared_ptr &) const; private: - typedef std::list Available; - typedef std::multimap::Object *> InUse; + typedef std::list> Available; + typedef std::multimap::Object>> InUse; - void putBack(Resource *); - void discard(Resource *); + void putBack(const std::shared_ptr &); + void discard(const std::shared_ptr &); - DLL_PRIVATE static void removeFrom(Resource *, InUse &); + DLL_PRIVATE static void removeFrom(const std::shared_ptr &, InUse &); DLL_PRIVATE ResourceHandle getOne(); - mutable boost::upgrade_mutex lock; + mutable std::shared_mutex lock; Semaphore poolSize; unsigned int keep; Available available; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index f49a548..8153ce0 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -13,7 +13,7 @@ namespace AdHoc { // template - ResourceHandle::ResourceHandle(Object * o) : + ResourceHandle::ResourceHandle(const std::shared_ptr & o) : resource(o) { incRef(); @@ -39,7 +39,7 @@ namespace AdHoc { ResourceHandle::handleCount() const { ASSERT(resource); - return boost::get<2>(*resource); + return std::get<2>(*resource); } template @@ -47,7 +47,7 @@ namespace AdHoc { ResourceHandle::get() const { ASSERT(resource); - return boost::get<0>(*resource); + return std::get<0>(*resource).get(); } template @@ -60,7 +60,7 @@ namespace AdHoc { template ResourceHandle::operator bool() const { - return resource; + return (bool)resource; } template @@ -68,7 +68,7 @@ namespace AdHoc { ResourceHandle::operator->() const { ASSERT(resource); - return boost::get<0>(*resource); + return std::get<0>(*resource).get(); } template @@ -87,7 +87,7 @@ namespace AdHoc { ResourceHandle::incRef() const { ASSERT(resource); - ++boost::get<2>(*resource); + ++std::get<2>(*resource); } template @@ -95,18 +95,17 @@ namespace AdHoc { ResourceHandle::decRef() { ASSERT(resource); - if (!--boost::get<2>(*resource)) { - if (auto & pool = boost::get<1>(*resource)) { + if (!--std::get<2>(*resource)) { + if (auto pool = std::get<1>(*resource)) { if (std::uncaught_exception()) { - pool->discard(boost::get<0>(*resource)); + pool->discard(std::get<0>(*resource)); } else { - pool->putBack(boost::get<0>(*resource)); + pool->putBack(std::get<0>(*resource)); } } - delete resource; } - resource = nullptr; + resource.reset(); } // @@ -127,27 +126,26 @@ namespace AdHoc { destroyResource(r); } for (auto & r : inUse) { - destroyResource(boost::get<0>(*r.second)); - boost::get<1>(*r.second) = nullptr; + destroyResource(std::get<0>(*r.second)); + std::get<1>(*r.second) = nullptr; } } template void - ResourcePool::destroyResource(R * r) const throw() + ResourcePool::destroyResource(const std::shared_ptr &) const throw() { - delete r; } template void - ResourcePool::testResource(const R *) const + ResourcePool::testResource(const std::shared_ptr &) const { } template void - ResourcePool::returnTestResource(const R *) const + ResourcePool::returnTestResource(const std::shared_ptr &) const { } @@ -233,7 +231,7 @@ namespace AdHoc { auto r = available.front(); try { testResource(r); - auto ro = new typename ResourceHandle::Object(r, this); + auto ro = std::make_shared::Object>(r, this, 0); available.pop_front(); inUse.insert({ std::this_thread::get_id(), ro }); return ro; @@ -243,14 +241,14 @@ namespace AdHoc { available.pop_front(); } } - auto ro = new typename ResourceHandle::Object(createResource(), this); + auto ro = std::make_shared::Object>(createResource(), this, 0); inUse.insert({ std::this_thread::get_id(), ro }); return ro; } template void - ResourcePool::putBack(R * r) + ResourcePool::putBack(const std::shared_ptr & r) { Lock(lock); removeFrom(r, inUse); @@ -271,7 +269,7 @@ namespace AdHoc { template void - ResourcePool::discard(R * r) + ResourcePool::discard(const std::shared_ptr & r) { Lock(lock); removeFrom(r, inUse); @@ -281,11 +279,11 @@ namespace AdHoc { template void - ResourcePool::removeFrom(R * r, InUse & inUse) + ResourcePool::removeFrom(const std::shared_ptr & r, InUse & inUse) { auto rs = inUse.equal_range(std::this_thread::get_id()); for (auto & ri = rs.first; ri != rs.second; ri++) { - if (boost::get<0>(*ri->second) == r) { + if (std::get<0>(*ri->second) == r) { inUse.erase(ri); return; } diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index 8a58ca0..56f29c9 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -232,7 +232,6 @@ run BOOST_TEST_DYN_LINK ..//adhocutil boost_utf - boost_thread boost_system pthread : diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index bc469c4..875179d 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -25,9 +25,9 @@ class TRP : public AdHoc::ResourcePool { public: TRP() : AdHoc::ResourcePool(10, 10) { } protected: - MockResource * createResource() const override + std::shared_ptr createResource() const override { - return new MockResource(); + return std::make_shared(); } }; @@ -35,15 +35,15 @@ class TRPSmall : public AdHoc::ResourcePool { public: TRPSmall() : AdHoc::ResourcePool(3, 1) { } protected: - MockResource * createResource() const override + std::shared_ptr createResource() const override { - return new MockResource(); + return std::make_shared(); } }; class TRPCreateFail : public TRPSmall { protected: - MockResource * createResource() const override + std::shared_ptr createResource() const override { throw std::exception(); } @@ -51,7 +51,7 @@ class TRPCreateFail : public TRPSmall { class TRPReturnFail : public TRPSmall { protected: - void returnTestResource(const MockResource *) const override + void returnTestResource(const std::shared_ptr &) const override { throw std::exception(); } @@ -296,7 +296,7 @@ BOOST_AUTO_TEST_CASE( threading2 ) class TTRP : public TRP { public: TTRP() : n(0) { } - void testResource(const MockResource *) const override + void testResource(const std::shared_ptr &) const override { n += 1; if (n % 2) { -- cgit v1.2.3