Bitcoin ABC 0.30.7
P2P Digital Currency
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:
27 RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {}
29 : lock(std::move(other.lock)), collection(other.collection) {}
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
Definition: rwcollection.h:75
ReadView getReadView() const
Definition: rwcollection.h:76
RWCollectionView< T, std::unique_lock< std::shared_mutex > > WriteView
Definition: rwcollection.h:81
std::shared_mutex rwmutex
Definition: rwcollection.h:69
WriteView getWriteView()
Definition: rwcollection.h:82
const T * operator->() const
Definition: rwcollection.h:35
std::reverse_iterator< const_iterator > rend() const
Definition: rwcollection.h:54
std::reverse_iterator< iterator > rend()
Definition: rwcollection.h:46
std::reverse_iterator< const_iterator > rbegin() const
Definition: rwcollection.h:51
BracketType< I >::type operator[](I &&index)
Forward bracket operator.
Definition: rwcollection.h:61
const_iterator begin() const
Definition: rwcollection.h:49
std::reverse_iterator< iterator > rbegin()
Definition: rwcollection.h:43
RWCollectionView(L l, T &c)
Definition: rwcollection.h:27
iterator end()
Definition: rwcollection.h:42
iterator begin()
Definition: rwcollection.h:41
typename boost::range_iterator< T >::type iterator
Iterator mechanics.
Definition: rwcollection.h:40
const RWCollectionView & operator=(const RWCollectionView)=delete
const_iterator end() const
Definition: rwcollection.h:50
RWCollectionView(const RWCollectionView &)=delete
RWCollectionView(RWCollectionView &&other)
Definition: rwcollection.h:28
typename boost::range_iterator< const T >::type const_iterator
Definition: rwcollection.h:48
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
decltype(std::declval< T & >()[std::declval< I >()]) type
Definition: rwcollection.h:23