diff --git a/src/rwcollection.h b/src/rwcollection.h index 36820f0c3..c87cd02ca 100644 --- a/src/rwcollection.h +++ b/src/rwcollection.h @@ -1,86 +1,88 @@ // Copyright (c) 2018-2019 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_RWCOLLECTION_H #define BITCOIN_RWCOLLECTION_H +#include + #include #include #include #include #include #include #include template class RWCollectionView : boost::noncopyable { private: L lock; T *collection; template struct BracketType { typedef decltype(std::declval()[std::declval()]) type; }; public: RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {} RWCollectionView(RWCollectionView &&other) : lock(std::move(other.lock)), collection(other.collection) {} T *operator->() { return collection; } const T *operator->() const { return collection; } /** * Iterator mechanics. */ typedef typename boost::range_iterator::type iterator; iterator begin() { return std::begin(*collection); } iterator end() { return std::end(*collection); } std::reverse_iterator rbegin() { return std::rbegin(*collection); } std::reverse_iterator rend() { return std::rend(*collection); } typedef typename boost::range_iterator::type const_iterator; const_iterator begin() const { return std::begin(*collection); } const_iterator end() const { return std::end(*collection); } std::reverse_iterator rbegin() const { return std::rbegin(*collection); } std::reverse_iterator rend() const { return std::rend(*collection); } /** * Forward bracket operator. */ template typename BracketType::type operator[](I &&index) { return (*collection)[std::forward(index)]; } }; template class RWCollection { private: + T collection GUARDED_BY(rwlock); mutable boost::shared_mutex rwlock; - T collection; public: RWCollection() : collection() {} typedef RWCollectionView> ReadView; ReadView getReadView() const { return ReadView(boost::shared_lock(rwlock), collection); } typedef RWCollectionView> WriteView; WriteView getWriteView() { return WriteView(boost::unique_lock(rwlock), collection); } }; #endif // BITCOIN_RWCOLLECTION_H