MarlinMT  0.1.0
Selection.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std includes
4 #include <algorithm>
5 #include <iostream>
6 #include <iterator>
7 #include <vector>
8 
9 // -- Marlin includes
10 // #include "marlinmt/Exceptions.h"
11 
12 // -- MarlinBook includes
14 #include "marlinmt/book/Entry.h"
16 
17 namespace marlinmt {
18  namespace book {
19 
20  // -- MarlinBook forward declaration
21  class BookStore ;
22  template < typename >
23  class Entry ;
24  class Selection;
25 
29  class WeakEntry {
30  friend Selection ;
31  friend BookStore ;
32 
33  // constructor
34  explicit WeakEntry( const std::shared_ptr< const details::Entry > &entry ) ;
35  // constructor
36  explicit WeakEntry( const std::shared_ptr< details::Entry > &entry ) ;
37  WeakEntry() = default;
38  public:
39 
44  [[nodiscard]] bool valid() const ;
45 
49  [[nodiscard]] const EntryKey &key() const ;
50 
56  template <typename T>
57  [[nodiscard]]
59  if( ! valid() ) {
60  MARLIN_BOOK_THROW( "Try to bind an expired WeakEntry");
61  }
62  return Handle<Entry<T>>(_entry.lock());
63  }
64 
65  private:
67  std::weak_ptr< const details::Entry > _entry {};
68  } ;
69 
75  class Selection {
76 
77  public:
78 
85  template < typename T >
86  static Selection find( T begin, T end, const Condition &cond ) ;
87 
94  template < typename T >
95  static WeakEntry findFirst( T begin, T end, const Condition &cond ) ;
96 
100  using const_iterator = typename std::vector< WeakEntry >::const_iterator ;
101 
105  enum struct ComposeStrategy { AND, ONLY_CHILD, ONLY_PARENT } ;
106 
108  Selection() = default ;
109 
110  Selection( const Selection & ) = delete ;
111  Selection &operator=( const Selection & ) = delete ;
112 
114  Selection( Selection && ) = default ;
115  Selection &operator=( Selection && ) = default ;
116 
117  ~Selection() = default ;
124  Selection( const Selection &sel,
125  const Condition &cond,
126  ComposeStrategy strategy = ComposeStrategy::AND ) ;
127 
129  [[nodiscard]] const Condition &condition() const ;
130 
132  [[nodiscard]] const_iterator begin() const ;
133 
135  [[nodiscard]] const_iterator end() const ;
136 
138  [[nodiscard]] std::size_t size() const ;
139 
145  Selection find( const Condition &cond,
146  ComposeStrategy strategy = ComposeStrategy::AND ) ;
147 
152  const WeakEntry &get( std::size_t i ) { return _entries[i]; }
153 
158  void remove( std::size_t i ) ;
159 
165  void remove( std::size_t i, std::size_t n ) ;
166 
171  void remove( const const_iterator &itr ) ;
172 
179  void remove( const const_iterator &begin, const const_iterator &end ) ;
180 
181  private:
183  std::vector< WeakEntry > _entries{} ;
185  Condition _condition{} ;
186  } ;
187 
188  //--------------------------------------------------------------------------
189 
190  template < typename T >
191  Selection Selection::find( T begin, T end, const Condition &cond ) {
192  Selection res{} ;
193  res._condition = cond ;
194 
195  auto dst = std::back_inserter( res._entries ) ;
196 
197  auto fn = [&c = cond]( const typename T::value_type &i ) -> bool {
198  const WeakEntry h = static_cast< WeakEntry >( i ) ;
199  return h.valid() && c( h.key() ) ;
200  } ;
201 
202  for ( auto itr = begin; itr != end; ++itr ) {
203  if ( fn( *itr ) ) {
204  *dst++ = static_cast< WeakEntry >( *itr ) ;
205  }
206  }
207  return res ;
208  }
209 
210  //--------------------------------------------------------------------------
211 
212  template < typename T >
213  WeakEntry Selection::findFirst(T begin, T end, const Condition &cond ) {
214  using value_type = std::remove_cv_t<std::remove_reference_t<decltype(*begin)>>;
215  static_assert(
216  std::is_same_v< value_type, std::shared_ptr<details::Entry>>
217  || std::is_same_v<value_type, std::shared_ptr<const details::Entry>>, "needs container of shared pointer to Entry");
218  for ( auto itr = begin; itr != end; ++itr ) {
219  if(cond((*itr)->key())) {
220  return WeakEntry(*itr);
221  }
222  }
223  return WeakEntry();
224  }
225 
226 
227 
228  } // end namespace book
229 } // end namespace marlinmt
Data selection to identify and manage an Entry.
Definition: EntryData.h:21
typename std::vector< WeakEntry >::const_iterator const_iterator
random access const_iterator for Selections.
Definition: Selection.h:100
bool valid() const
check if WeakEntry is usable.
Definition: Selection.cc:14
const EntryKey & key() const
get key from Entry.
Definition: Selection.cc:18
std::weak_ptr< const details::Entry > _entry
wake reference to Entry
Definition: Selection.h:67
#define MARLIN_BOOK_THROW(message)
Definition: Types.h:10
Handle< Entry< T > > handle() const
create new Handle for Entry.
Definition: Selection.h:58
static Selection find(T begin, T end, const Condition &cond)
Construct Selection from range of Entries.
Definition: Selection.h:191
Wrapper for weak pointer to Entry.
Definition: Selection.h:29
Contains references to entries.
Definition: Selection.h:75
Condition _condition
condition which every entry fulfill.
Definition: Selection.h:185
vanilla Handle.
Definition: Handle.h:54
ComposeStrategy
Possibilities to compose Conditions when creating sub selections.
Definition: Selection.h:105
static WeakEntry findFirst(T begin, T end, const Condition &cond)
Search for first Entry which matches condition.
Definition: Selection.h:213
wrapper class for an Entry filter function.
Definition: Condition.h:21