OpenWalnut  1.5.0dev
WItemSelector.cpp
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 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WItemSelection.h"
30 #include "WItemSelector.h"
31 #include "WStringUtils.h"
32 
33 WItemSelector::WItemSelector( std::shared_ptr< WItemSelection > selection, IndexList selected ):
34  m_selection( selection ),
35  m_selected( selected ),
36  m_invalidateSignalConnection(),
37  m_valid( true )
38 {
39  // initialize members
40  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
41 }
42 
44  m_selection( other.m_selection ),
45  m_selected( other.m_selected ),
46  m_valid( other.m_valid )
47 {
48  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
49 }
50 
52 {
53  if( this != &other ) // protect against invalid self-assignment
54  {
55  m_selection = other.m_selection;
56  m_selected = other.m_selected;
57  m_valid = other.m_valid;
58 
59  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
60  }
61 
62  // by convention, always return *this
63  return *this;
64 }
65 
67 {
68  // cleanup
69  m_invalidateSignalConnection.disconnect();
70 }
71 
73 {
74  return createSelector( selected );
75 }
76 
77 WItemSelector WItemSelector::newSelector( size_t selected ) const
78 {
80  n.push_back( selected );
81  return createSelector( n );
82 }
83 
84 WItemSelector WItemSelector::newSelector( const std::string asString ) const
85 {
86  std::vector<std::string> tokens;
87  tokens = string_utils::tokenize( asString, ";" );
88 
89  IndexList l;
90  for( size_t i = 0; i < tokens.size(); ++i )
91  {
92  l.push_back( string_utils::fromString< size_t >( tokens[i] ) );
93  }
94 
95  return createSelector( l );
96 }
97 
99 {
100  WItemSelector s( *this );
101  s.m_valid = true;
102  // iterate selected items to remove items with invalid index
103  for( IndexList::iterator i = s.m_selected.begin(); i != s.m_selected.end(); ++i )
104  {
105  if( ( *i ) >= m_selection->size() )
106  {
107  s.m_selected.erase( i );
108  }
109  }
110  return s;
111 }
112 
113 std::ostream& WItemSelector::operator<<( std::ostream& out ) const
114 {
115  for( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
116  {
117  out << ( *iter );
118  if( ( iter + 1 ) != m_selected.end() )
119  {
120  out << ";";
121  }
122  }
123  return out;
124 }
125 
126 std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
127 {
128  return other.operator<<( out );
129 }
130 
131 bool WItemSelector::operator==( const WItemSelector& other ) const
132 {
133  return ( ( m_selection == other.m_selection ) && ( m_selected == other.m_selected ) && ( m_valid == other.m_valid ) );
134 }
135 
137 {
138  return m_selection->size();
139 }
140 
141 size_t WItemSelector::size() const
142 {
143  return m_selected.size();
144 }
145 
146 const std::shared_ptr< WItemSelectionItem > WItemSelector::atAll( size_t index ) const
147 {
148  return m_selection->at( index );
149 }
150 
151 const std::shared_ptr< WItemSelectionItem > WItemSelector::at( size_t index ) const
152 {
153  return m_selection->at( getItemIndexOfSelected( index ) );
154 }
155 
156 size_t WItemSelector::getItemIndexOfSelected( size_t index ) const
157 {
158  return m_selected.at( index );
159 }
160 
162 {
163  return ( size() == 0 );
164 }
165 
167 {
168  m_valid = false;
169 }
170 
172 {
173  return m_valid;
174 }
175 
177 {
178  WItemSelector s = WItemSelector( m_selection, selected );
179  return s;
180 }
181 
183 {
184  // NOTE: it is not needed to check whether lock() has been called earlier. The old lock gets freed in the moment m_lock gets overwritten as
185  // ReadTickets are reference counted.
186  m_lock = m_selection->getReadTicket();
187 }
188 
190 {
191  m_lock.reset();
192 }
193 
194 WItemSelector::operator unsigned int() const
195 {
196  return getItemIndexOfSelected( 0 );
197 }
198 
200 {
201  return m_selected;
202 }
203 
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::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).