Add a very simple ResourcePool implementation.
This commit is contained in:
parent
fb774671ad
commit
891303a578
3 changed files with 201 additions and 0 deletions
117
include/duckhandy/resource_pool.hpp
Normal file
117
include/duckhandy/resource_pool.hpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
#pragma once
|
||||
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
|
||||
namespace dhandy {
|
||||
namespace implem {
|
||||
} //namespace implem
|
||||
|
||||
template <typename Name, typename Res>
|
||||
class ResourcePoolBase {
|
||||
struct Wrapper;
|
||||
struct WrapperCompare {
|
||||
typedef std::true_type is_transparent;
|
||||
bool operator() (const Wrapper& a, const Name& b) const;
|
||||
bool operator() (const Name& a, const Wrapper& b) const;
|
||||
bool operator() (const Wrapper& a, const Wrapper& b) const;
|
||||
};
|
||||
typedef std::set<Wrapper, WrapperCompare> StorageContainer;
|
||||
public:
|
||||
virtual ~ResourcePoolBase() = default;
|
||||
|
||||
class Item {
|
||||
friend class ResourcePoolBase;
|
||||
Item (typename StorageContainer::iterator it) : m_it(it) {}
|
||||
typename StorageContainer::iterator m_it;
|
||||
public:
|
||||
Res& operator*();
|
||||
typename std::add_const<Res>::type& operator*() const;
|
||||
Res* operator->();
|
||||
typename std::add_const<Res>::type* operator->() const;
|
||||
};
|
||||
|
||||
Item make (const Name& res_name);
|
||||
std::size_t size() const { return m_storage.size(); }
|
||||
|
||||
private:
|
||||
struct Wrapper {
|
||||
Wrapper (Res&& res, const Name& nm) :
|
||||
payload(std::move(res)), name(nm)
|
||||
{}
|
||||
|
||||
mutable Res payload;
|
||||
Name name;
|
||||
};
|
||||
|
||||
virtual Res on_res_requested (const Name& name) = 0;
|
||||
|
||||
StorageContainer m_storage;
|
||||
};
|
||||
|
||||
template <typename Name, typename Res>
|
||||
class ResourcePool : private ResourcePoolBase<Name, Res> {
|
||||
typedef std::function<Res(const Name&)> MakerFunc;
|
||||
public:
|
||||
explicit ResourcePool (MakerFunc maker) : m_make_res(maker) {}
|
||||
using ResourcePoolBase<Name, Res>::make;
|
||||
using ResourcePoolBase<Name, Res>::Item;
|
||||
using ResourcePoolBase<Name, Res>::size;
|
||||
|
||||
private:
|
||||
virtual Res on_res_requested (const Name& n) { return m_make_res(n); }
|
||||
|
||||
MakerFunc m_make_res;
|
||||
};
|
||||
|
||||
template <typename Name, typename Res>
|
||||
inline Res& ResourcePoolBase<Name, Res>::Item::operator*() {
|
||||
return m_it->payload;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
inline typename std::add_const<Res>::type& ResourcePoolBase<Name, Res>::Item::operator*() const {
|
||||
return m_it->payload;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
inline bool ResourcePoolBase<Name, Res>::WrapperCompare::operator() (const Wrapper& a, const Name& b) const {
|
||||
return a.name < b;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
inline bool ResourcePoolBase<Name, Res>::WrapperCompare::operator() (const Name& a, const Wrapper& b) const {
|
||||
return a < b.name;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
inline bool ResourcePoolBase<Name, Res>::WrapperCompare::operator() (const Wrapper& a, const Wrapper& b) const {
|
||||
return a.name < b.name;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
Res* ResourcePoolBase<Name, Res>::Item::operator->() {
|
||||
return &m_it->payload;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
typename std::add_const<Res>::type* ResourcePoolBase<Name, Res>::Item::operator->() const {
|
||||
return &m_it->payload;
|
||||
}
|
||||
|
||||
template <typename Name, typename Res>
|
||||
auto ResourcePoolBase<Name, Res>::make (const Name& name) -> Item {
|
||||
auto it_found = m_storage.find(name);
|
||||
if (m_storage.end() != it_found) {
|
||||
return Item(it_found);
|
||||
}
|
||||
else {
|
||||
auto insertion_pair = m_storage.insert(Wrapper(this->on_res_requested(name), name));
|
||||
assert(insertion_pair.second);
|
||||
return Item(insertion_pair.first);
|
||||
}
|
||||
}
|
||||
} //namespace dhandy
|
|
@ -7,6 +7,7 @@ add_executable(${PROJECT_NAME}
|
|||
int_conv_test.cpp
|
||||
reversed_sized_array_test.cpp
|
||||
bitfield_pack_test.cpp
|
||||
resource_pool_test.cpp
|
||||
)
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
|
83
test/unit/resource_pool_test.cpp
Normal file
83
test/unit/resource_pool_test.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
/* Copyright 2016-2018 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "duckhandy" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
#include "duckhandy/resource_pool.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
TEST_CASE ("Make operations on the ResourcePool", "[ResourcePool]") {
|
||||
using dhandy::ResourcePool;
|
||||
int callback_count = 0;
|
||||
|
||||
ResourcePool<int, std::vector<int>> vec_pool([&callback_count](int count) {
|
||||
++callback_count;
|
||||
std::vector<int> nums(count);
|
||||
for (int z = 0; z < count; ++z) {
|
||||
nums[z] = z;
|
||||
}
|
||||
return nums;
|
||||
});
|
||||
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(1 == callback_count);
|
||||
CHECK(vec_pool.size() == 1);
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(1 == callback_count);
|
||||
CHECK(vec_pool.size() == 1);
|
||||
|
||||
{
|
||||
auto vec_9 = vec_pool.make(9);
|
||||
REQUIRE(vec_9->size() == 9);
|
||||
for (int z = 0; z < 9; ++z) {
|
||||
CHECK((*vec_9)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(2 == callback_count);
|
||||
CHECK(vec_pool.size() == 2);
|
||||
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(2 == callback_count);
|
||||
CHECK(vec_pool.size() == 2);
|
||||
|
||||
{
|
||||
auto vec_5 = vec_pool.make(5);
|
||||
REQUIRE(vec_5->size() == 5);
|
||||
for (int z = 0; z < 5; ++z) {
|
||||
CHECK((*vec_5)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(3 == callback_count);
|
||||
CHECK(vec_pool.size() == 3);
|
||||
}
|
Loading…
Add table
Reference in a new issue