mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Removed releaser and destroyer in favour of lambdas and similar
This commit is contained in:
parent
6b7bfb8633
commit
2a970134da
@ -14,31 +14,15 @@ struct S
|
||||
double b;
|
||||
};
|
||||
|
||||
// Custom deleter that returns S* to the pool
|
||||
// This class is defined to be able to log the release of objects back to the pool.
|
||||
// Normally, you would use the predefined etl::ipool::destroyer
|
||||
struct Deleter
|
||||
{
|
||||
Deleter(etl::pool<S, 10>& p)
|
||||
: pool(&p) {}
|
||||
|
||||
void operator()(S* ptr) const
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
std::cout << "Releasing S(" << ptr->a << ", " << ptr->b << ") back to pool." << std::endl;
|
||||
pool->destroy(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
etl::pool<S, 10>* pool;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
etl::pool<S, 10> pool;
|
||||
Deleter pool_deleter(pool);
|
||||
using Unique = etl::unique_ptr<S, Deleter>;
|
||||
auto pool_deleter = [&pool](auto ptr)
|
||||
{
|
||||
std::cout << "Releasing S(" << ptr->a << ", " << ptr->b << ") back to pool." << std::endl;
|
||||
pool.destroy(ptr);
|
||||
};
|
||||
using Unique = etl::unique_ptr<S, decltype(pool_deleter)>;
|
||||
|
||||
Unique us1(pool.create(1, 2), pool_deleter);
|
||||
std::cout << "Created S(" << us1->a << ", " << us1->b << ") from pool." << std::endl;
|
||||
|
||||
@ -271,58 +271,6 @@ namespace etl
|
||||
const_iterator(value_type p, pool_type* pool_) : ipool_iterator<true>(p, pool_) {}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A releaser is a functor that releases an object back to the pool.
|
||||
//***************************************************************************
|
||||
class releaser
|
||||
{
|
||||
public:
|
||||
|
||||
releaser(etl::ipool& p)
|
||||
: pool(&p)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void operator()(const T* const p_object) const
|
||||
{
|
||||
if (p_object)
|
||||
{
|
||||
pool->release(p_object);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::ipool* pool;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A destroyer is a functor that destroys an object and releases it back to the pool.
|
||||
//***************************************************************************
|
||||
class destroyer
|
||||
{
|
||||
public:
|
||||
|
||||
destroyer(etl::ipool& p)
|
||||
: pool(&p)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void operator()(const T* const p_object) const
|
||||
{
|
||||
if (p_object)
|
||||
{
|
||||
pool->destroy(p_object);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::ipool* pool;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
iterator begin()
|
||||
{
|
||||
|
||||
@ -774,9 +774,8 @@ namespace
|
||||
TEST(test_releaser_functor_with_unique_ptr)
|
||||
{
|
||||
etl::pool<S, 10> pool;
|
||||
using Deleter = etl::ipool::releaser;
|
||||
Deleter pool_deleter(pool);
|
||||
using Unique = etl::unique_ptr<S, Deleter>;
|
||||
auto pool_deleter = [&pool](S* ptr) { pool.release(ptr); };
|
||||
using Unique = etl::unique_ptr<S, decltype(pool_deleter)>;
|
||||
|
||||
S::instance_count = 0;
|
||||
|
||||
@ -823,9 +822,8 @@ namespace
|
||||
TEST(test_destroyer_functor_with_unique_ptr)
|
||||
{
|
||||
etl::pool<S, 10> pool;
|
||||
using Deleter = etl::ipool::destroyer;
|
||||
Deleter pool_deleter(pool);
|
||||
using Unique = etl::unique_ptr<S, Deleter>;
|
||||
auto pool_deleter = [&pool](S* ptr) { pool.destroy(ptr); };
|
||||
using Unique = etl::unique_ptr<S, decltype(pool_deleter)>;
|
||||
|
||||
S::instance_count = 0;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user