Changeset View
Changeset View
Standalone View
Standalone View
src/rwcollection.h
// Copyright (c) 2018-2019 The Bitcoin developers | // Copyright (c) 2018-2019 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_RWCOLLECTION_H | #ifndef BITCOIN_RWCOLLECTION_H | ||||
#define BITCOIN_RWCOLLECTION_H | #define BITCOIN_RWCOLLECTION_H | ||||
#include <threadsafety.h> | #include <threadsafety.h> | ||||
#include <boost/noncopyable.hpp> | |||||
#include <boost/range/iterator.hpp> | #include <boost/range/iterator.hpp> | ||||
#include <boost/thread/locks.hpp> | #include <boost/thread/locks.hpp> | ||||
#include <boost/thread/shared_mutex.hpp> | #include <boost/thread/shared_mutex.hpp> | ||||
#include <iterator> | #include <iterator> | ||||
#include <type_traits> | #include <type_traits> | ||||
#include <utility> | #include <utility> | ||||
template <typename T, typename L> class RWCollectionView : boost::noncopyable { | template <typename T, typename L> class RWCollectionView { | ||||
private: | private: | ||||
L lock; | L lock; | ||||
T *collection; | T *collection; | ||||
template <typename I> struct BracketType { | template <typename I> struct BracketType { | ||||
using type = decltype(std::declval<T &>()[std::declval<I>()]); | using type = decltype(std::declval<T &>()[std::declval<I>()]); | ||||
}; | }; | ||||
public: | public: | ||||
RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {} | RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {} | ||||
RWCollectionView(RWCollectionView &&other) | RWCollectionView(RWCollectionView &&other) | ||||
: lock(std::move(other.lock)), collection(other.collection) {} | : lock(std::move(other.lock)), collection(other.collection) {} | ||||
RWCollectionView(const RWCollectionView &) = delete; | |||||
const RWCollectionView &operator=(const RWCollectionView) = delete; | |||||
T *operator->() { return collection; } | T *operator->() { return collection; } | ||||
const T *operator->() const { return collection; } | const T *operator->() const { return collection; } | ||||
/** | /** | ||||
* Iterator mechanics. | * Iterator mechanics. | ||||
*/ | */ | ||||
using iterator = typename boost::range_iterator<T>::type; | using iterator = typename boost::range_iterator<T>::type; | ||||
iterator begin() { return std::begin(*collection); } | iterator begin() { return std::begin(*collection); } | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |