OpenWalnut  1.5.0dev
WItemSelector.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WITEMSELECTOR_H
26 #define WITEMSELECTOR_H
27 
28 #include <istream>
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 #include <vector>
33 
34 #include <boost/signals2/signal.hpp>
35 
36 #include "WItemSelection.h"
37 #include "WItemSelectionItem.h"
38 
39 
40 /**
41  * This class represents a subset of a WItemSelection. It is a class for managing selections. The class is kept very restrictive. The selection
42  * can't be edited after the instantiation of the class to keep the interface clean, easily usable and consistent among multiple threads. So
43  * please DO NOT extend it to provide methods for changing it!
44  *
45  * This class can be seen as some kind of special "iterator" providing access to the underlying set without allowing it to be modified. The class
46  * provides methods to access the whole set and the subset represented by itself. The restrictive interface ensures thread-safety and enforces
47  * that each new selection is done by a new instance of this class, which is needed by the WPropertyVariable to work properly.
48  *
49  * \note the protected constructor avoids instance creation of classes not the WItemSelection. This is restrictive but needed. Nobody can create
50  * instances of it, changing the underlying WItemSelection and using it as selector for another ItemSelection instance.
51  */
52 class WItemSelector // NOLINT
53 {
54  friend class WItemSelection; //!< Only class allowed to instanciate this class.
55 public:
56  /**
57  * The type used for storing index lists. It is a list of integer correlating with the elements in the managed WItemSelection class.
58  */
59  typedef std::vector< size_t > IndexList;
60 
61  /**
62  * Copy constructor. Creates a new copy of the selector and ensure proper signal subscriptions to the underlying selection.
63  *
64  * \param other the selector to copy
65  */
66  WItemSelector( const WItemSelector& other );
67 
68  /**
69  * Copy assignment. Creates a new copy of the selector and ensure proper signal subscriptions to the underlying selection.
70  *
71  * \param other the selector to copy
72  *
73  * \return this.
74  */
75  WItemSelector& operator=( const WItemSelector & other );
76 
77  /**
78  * Destructor.
79  */
80  virtual ~WItemSelector();
81 
82  /**
83  * Creates a new valid instance with the specified items selected. This is especially useful to simply create a new selection if only the old
84  * selection is known.
85  *
86  * \note Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned
87  * one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using \ref lock and \ref unlock.
88  *
89  * \param selected the selected items (their index in WItemSelection).
90  *
91  * \return the new selector instance
92  */
93  WItemSelector newSelector( IndexList selected ) const;
94 
95  /**
96  * Creates a new valid instance with the specified items selected. This can be useful to add a certain index. The new selector has the
97  * selection from this AND the specified one. If you want to create a selector containing only one selected item, use the method that uses
98  * the IndexList.
99  *
100  * \note Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned
101  * one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using \ref lock and \ref unlock.
102  *
103  * \param selected the selected item (the index in WItemSelection).
104  *
105  * \return the new selector instance
106  */
107  WItemSelector newSelector( size_t selected ) const;
108 
109  /**
110  * Creates a new valid instance with the specified items selected. This is especially useful to simply create a new selection if only the
111  * string representing it is known. This somehow correlates to the << operator.
112  *
113  * \note Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned
114  * one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using \ref lock and \ref unlock.
115  *
116  * \param asString the selected items
117  *
118  * \return the new selector instance
119  */
120  WItemSelector newSelector( const std::string asString ) const;
121 
122  /**
123  * Creates a new selector, but basing on this instance as old one. The new selector tries to keep the old selection but makes the internal
124  * selection list valid with the current underlying selection.
125  *
126  * \note Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned
127  * one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using \ref lock and \ref unlock.
128  *
129  * \return the new (valid) selector.
130  */
131  WItemSelector newSelector() const;
132 
133  /**
134  * Compares two selector. They are assumed to be equal if the selected items are equal and if the underlying WItemSelection is the same.
135  *
136  * \param other the selector
137  *
138  * \return true if equal
139  */
140  bool operator==( const WItemSelector& other ) const;
141 
142  /**
143  * Write a selection in string representation to the given output stream.
144  *
145  * \param out the output stream where to put the information
146  *
147  * \return the output stream extended by the information of this selector
148  */
149  std::ostream& operator<<( std::ostream& out ) const;
150 
151  /**
152  * Gives the count of elements in the set of selectable items. This is \ref size + number of unselected items.
153  *
154  * \return the number of all items in the item set.
155  */
156  virtual size_t sizeAll() const;
157 
158  /**
159  * The number of selected items.
160  *
161  * \return the number of selected items.
162  */
163  virtual size_t size() const;
164 
165  /**
166  * True if the selection is empty.
167  *
168  * \return true if nothing is selected.
169  */
170  virtual bool empty() const;
171 
172  /**
173  * Gets the item with the given index from the WItemSelection. This index does not equal the index of the same item for \ref at. This method
174  * is useful to go through the list of ALL items (not only the selected).
175  *
176  * \param index the index
177  *
178  * \return the item
179  */
180  virtual const std::shared_ptr< WItemSelectionItem > atAll( size_t index ) const;
181 
182  /**
183  * Gets the selected item with the given index. This is not the same index as the element has in the corresponding WItemSelection!
184  * This method is especially useful to iterate the through the selected items.
185  *
186  * \param index the index
187  *
188  * \return the item
189  */
190  virtual const std::shared_ptr< WItemSelectionItem > at( size_t index ) const;
191 
192  /**
193  * Helps to get the index of an selected item in the WItemSelection. This is somehow similar to \ref at, but does not return the item but the
194  * index to it.
195  *
196  * \param index the index in the selection (not the item index in WItemSelection)
197  *
198  * \return the index in WItemSelection.
199  */
200  virtual size_t getItemIndexOfSelected( size_t index ) const;
201 
202  /**
203  * Checks whether the selection is valid anymore. If a selector is not valid anymore, you should ask the one providing the selectors (most
204  * probably a WPropSelection) for a new one.
205  *
206  * \return true if valid.
207  */
208  virtual bool isValid() const;
209 
210  /**
211  * Read locks the underlying selection. This ensure, that the selection stays fixed as long as this selector is locked. This also ensures
212  * that no invalidation can be issued as long as this selector has the lock. BUT it is possible that an invalidation occurs while this
213  * selector waits. So please always check for validity of the selector ater locking.
214  */
215  void lock();
216 
217  /**
218  * Unlocks the selection again. Always call this after a lock.
219  */
220  void unlock();
221 
222  /**
223  * Allow cast from selector to unsigned int.
224  *
225  * \return the index of the first selected item in the selection.
226  */
227  operator unsigned int() const;
228 
229  /**
230  * Casts the selector to a list of indices currently selected. It contains the list of index in the corresponding WItemSelection. This is
231  * especially useful if the whole index list is needed without nasty iterations.
232  *
233  * \return the list of index.
234  */
235  IndexList getIndexList() const;
236 
237 protected:
238  /**
239  * Constructor creates an selector for the specified selection of items. Noting is selected after construction.
240  *
241  * \param selection the selection handled by this instance
242  * \param selected the set of selected items
243  */
244  WItemSelector( std::shared_ptr< WItemSelection > selection, IndexList selected );
245 
246  /**
247  * The selection handled by this selector.
248  */
249  std::shared_ptr< WItemSelection > m_selection;
250 
251  /**
252  * The list of items currently selected.
253  */
255 
256  /**
257  * Stores the connection made using WItemSelection::subscribeInvalidateSignal.
258  */
259  boost::signals2::connection m_invalidateSignalConnection;
260 
261 private:
262  /**
263  * Creates a new selector instance using the specified index list. Handles all needed signal subscription stuff.
264  *
265  * \param selected the index list of selected items
266  *
267  * \return new selector
268  */
269  WItemSelector createSelector( const IndexList& selected ) const;
270 
271  /**
272  * Handles the case of invalidation.
273  */
274  void invalidate();
275 
276  /**
277  * If true the selector is valid.
278  */
279  bool m_valid;
280 
281  /**
282  * This locks prevents the selection to be modified during selector iteration.
283  */
285 };
286 
287 /**
288  * Write a selection in string representation to the given output stream.
289  *
290  * \param out the output stream where to put the information
291  * \param other the instance to write out
292  *
293  * \return the output stream extended by the information of this selector
294  */
295 std::ostream& operator<<( std::ostream& out, const WItemSelector& other );
296 
297 #endif // WITEMSELECTOR_H
A class containing a list of named items.
This class represents a subset of a WItemSelection.
Definition: WItemSelector.h:53
virtual bool empty() const
True if the selection is empty.
std::vector< size_t > IndexList
The type used for storing index lists.
Definition: WItemSelector.h:59
void unlock()
Unlocks the selection again.
void invalidate()
Handles the case of invalidation.
std::shared_ptr< WItemSelection > m_selection
The selection handled by this selector.
virtual const std::shared_ptr< WItemSelectionItem > at(size_t index) const
Gets the selected item with the given index.
WItemSelector & operator=(const WItemSelector &other)
Copy assignment.
virtual size_t size() const
The number of selected items.
WItemSelection::ReadTicket m_lock
This locks prevents the selection to be modified during selector iteration.
virtual size_t sizeAll() const
Gives the count of elements in the set of selectable items.
std::ostream & operator<<(std::ostream &out) const
Write a selection in string representation to the given output stream.
bool m_valid
If true the selector is valid.
WItemSelector createSelector(const IndexList &selected) const
Creates a new selector instance using the specified index list.
IndexList getIndexList() const
Casts the selector to a list of indices currently selected.
IndexList m_selected
The list of items currently selected.
WItemSelector(const WItemSelector &other)
Copy constructor.
bool operator==(const WItemSelector &other) const
Compares two selector.
virtual bool isValid() const
Checks whether the selection is valid anymore.
WItemSelector newSelector() const
Creates a new selector, but basing on this instance as old one.
virtual const std::shared_ptr< WItemSelectionItem > atAll(size_t index) const
Gets the item with the given index from the WItemSelection.
void lock()
Read locks the underlying selection.
virtual ~WItemSelector()
Destructor.
virtual size_t getItemIndexOfSelected(size_t index) const
Helps to get the index of an selected item in the WItemSelection.
boost::signals2::connection m_invalidateSignalConnection
Stores the connection made using WItemSelection::subscribeInvalidateSignal.
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65