CatapultServer  v0.5.0.1 (Elephant)
BaseSet.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BaseSetCommitPolicy.h"
23 #include "BaseSetDefaultTraits.h"
24 #include "BaseSetFindIterator.h"
25 #include <memory>
26 
27 namespace catapult {
28  namespace deltaset {
29  template<typename TElementTraits, typename TSetTraits>
30  class BaseSetDelta;
31 
32  template<typename TSetTraits>
34  }
35 }
36 
37 namespace catapult { namespace deltaset {
38 
47  template<
48  typename TElementTraits,
49  typename TSetTraits,
50  typename TCommitPolicy = BaseSetCommitPolicy<TSetTraits>
51  >
52  class BaseSet : public utils::MoveOnly {
53  public:
54  using ElementType = typename TElementTraits::ElementType;
55  using SetType = typename TSetTraits::SetType;
56  using KeyType = typename TSetTraits::KeyType;
59 
61 
62  public:
65  template<typename... TArgs>
66  explicit BaseSet(TArgs&&... args) : m_elements(std::forward<TArgs>(args)...)
67  {}
68 
69  public:
71  bool empty() const {
72  return m_elements.empty();
73  }
74 
76  size_t size() const {
77  return m_elements.size();
78  }
79 
82  FindConstIterator find(const KeyType& key) const {
83  auto iter = m_elements.find(key);
84  return m_elements.cend() != iter ? FindConstIterator(std::move(iter)) : FindConstIterator();
85  }
86 
89  bool contains(const KeyType& key) const {
90  return m_elements.cend() != m_elements.find(key);
91  }
92 
93  public:
95  std::shared_ptr<DeltaType> rebase() {
96  if (m_pWeakDelta.lock())
97  CATAPULT_THROW_RUNTIME_ERROR("only a single attached delta is allowed at a time");
98 
99  auto pDelta = std::make_shared<DeltaType>(m_elements);
100  m_pWeakDelta = pDelta;
101  return pDelta;
102  }
103 
106  std::shared_ptr<DeltaType> rebaseDetached() const {
107  return std::make_shared<DeltaType>(m_elements);
108  }
109 
110  public:
113  template<typename... TArgs>
114  void commit(TArgs&&... args) {
115  auto pDelta = m_pWeakDelta.lock();
116  if (!pDelta)
117  CATAPULT_THROW_RUNTIME_ERROR("attempting to commit changes to a set without any outstanding attached deltas");
118 
119  auto deltas = pDelta->deltas();
120  TCommitPolicy::Update(m_elements, deltas, std::forward<TArgs>(args)...);
121  pDelta->reset();
122  }
123 
124  private:
126  std::weak_ptr<DeltaType> m_pWeakDelta;
127 
128  private:
129  template<typename TElementTraits2, typename TSetTraits2, typename TCommitPolicy2>
131 
132  template<typename TElementTraits2, typename TSetTraits2, typename TCommitPolicy2>
134  };
135 }}
catapult::deltaset::BaseSet::m_elements
SetType m_elements
Definition: BaseSet.h:125
catapult::deltaset::BaseSetDelta
Definition: BaseSet.h:30
catapult::deltaset::BaseSet::commit
void commit(TArgs &&... args)
Definition: BaseSet.h:114
catapult::deltaset::BaseSet::MakeIterableView
friend BaseSetIterationView< TSetTraits2 > MakeIterableView(const BaseSet< TElementTraits2, TSetTraits2, TCommitPolicy2 > &set)
catapult::deltaset::BaseSet::size
size_t size() const
Gets the size of this set.
Definition: BaseSet.h:76
catapult::deltaset::BaseSet::find
FindConstIterator find(const KeyType &key) const
Definition: BaseSet.h:82
catapult::deltaset::BaseSet::rebaseDetached
std::shared_ptr< DeltaType > rebaseDetached() const
Definition: BaseSet.h:106
BaseSetCommitPolicy.h
BaseSetFindIterator.h
catapult::deltaset::BaseSet::m_pWeakDelta
std::weak_ptr< DeltaType > m_pWeakDelta
Definition: BaseSet.h:126
catapult::deltaset::FindTraitsT
Traits for customizing the behavior of find depending on element type.
Definition: BaseSetDefaultTraits.h:180
catapult::deltaset::BaseSet< TElementTraits, TStorageTraits, detail::OrderedSetCommitPolicy< TStorageTraits > >::ElementType
typename TElementTraits::ElementType ElementType
Definition: BaseSet.h:54
catapult::deltaset::detail::BaseSetSingleIteratorWrapper
Definition: BaseSetFindIterator.h:30
catapult::deltaset::BaseSet
Definition: BaseSet.h:52
catapult::deltaset::BaseSet::rebase
std::shared_ptr< DeltaType > rebase()
Returns a delta based on the same original elements as this set.
Definition: BaseSet.h:95
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
catapult::deltaset::BaseSet::IsBaseSetIterable
friend bool IsBaseSetIterable(const BaseSet< TElementTraits2, TSetTraits2, TCommitPolicy2 > &set)
catapult::deltaset::BaseSet::BaseSet
BaseSet(TArgs &&... args)
Definition: BaseSet.h:66
catapult::deltaset::BaseSetIterationView
A view that provides iteration support to a base set.
Definition: BaseSet.h:33
CATAPULT_THROW_RUNTIME_ERROR
#define CATAPULT_THROW_RUNTIME_ERROR(MESSAGE)
Macro used to throw a catapult runtime error.
Definition: exceptions.h:167
catapult::deltaset::BaseSet::FindConstIterator
BaseSetFindIterator< FindTraits, TSetTraits > FindConstIterator
Definition: BaseSet.h:60
BaseSetDefaultTraits.h
catapult::deltaset::BaseSet::contains
bool contains(const KeyType &key) const
Definition: BaseSet.h:89
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::deltaset::BaseSet< TElementTraits, TStorageTraits, detail::OrderedSetCommitPolicy< TStorageTraits > >::KeyType
typename TStorageTraits ::KeyType KeyType
Definition: BaseSet.h:56
catapult::deltaset::BaseSet::empty
bool empty() const
Gets a value indicating whether or not the set is empty.
Definition: BaseSet.h:71
catapult::deltaset::BaseSet::SetType
typename TSetTraits::SetType SetType
Definition: BaseSet.h:55