OpenWalnut  1.4.0
WItemSelector.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "WStringUtils.h"
00029 #include "WItemSelection.h"
00030 
00031 #include "WItemSelector.h"
00032 
00033 WItemSelector::WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected ):
00034     m_selection( selection ),
00035     m_selected( selected ),
00036     m_invalidateSignalConnection(),
00037     m_valid( true )
00038 {
00039     // initialize members
00040     m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00041 }
00042 
00043 WItemSelector::WItemSelector( const WItemSelector& other ):
00044     m_selection( other.m_selection ),
00045     m_selected( other.m_selected ),
00046     m_valid( other.m_valid )
00047 {
00048     m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00049 }
00050 
00051 WItemSelector& WItemSelector::operator=( const WItemSelector & other )
00052 {
00053     if( this != &other ) // protect against invalid self-assignment
00054     {
00055         m_selection = other.m_selection;
00056         m_selected = other.m_selected;
00057         m_valid = other.m_valid;
00058 
00059         m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00060     }
00061 
00062     // by convention, always return *this
00063     return *this;
00064 }
00065 
00066 WItemSelector::~WItemSelector()
00067 {
00068     // cleanup
00069     m_invalidateSignalConnection.disconnect();
00070 }
00071 
00072 WItemSelector WItemSelector::newSelector( IndexList selected ) const
00073 {
00074     return createSelector( selected );
00075 }
00076 
00077 WItemSelector WItemSelector::newSelector( size_t selected ) const
00078 {
00079     IndexList n = m_selected;
00080     n.push_back( selected );
00081     return createSelector( n );
00082 }
00083 
00084 WItemSelector WItemSelector::newSelector( const std::string asString ) const
00085 {
00086     std::vector<std::string> tokens;
00087     tokens = string_utils::tokenize( asString, ";" );
00088 
00089     IndexList l;
00090     for( size_t i = 0; i < tokens.size(); ++i )
00091     {
00092         l.push_back( string_utils::fromString< size_t >( tokens[i] ) );
00093     }
00094 
00095     return createSelector( l );
00096 }
00097 
00098 WItemSelector WItemSelector::newSelector() const
00099 {
00100     WItemSelector s( *this );
00101     s.m_valid = true;
00102     // iterate selected items to remove items with invalid index
00103     for( IndexList::iterator i = s.m_selected.begin(); i != s.m_selected.end(); ++i )
00104     {
00105         if( ( *i ) >= m_selection->size() )
00106         {
00107             s.m_selected.erase( i );
00108         }
00109     }
00110     return s;
00111 }
00112 
00113 std::ostream& WItemSelector::operator<<( std::ostream& out ) const
00114 {
00115     for( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
00116     {
00117         out << ( *iter );
00118         if( ( iter + 1 ) != m_selected.end() )
00119         {
00120             out << ";";
00121         }
00122     }
00123     return out;
00124 }
00125 
00126 std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
00127 {
00128     return other.operator<<( out );
00129 }
00130 
00131 bool WItemSelector::operator==( const WItemSelector& other ) const
00132 {
00133     return ( ( m_selection == other.m_selection ) && ( m_selected == other.m_selected ) && ( m_valid == other.m_valid ) );
00134 }
00135 
00136 size_t WItemSelector::sizeAll() const
00137 {
00138     return m_selection->size();
00139 }
00140 
00141 size_t WItemSelector::size() const
00142 {
00143     return m_selected.size();
00144 }
00145 
00146 const boost::shared_ptr< WItemSelectionItem > WItemSelector::atAll( size_t index ) const
00147 {
00148     return m_selection->at( index );
00149 }
00150 
00151 const boost::shared_ptr< WItemSelectionItem > WItemSelector::at( size_t index ) const
00152 {
00153     return m_selection->at( getItemIndexOfSelected( index ) );
00154 }
00155 
00156 size_t WItemSelector::getItemIndexOfSelected( size_t index ) const
00157 {
00158     return m_selected.at( index );
00159 }
00160 
00161 bool WItemSelector::empty() const
00162 {
00163     return ( size() == 0 );
00164 }
00165 
00166 void WItemSelector::invalidate()
00167 {
00168     m_valid = false;
00169 }
00170 
00171 bool WItemSelector::isValid() const
00172 {
00173     return m_valid;
00174 }
00175 
00176 WItemSelector WItemSelector::createSelector( const IndexList& selected ) const
00177 {
00178     WItemSelector s = WItemSelector( m_selection, selected );
00179     return s;
00180 }
00181 
00182 void WItemSelector::lock()
00183 {
00184     // 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
00185     // ReadTickets are reference counted.
00186     m_lock = m_selection->getReadTicket();
00187 }
00188 
00189 void WItemSelector::unlock()
00190 {
00191     m_lock.reset();
00192 }
00193 
00194 WItemSelector::operator unsigned int() const
00195 {
00196     return getItemIndexOfSelected( 0 );
00197 }
00198 
00199 WItemSelector::IndexList WItemSelector::getIndexList() const
00200 {
00201     return m_selected;
00202 }
00203