2018-09-02 06:04:34 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_INSTANCE_MULTIMAP_H
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2018-09-02 06:04:34 +02:00
|
|
|
{
|
2018-09-30 01:46:55 +02:00
|
|
|
template<class K, class T, class C = std::less<K>> struct instance_multimap;
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2018-09-02 06:04:34 +02:00
|
|
|
|
2018-09-15 19:15:02 +02:00
|
|
|
/// See instance_list for purpose and overview.
|
2018-09-02 06:04:34 +02:00
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
2018-09-02 06:04:34 +02:00
|
|
|
struct ircd::util::instance_multimap
|
|
|
|
{
|
2018-09-30 01:46:55 +02:00
|
|
|
static std::multimap<K, T *, C> map;
|
2018-09-02 06:04:34 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typename decltype(map)::iterator it;
|
|
|
|
|
|
|
|
instance_multimap(const typename decltype(map)::const_iterator &hint, K&& key);
|
|
|
|
instance_multimap(K&& key);
|
|
|
|
instance_multimap(instance_multimap &&) noexcept;
|
|
|
|
instance_multimap(const instance_multimap &);
|
|
|
|
instance_multimap &operator=(instance_multimap &&) noexcept;
|
|
|
|
instance_multimap &operator=(const instance_multimap &);
|
|
|
|
~instance_multimap() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C>::instance_multimap(K&& key)
|
2018-09-02 06:04:34 +02:00
|
|
|
:it
|
|
|
|
{
|
|
|
|
map.emplace(std::forward<K>(key), static_cast<T *>(this))
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C>::instance_multimap(const typename decltype(map)::const_iterator &hint,
|
|
|
|
K&& key)
|
2018-09-02 06:04:34 +02:00
|
|
|
:it
|
|
|
|
{
|
|
|
|
map.emplace_hint(hint, std::forward<K>(key), static_cast<T *>(this))
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C>::instance_multimap(instance_multimap &&other)
|
2018-09-02 06:04:34 +02:00
|
|
|
noexcept
|
|
|
|
:it
|
|
|
|
{
|
|
|
|
std::move(other.it)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(it != end(map))
|
|
|
|
{
|
|
|
|
it->second = static_cast<T *>(this);
|
|
|
|
other.it = end(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C>::instance_multimap(const instance_multimap &other)
|
2018-09-02 06:04:34 +02:00
|
|
|
:it
|
|
|
|
{
|
|
|
|
other.it != end(map)?
|
|
|
|
map.emplace_hint(other.it, other.it->first, static_cast<T *>(this)):
|
|
|
|
end(map)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C> &
|
|
|
|
ircd::util::instance_multimap<K, T, C>::operator=(instance_multimap &&other)
|
2018-09-02 06:04:34 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~instance_multimap();
|
|
|
|
it = std::move(other.it);
|
|
|
|
if(it != end(map))
|
|
|
|
it->second = static_cast<T *>(this);
|
|
|
|
|
|
|
|
other.it = end(map);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C> &
|
|
|
|
ircd::util::instance_multimap<K, T, C>::operator=(const instance_multimap &other)
|
2018-09-02 06:04:34 +02:00
|
|
|
{
|
|
|
|
this->~instance_multimap();
|
|
|
|
it = other.it != end(map)?
|
|
|
|
map.emplace_hint(other.it, other.it->first, static_cast<T *>(this)):
|
|
|
|
end(map);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K,
|
2018-09-30 01:46:55 +02:00
|
|
|
class T,
|
|
|
|
class C>
|
|
|
|
ircd::util::instance_multimap<K, T, C>::~instance_multimap()
|
2018-09-02 06:04:34 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(it != end(map))
|
|
|
|
map.erase(it);
|
|
|
|
}
|