mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-24 06:05:01 +02:00
Implement std::hash<boost::intrusive_ptr<T>> for old Boost versions
Boost only implements it iself starting from version 1.74, but a specialization of std::hash<> can be added trivially to allow the use of std::unordered_set<boost::intrusive_ptr<T>> and std::unordered_map<boost::intrusive_ptr<K>, V>. Being unable to use such types already came up a few types in the past, often resulting in the use of raw pointer instead which always involves an additional "is this safe?"/"could the object go out of scope?" discussion. This commit simply solves this for the future by simply allowing the use of intrusive_ptr in unordered containers.
This commit is contained in:
parent
4b18f62a11
commit
500ad70b8c
@ -38,6 +38,7 @@ set(base_SOURCES
|
||||
filelogger.cpp filelogger.hpp filelogger-ti.hpp
|
||||
function.cpp function.hpp function-ti.hpp function-script.cpp functionwrapper.hpp
|
||||
initialize.cpp initialize.hpp
|
||||
intrusive-ptr.hpp
|
||||
io-engine.cpp io-engine.hpp
|
||||
journaldlogger.cpp journaldlogger.hpp journaldlogger-ti.hpp
|
||||
json.cpp json.hpp json-script.cpp
|
||||
|
22
lib/base/intrusive-ptr.hpp
Normal file
22
lib/base/intrusive-ptr.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/i2-base.hpp"
|
||||
#include <memory>
|
||||
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
||||
#include <boost/version.hpp>
|
||||
|
||||
// std::hash is only implemented starting from Boost 1.74. Implement it ourselves for older version to allow using
|
||||
// boost::intrusive_ptr inside std::unordered_set<> or as the key of std::unordered_map<>.
|
||||
// https://github.com/boostorg/smart_ptr/commit/5a18ffdc5609a0e64b63e47cb81c4f0847e0c087
|
||||
#if BOOST_VERSION < 107400
|
||||
template<class T>
|
||||
struct std::hash<boost::intrusive_ptr<T>>
|
||||
{
|
||||
std::size_t operator()(const boost::intrusive_ptr<T>& ptr) const noexcept
|
||||
{
|
||||
return std::hash<T*>{}(ptr.get());
|
||||
}
|
||||
};
|
||||
#endif /* BOOST_VERSION < 107400 */
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "base/i2-base.hpp"
|
||||
#include "base/debug.hpp"
|
||||
#include "base/intrusive-ptr.hpp"
|
||||
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
|
@ -4,6 +4,7 @@
|
||||
#define SHARED_H
|
||||
|
||||
#include "base/atomic.hpp"
|
||||
#include "base/intrusive-ptr.hpp"
|
||||
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
Loading…
x
Reference in New Issue
Block a user