summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2018-04-06 11:30:10 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2018-04-11 21:32:07 +0100
commitaf80cb647b50d6321b6e77135b0f0a4a560ab95f (patch)
treeaa8142856dabf7490118d815d80d974564e25467
parentC++17 (diff)
downloadlibadhocutil-af80cb647b50d6321b6e77135b0f0a4a560ab95f.tar.bz2
libadhocutil-af80cb647b50d6321b6e77135b0f0a4a560ab95f.tar.xz
libadhocutil-af80cb647b50d6321b6e77135b0f0a4a560ab95f.zip
C++17
Updates resource pool to have an interface more in line with the C++ Core Guidelines.
-rw-r--r--libadhocutil/resourcePool.h6
-rw-r--r--libadhocutil/resourcePool.impl.h26
-rw-r--r--libadhocutil/unittests/testResourcePool.cpp5
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<Resource> createResource() const = 0;
/// Destroy an existing resource (defaults to delete).
- virtual void destroyResource(const std::shared_ptr<Resource> &) 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 Resource> &) 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 Resource> &) const;
+ virtual void returnTestResource(Resource const *) const;
private:
typedef std::list<std::shared_ptr<Resource>> 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<R>::~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 <typename R>
void
- ResourcePool<R>::destroyResource(const std::shared_ptr<R> &) const throw()
+ ResourcePool<R>::destroyResource(R const *) const throw()
{
}
template <typename R>
void
- ResourcePool<R>::testResource(const std::shared_ptr<const R> &) const
+ ResourcePool<R>::testResource(R const *) const
{
}
template <typename R>
void
- ResourcePool<R>::returnTestResource(const std::shared_ptr<const R> &) const
+ ResourcePool<R>::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<typename ResourceHandle<R>::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 MockResource> &) 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 MockResource> &) const override
+ void testResource(MockResource const *) const override
{
n += 1;
if (n % 2) {
- fprintf(stderr, "%d, so throwing\n", n);
throw std::exception();
}
}