mirror of
https://github.com/matrix-construct/construct
synced 2025-02-18 09:40:12 +01:00
ircd::util: Add RAII container for iterators.
This commit is contained in:
parent
bd772a4452
commit
e43b67d146
1 changed files with 66 additions and 0 deletions
|
@ -794,5 +794,71 @@ end(const const_iterators<T> &ci)
|
|||
}
|
||||
|
||||
|
||||
//
|
||||
// For instances of objects that want to add themselves to a container
|
||||
// and store an iterator as a member to remove themselves in the destructor.
|
||||
// It is unsafe to do that. Use this instead.
|
||||
//
|
||||
template<class container>
|
||||
struct unique_const_iterator
|
||||
{
|
||||
container *c;
|
||||
typename container::const_iterator it;
|
||||
|
||||
unique_const_iterator(container &c, typename container::const_iterator it)
|
||||
:c{&c}
|
||||
,it{std::move(it)}
|
||||
{}
|
||||
|
||||
unique_const_iterator()
|
||||
:c{nullptr}
|
||||
{}
|
||||
|
||||
unique_const_iterator(const unique_const_iterator &) = delete;
|
||||
unique_const_iterator(unique_const_iterator &&o)
|
||||
:c{std::move(o.c)}
|
||||
,it{std::move(o.it)}
|
||||
{
|
||||
o.c = nullptr;
|
||||
}
|
||||
|
||||
~unique_const_iterator() noexcept
|
||||
{
|
||||
if(c)
|
||||
c->erase(it);
|
||||
}
|
||||
};
|
||||
|
||||
template<class container>
|
||||
struct unique_iterator
|
||||
{
|
||||
container *c;
|
||||
typename container::iterator it;
|
||||
|
||||
unique_iterator(container &c, typename container::iterator it)
|
||||
:c{&c}
|
||||
,it{std::move(it)}
|
||||
{}
|
||||
|
||||
unique_iterator()
|
||||
:c{nullptr}
|
||||
{}
|
||||
|
||||
unique_iterator(const unique_iterator &) = delete;
|
||||
unique_iterator(unique_iterator &&o)
|
||||
:c{std::move(o.c)}
|
||||
,it{std::move(o.it)}
|
||||
{
|
||||
o.c = nullptr;
|
||||
}
|
||||
|
||||
~unique_iterator() noexcept
|
||||
{
|
||||
if(c)
|
||||
c->erase(it);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace util
|
||||
} // namespace ircd
|
||||
|
|
Loading…
Add table
Reference in a new issue