Bitcoin ABC  0.22.12
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/noncopyable.hpp>
11 #include <boost/range/iterator.hpp>
12 #include <boost/thread/locks.hpp>
13 #include <boost/thread/shared_mutex.hpp>
14 
15 #include <iterator>
16 #include <type_traits>
17 #include <utility>
18 
19 template <typename T, typename L> class RWCollectionView : boost::noncopyable {
20 private:
21  L lock;
23 
24  template <typename I> struct BracketType {
25  using type = decltype(std::declval<T &>()[std::declval<I>()]);
26  };
27 
28 public:
29  RWCollectionView(L l, T &c) : lock(std::move(l)), collection(&c) {}
31  : lock(std::move(other.lock)), collection(other.collection) {}
32 
33  T *operator->() { return collection; }
34  const T *operator->() const { return collection; }
35 
39  using iterator = typename boost::range_iterator<T>::type;
40  iterator begin() { return std::begin(*collection); }
41  iterator end() { return std::end(*collection); }
42  std::reverse_iterator<iterator> rbegin() {
43  return std::rbegin(*collection);
44  }
45  std::reverse_iterator<iterator> rend() { return std::rend(*collection); }
46 
47  using const_iterator = typename boost::range_iterator<const T>::type;
48  const_iterator begin() const { return std::begin(*collection); }
49  const_iterator end() const { return std::end(*collection); }
50  std::reverse_iterator<const_iterator> rbegin() const {
51  return std::rbegin(*collection);
52  }
53  std::reverse_iterator<const_iterator> rend() const {
54  return std::rend(*collection);
55  }
56 
60  template <typename I> typename BracketType<I>::type operator[](I &&index) {
61  return (*collection)[std::forward<I>(index)];
62  }
63 };
64 
65 template <typename T> class RWCollection {
66 private:
68  mutable boost::shared_mutex rwmutex;
69 
70 public:
71  RWCollection() : collection() {}
72 
73  using ReadView =
76  return ReadView(boost::shared_lock<boost::shared_mutex>(rwmutex),
77  collection);
78  }
79 
80  using WriteView =
83  return WriteView(boost::unique_lock<boost::shared_mutex>(rwmutex),
84  collection);
85  }
86 };
87 
88 #endif // BITCOIN_RWCOLLECTION_H
boost::shared_mutex rwmutex
Definition: rwcollection.h:68
RWCollectionView(L l, T &c)
Definition: rwcollection.h:29
iterator end()
Definition: rwcollection.h:41
const T * operator->() const
Definition: rwcollection.h:34
std::reverse_iterator< const_iterator > rend() const
Definition: rwcollection.h:53
typename boost::range_iterator< T >::type iterator
Iterator mechanics.
Definition: rwcollection.h:39
const_iterator begin() const
Definition: rwcollection.h:48
RWCollectionView(RWCollectionView &&other)
Definition: rwcollection.h:30
WriteView getWriteView()
Definition: rwcollection.h:82
iterator begin()
Definition: rwcollection.h:40
decltype(std::declval< T & >()[std::declval< I >()]) type
Definition: rwcollection.h:25
std::reverse_iterator< iterator > rbegin()
Definition: rwcollection.h:42
std::reverse_iterator< iterator > rend()
Definition: rwcollection.h:45
const_iterator end() const
Definition: rwcollection.h:49
std::reverse_iterator< const_iterator > rbegin() const
Definition: rwcollection.h:50
ReadView getReadView() const
Definition: rwcollection.h:75
typename boost::range_iterator< const T >::type const_iterator
Definition: rwcollection.h:47
BracketType< I >::type operator[](I &&index)
Forward bracket operator.
Definition: rwcollection.h:60