core: Add hashing functor for creating hash of a Ptr

This commit is contained in:
Peter D. Barnes, Jr
2021-09-15 22:01:48 +02:00
committed by Tommaso Pecorella
parent 8cfe1b5e2e
commit 7c48447ffa
3 changed files with 37 additions and 0 deletions

View File

@@ -695,4 +695,39 @@ Ptr<T>::operator Tester * () const
} // namespace ns3
/****************************************************
* Global Functions (outside namespace ns3)
***************************************************/
/**
* \ingroup ptr
* Hashing functor taking a `Ptr` and returning a @c std::size_t.
* For use with `unordered_map` and `unordered_set`.
*
* \note When a `Ptr` is used in a container the lifetime of the underlying
* object is at least as long as the container. In other words,
* you need to remove the object from the container when you are done with
* it, otherwise the object will persist until the container itself is
* deleted.
*
* \tparam T \deduced The type held by the `Ptr`
*/
template<class T>
struct
std::hash<ns3::Ptr<T>>
{
/**
* The functor.
* \param p The `Ptr` value to hash.
* \return the hash
*/
std::size_t
operator () (ns3::Ptr<T> p) const
{
return std::hash<const T *> () (ns3::PeekPointer (p));
}
};
#endif /* PTR_H */