Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
rwcollection.h
Go to the documentation of this file.
1// Copyright (c) 2018-2019 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_RWCOLLECTION_H
6#define BITCOIN_RWCOLLECTION_H
7
8#include <threadsafety.h>
9
10#include <boost/range/iterator.hpp>
11
12#include <iterator>
13#include <shared_mutex>
14#include <type_traits>
15#include <utility>
16
17template <typename T, typename L> class RWCollectionView {
18private:
21
22 template <typename I> struct BracketType {
23 using type = decltype(std::declval<T &>()[std::declval<I>()]);
24 };
25
26public:
30
33
34 T *operator->() { return collection; }
35 const T *operator->() const { return collection; }
36
40 using iterator = typename boost::range_iterator<T>::type;
41 iterator begin() { return std::begin(*collection); }
42 iterator end() { return std::end(*collection); }
43 std::reverse_iterator<iterator> rbegin() {
44 return std::rbegin(*collection);
45 }
46 std::reverse_iterator<iterator> rend() { return std::rend(*collection); }
47
48 using const_iterator = typename boost::range_iterator<const T>::type;
49 const_iterator begin() const { return std::begin(*collection); }
50 const_iterator end() const { return std::end(*collection); }
51 std::reverse_iterator<const_iterator> rbegin() const {
52 return std::rbegin(*collection);
53 }
54 std::reverse_iterator<const_iterator> rend() const {
55 return std::rend(*collection);
56 }
57
61 template <typename I> typename BracketType<I>::type operator[](I &&index) {
62 return (*collection)[std::forward<I>(index)];
63 }
64};
65
66template <typename T> class RWCollection {
67private:
69 mutable std::shared_mutex rwmutex;
70
71public:
73
74 using ReadView =
77 return ReadView(std::shared_lock<std::shared_mutex>(rwmutex),
79 }
80
83 return WriteView(std::unique_lock<std::shared_mutex>(rwmutex),
85 }
86};
87
88#endif // BITCOIN_RWCOLLECTION_H
RWCollectionView< const T, std::shared_lock< std::shared_mutex > > ReadView
ReadView getReadView() const
RWCollectionView< T, std::unique_lock< std::shared_mutex > > WriteView
std::shared_mutex rwmutex
WriteView getWriteView()
const T * operator->() const
std::reverse_iterator< const_iterator > rend() const
std::reverse_iterator< iterator > rend()
std::reverse_iterator< const_iterator > rbegin() const
BracketType< I >::type operator[](I &&index)
Forward bracket operator.
const_iterator begin() const
std::reverse_iterator< iterator > rbegin()
RWCollectionView(L l, T &c)
iterator begin()
typename boost::range_iterator< T >::type iterator
Iterator mechanics.
const RWCollectionView & operator=(const RWCollectionView)=delete
const_iterator end() const
RWCollectionView(const RWCollectionView &)=delete
RWCollectionView(RWCollectionView &&other)
typename boost::range_iterator< const T >::type const_iterator
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:259
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
decltype(std::declval< T & >()[std::declval< I >()]) type