OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <string>
26 #include <vector>
27 
28 #include <boost/lexical_cast.hpp>
29 
30 #include "WStringUtils.h"
31 #include "WItemSelection.h"
32 
33 #include "WItemSelector.h"
34 
35 WItemSelector::WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected ):
36  m_selection( selection ),
37  m_selected( selected ),
38  m_invalidateSignalConnection(),
39  m_valid( true )
40 {
41  // initialize members
42  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
43 }
44 
46  m_selection( other.m_selection ),
47  m_selected( other.m_selected ),
48  m_valid( other.m_valid )
49 {
50  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
51 }
52 
54 {
55  if( this != &other ) // protect against invalid self-assignment
56  {
57  m_selection = other.m_selection;
58  m_selected = other.m_selected;
59  m_valid = other.m_valid;
60 
61  m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
62  }
63 
64  // by convention, always return *this
65  return *this;
66 }
67 
69 {
70  // cleanup
71  m_invalidateSignalConnection.disconnect();
72 }
73 
75 {
76  return createSelector( selected );
77 }
78 
79 WItemSelector WItemSelector::newSelector( size_t selected ) const
80 {
82  n.push_back( selected );
83  return createSelector( n );
84 }
85 
86 WItemSelector WItemSelector::newSelector( const std::string asString ) const
87 {
88  std::vector<std::string> tokens;
89  tokens = string_utils::tokenize( asString, ";" );
90 
91  IndexList l;
92  for( size_t i = 0; i < tokens.size(); ++i )
93  {
94  l.push_back( boost::lexical_cast< size_t >( tokens[i] ) );
95  }
96 
97  return createSelector( l );
98 }
99 
101 {
102  WItemSelector s( *this );
103  s.m_valid = true;
104  // iterate selected items to remove items with invalid index
105  for( IndexList::iterator i = s.m_selected.begin(); i != s.m_selected.end(); ++i )
106  {
107  if( ( *i ) >= m_selection->size() )
108  {
109  s.m_selected.erase( i );
110  }
111  }
112  return s;
113 }
114 
115 std::ostream& WItemSelector::operator<<( std::ostream& out ) const
116 {
117  for( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
118  {
119  out << ( *iter );
120  if( ( iter + 1 ) != m_selected.end() )
121  {
122  out << ";";
123  }
124  }
125  return out;
126 }
127 
128 std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
129 {
130  return other.operator<<( out );
131 }
132 
133 bool WItemSelector::operator==( const WItemSelector& other ) const
134 {
135  return ( ( m_selection == other.m_selection ) && ( m_selected == other.m_selected ) && ( m_valid == other.m_valid ) );
136 }
137 
139 {
140  return m_selection->size();
141 }
142 
143 size_t WItemSelector::size() const
144 {
145  return m_selected.size();
146 }
147 
148 const boost::shared_ptr< WItemSelectionItem > WItemSelector::atAll( size_t index ) const
149 {
150  return m_selection->at( index );
151 }
152 
153 const boost::shared_ptr< WItemSelectionItem > WItemSelector::at( size_t index ) const
154 {
155  return m_selection->at( getItemIndexOfSelected( index ) );
156 }
157 
158 size_t WItemSelector::getItemIndexOfSelected( size_t index ) const
159 {
160  return m_selected.at( index );
161 }
162 
164 {
165  return ( size() == 0 );
166 }
167 
169 {
170  m_valid = false;
171 }
172 
174 {
175  return m_valid;
176 }
177 
179 {
180  WItemSelector s = WItemSelector( m_selection, selected );
181  return s;
182 }
183 
185 {
186  // 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
187  // ReadTickets are reference counted.
188  m_lock = m_selection->getReadTicket();
189 }
190 
192 {
193  m_lock.reset();
194 }
195 
196 WItemSelector::operator unsigned int() const
197 {
198  return getItemIndexOfSelected( 0 );
199 }
200 
202 {
203  return m_selected;
204 }
205