Changeset View
Changeset View
Standalone View
Standalone View
src/rwcollection.h
Show All 16 Lines | |||||
#include <utility> | #include <utility> | ||||
template <typename T, typename L> class RWCollectionView : boost::noncopyable { | template <typename T, typename L> class RWCollectionView : boost::noncopyable { | ||||
private: | private: | ||||
L lock; | L lock; | ||||
T *collection; | T *collection; | ||||
template <typename I> struct BracketType { | template <typename I> struct BracketType { | ||||
typedef decltype(std::declval<T &>()[std::declval<I>()]) type; | 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) {} | ||||
T *operator->() { return collection; } | T *operator->() { return collection; } | ||||
const T *operator->() const { return collection; } | const T *operator->() const { return collection; } | ||||
/** | /** | ||||
* Iterator mechanics. | * Iterator mechanics. | ||||
*/ | */ | ||||
typedef typename boost::range_iterator<T>::type iterator; | using iterator = typename boost::range_iterator<T>::type; | ||||
iterator begin() { return std::begin(*collection); } | iterator begin() { return std::begin(*collection); } | ||||
iterator end() { return std::end(*collection); } | iterator end() { return std::end(*collection); } | ||||
std::reverse_iterator<iterator> rbegin() { | std::reverse_iterator<iterator> rbegin() { | ||||
return std::rbegin(*collection); | return std::rbegin(*collection); | ||||
} | } | ||||
std::reverse_iterator<iterator> rend() { return std::rend(*collection); } | std::reverse_iterator<iterator> rend() { return std::rend(*collection); } | ||||
typedef typename boost::range_iterator<const T>::type const_iterator; | using const_iterator = typename boost::range_iterator<const T>::type; | ||||
const_iterator begin() const { return std::begin(*collection); } | const_iterator begin() const { return std::begin(*collection); } | ||||
const_iterator end() const { return std::end(*collection); } | const_iterator end() const { return std::end(*collection); } | ||||
std::reverse_iterator<const_iterator> rbegin() const { | std::reverse_iterator<const_iterator> rbegin() const { | ||||
return std::rbegin(*collection); | return std::rbegin(*collection); | ||||
} | } | ||||
std::reverse_iterator<const_iterator> rend() const { | std::reverse_iterator<const_iterator> rend() const { | ||||
return std::rend(*collection); | return std::rend(*collection); | ||||
} | } | ||||
Show All 9 Lines | |||||
template <typename T> class RWCollection { | template <typename T> class RWCollection { | ||||
private: | private: | ||||
T collection GUARDED_BY(rwlock); | T collection GUARDED_BY(rwlock); | ||||
mutable boost::shared_mutex rwlock; | mutable boost::shared_mutex rwlock; | ||||
public: | public: | ||||
RWCollection() : collection() {} | RWCollection() : collection() {} | ||||
typedef RWCollectionView<const T, boost::shared_lock<boost::shared_mutex>> | using ReadView = | ||||
ReadView; | RWCollectionView<const T, boost::shared_lock<boost::shared_mutex>>; | ||||
ReadView getReadView() const { | ReadView getReadView() const { | ||||
return ReadView(boost::shared_lock<boost::shared_mutex>(rwlock), | return ReadView(boost::shared_lock<boost::shared_mutex>(rwlock), | ||||
collection); | collection); | ||||
} | } | ||||
typedef RWCollectionView<T, boost::unique_lock<boost::shared_mutex>> | using WriteView = | ||||
WriteView; | RWCollectionView<T, boost::unique_lock<boost::shared_mutex>>; | ||||
WriteView getWriteView() { | WriteView getWriteView() { | ||||
return WriteView(boost::unique_lock<boost::shared_mutex>(rwlock), | return WriteView(boost::unique_lock<boost::shared_mutex>(rwlock), | ||||
collection); | collection); | ||||
} | } | ||||
}; | }; | ||||
#endif // BITCOIN_RWCOLLECTION_H | #endif // BITCOIN_RWCOLLECTION_H |