From af80cb647b50d6321b6e77135b0f0a4a560ab95f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Apr 2018 11:30:10 +0100 Subject: C++17 Updates resource pool to have an interface more in line with the C++ Core Guidelines. --- libadhocutil/resourcePool.h | 6 +++--- libadhocutil/resourcePool.impl.h | 26 +++++++++++++------------- libadhocutil/unittests/testResourcePool.cpp | 5 ++--- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 791737a..f65bf0f 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -82,11 +82,11 @@ namespace AdHoc { /// Create a new resource instance to add to the pool. virtual std::shared_ptr createResource() const = 0; /// Destroy an existing resource (defaults to delete). - virtual void destroyResource(const std::shared_ptr &) const throw(); + virtual void destroyResource(Resource const *) const throw(); /// Test a cached resource is still suitable for use before re-use (defaults to no-op). - virtual void testResource(const std::shared_ptr &) const; + virtual void testResource(Resource const *) const; /// Test a cached resource is still suitable for use on return (defaults to no-op). - virtual void returnTestResource(const std::shared_ptr &) const; + virtual void returnTestResource(Resource const *) const; private: typedef std::list> Available; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index 8153ce0..3d2f089 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -123,29 +123,29 @@ namespace AdHoc { ResourcePool::~ResourcePool() { for (auto & r : available) { - destroyResource(r); + destroyResource(r.get()); } for (auto & r : inUse) { - destroyResource(std::get<0>(*r.second)); + destroyResource(std::get<0>(*r.second).get()); std::get<1>(*r.second) = nullptr; } } template void - ResourcePool::destroyResource(const std::shared_ptr &) const throw() + ResourcePool::destroyResource(R const *) const throw() { } template void - ResourcePool::testResource(const std::shared_ptr &) const + ResourcePool::testResource(R const *) const { } template void - ResourcePool::returnTestResource(const std::shared_ptr &) const + ResourcePool::returnTestResource(R const *) const { } @@ -187,7 +187,7 @@ namespace AdHoc { { Lock(lock); for (auto & r : available) { - destroyResource(r); + destroyResource(r.get()); } available.clear(); } @@ -228,16 +228,16 @@ namespace AdHoc { { Lock(lock); while (!available.empty()) { - auto r = available.front(); + auto & r = available.front(); try { - testResource(r); + testResource(r.get()); auto ro = std::make_shared::Object>(r, this, 0); available.pop_front(); inUse.insert({ std::this_thread::get_id(), ro }); return ro; } catch (...) { - destroyResource(r); + destroyResource(r.get()); available.pop_front(); } } @@ -253,16 +253,16 @@ namespace AdHoc { Lock(lock); removeFrom(r, inUse); try { - returnTestResource(r); + returnTestResource(r.get()); if (available.size() < keep) { available.push_back(r); } else { - destroyResource(r); + destroyResource(r.get()); } } catch (...) { - destroyResource(r); + destroyResource(r.get()); } poolSize.notify(); } @@ -273,7 +273,7 @@ namespace AdHoc { { Lock(lock); removeFrom(r, inUse); - destroyResource(r); + destroyResource(r.get()); poolSize.notify(); } diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index 875179d..216c40c 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -51,7 +51,7 @@ class TRPCreateFail : public TRPSmall { class TRPReturnFail : public TRPSmall { protected: - void returnTestResource(const std::shared_ptr &) const override + void returnTestResource(MockResource const *) const override { throw std::exception(); } @@ -296,11 +296,10 @@ BOOST_AUTO_TEST_CASE( threading2 ) class TTRP : public TRP { public: TTRP() : n(0) { } - void testResource(const std::shared_ptr &) const override + void testResource(MockResource const *) const override { n += 1; if (n % 2) { - fprintf(stderr, "%d, so throwing\n", n); throw std::exception(); } } -- cgit v1.2.3