1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
28
#include <boost/lexical_cast.hpp>
30
#include "WStringUtils.h"
31
#include "WItemSelection.h"
33
#include "WItemSelector.h"
35
WItemSelector::WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected ):
36
m_selection( selection ),
37
m_selected( selected ),
38
m_invalidateSignalConnection(),
42
m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
45
WItemSelector::WItemSelector( const WItemSelector& other ):
46
m_selection( other.m_selection ),
47
m_selected( other.m_selected ),
48
m_valid( other.m_valid )
50
m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
53
WItemSelector& WItemSelector::operator=( const WItemSelector & other )
55
if( this != &other ) // protect against invalid self-assignment
57
m_selection = other.m_selection;
58
m_selected = other.m_selected;
59
m_valid = other.m_valid;
61
m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
64
// by convention, always return *this
68
WItemSelector::~WItemSelector()
71
m_invalidateSignalConnection.disconnect();
74
WItemSelector WItemSelector::newSelector( IndexList selected ) const
76
return createSelector( selected );
79
WItemSelector WItemSelector::newSelector( size_t selected ) const
81
IndexList n = m_selected;
82
n.push_back( selected );
83
return createSelector( n );
86
WItemSelector WItemSelector::newSelector( const std::string asString ) const
88
std::vector<std::string> tokens;
89
tokens = string_utils::tokenize( asString, ";" );
92
for( size_t i = 0; i < tokens.size(); ++i )
94
l.push_back( boost::lexical_cast< size_t >( tokens[i] ) );
97
return createSelector( l );
100
WItemSelector WItemSelector::newSelector() const
102
WItemSelector s( *this );
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 )
107
if( ( *i ) >= m_selection->size() )
109
s.m_selected.erase( i );
115
std::ostream& WItemSelector::operator<<( std::ostream& out ) const
117
for( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
120
if( ( iter + 1 ) != m_selected.end() )
128
std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
130
return other.operator<<( out );
133
bool WItemSelector::operator==( const WItemSelector& other ) const
135
return ( ( m_selection == other.m_selection ) && ( m_selected == other.m_selected ) && ( m_valid == other.m_valid ) );
138
size_t WItemSelector::sizeAll() const
140
return m_selection->size();
143
size_t WItemSelector::size() const
145
return m_selected.size();
148
const boost::shared_ptr< WItemSelectionItem > WItemSelector::atAll( size_t index ) const
150
return m_selection->at( index );
153
const boost::shared_ptr< WItemSelectionItem > WItemSelector::at( size_t index ) const
155
return m_selection->at( getItemIndexOfSelected( index ) );
158
size_t WItemSelector::getItemIndexOfSelected( size_t index ) const
160
return m_selected.at( index );
163
bool WItemSelector::empty() const
165
return ( size() == 0 );
168
void WItemSelector::invalidate()
173
bool WItemSelector::isValid() const
178
WItemSelector WItemSelector::createSelector( const IndexList& selected ) const
180
WItemSelector s = WItemSelector( m_selection, selected );
184
void WItemSelector::lock()
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();
191
void WItemSelector::unlock()
196
WItemSelector::operator unsigned int() const
198
return getItemIndexOfSelected( 0 );
201
WItemSelector::IndexList WItemSelector::getIndexList() const