Bitcoin ABC  0.24.10
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 
17 template <typename T, typename L> class RWCollectionView {
18 private:
19  L lock;
21 
22  template <typename I> struct BracketType {
23  using type = decltype(std::declval<T &>()[std::declval<I>()]);
24  };
25 
26 public:
27  RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {}
29  : lock(std::move(other.lock)), collection(other.collection) {}
30 
31  RWCollectionView(const RWCollectionView &) = delete;
32  const RWCollectionView &operator=(const RWCollectionView) = delete;
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 
66 template <typename T> class RWCollection {
67 private:
69  mutable std::shared_mutex rwmutex;
70 
71 public:
73 
74  using ReadView =
77  return ReadView(std::shared_lock<std::shared_mutex>(rwmutex),
78  collection);
79  }
80 
83  return WriteView(std::unique_lock<std::shared_mutex>(rwmutex),
84  collection);
85  }
86 };
87 
88 #endif // BITCOIN_RWCOLLECTION_H
RWCollectionView::iterator
typename boost::range_iterator< T >::type iterator
Iterator mechanics.
Definition: rwcollection.h:40
RWCollectionView::end
iterator end()
Definition: rwcollection.h:42
RWCollectionView::rbegin
std::reverse_iterator< const_iterator > rbegin() const
Definition: rwcollection.h:51
RWCollectionView
Definition: rwcollection.h:17
RWCollectionView::begin
const_iterator begin() const
Definition: rwcollection.h:49
RWCollectionView::rend
std::reverse_iterator< const_iterator > rend() const
Definition: rwcollection.h:54
RWCollectionView::end
const_iterator end() const
Definition: rwcollection.h:50
RWCollection::RWCollection
RWCollection()
Definition: rwcollection.h:72
threadsafety.h
RWCollectionView::collection
T * collection
Definition: rwcollection.h:20
RWCollectionView::BracketType
Definition: rwcollection.h:22
RWCollectionView::operator->
const T * operator->() const
Definition: rwcollection.h:35
RWCollection::getWriteView
WriteView getWriteView()
Definition: rwcollection.h:82
RWCollectionView::RWCollectionView
RWCollectionView(L l, T &c)
Definition: rwcollection.h:27
RWCollectionView::operator=
const RWCollectionView & operator=(const RWCollectionView)=delete
RWCollection::rwmutex
std::shared_mutex rwmutex
Definition: rwcollection.h:69
RWCollectionView::rbegin
std::reverse_iterator< iterator > rbegin()
Definition: rwcollection.h:43
RWCollection::ReadView
RWCollectionView< const T, std::shared_lock< std::shared_mutex > > ReadView
Definition: rwcollection.h:75
RWCollection::getReadView
ReadView getReadView() const
Definition: rwcollection.h:76
RWCollectionView::RWCollectionView
RWCollectionView(RWCollectionView &&other)
Definition: rwcollection.h:28
RWCollectionView::rend
std::reverse_iterator< iterator > rend()
Definition: rwcollection.h:46
RWCollection::WriteView
RWCollectionView< T, std::unique_lock< std::shared_mutex > > WriteView
Definition: rwcollection.h:81
RWCollectionView::BracketType::type
decltype(std::declval< T & >()[std::declval< I >()]) type
Definition: rwcollection.h:23
RWCollectionView::operator[]
BracketType< I >::type operator[](I &&index)
Forward bracket operator.
Definition: rwcollection.h:61
RWCollectionView::begin
iterator begin()
Definition: rwcollection.h:41
RWCollectionView::operator->
T * operator->()
Definition: rwcollection.h:34
RWCollectionView::const_iterator
typename boost::range_iterator< const T >::type const_iterator
Definition: rwcollection.h:48
RWCollection::collection
T collection
Definition: rwcollection.h:68
RWCollectionView::lock
L lock
Definition: rwcollection.h:19
RWCollection
Definition: rwcollection.h:66