OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 <boost/lexical_cast.hpp>
00029 
00030 #include "WStringUtils.h"
00031 #include "WItemSelection.h"
00032 
00033 #include "WItemSelector.h"
00034 
00035 WItemSelector::WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected ):
00036     m_selection( selection ),
00037     m_selected( selected ),
00038     m_invalidateSignalConnection(),
00039     m_valid( true )
00040 {
00041     // initialize members
00042     m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00043 }
00044 
00045 WItemSelector::WItemSelector( const WItemSelector& other ):
00046     m_selection( other.m_selection ),
00047     m_selected( other.m_selected ),
00048     m_valid( other.m_valid )
00049 {
00050     m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00051 }
00052 
00053 WItemSelector& WItemSelector::operator=( const WItemSelector & other )
00054 {
00055     if( this != &other ) // protect against invalid self-assignment
00056     {
00057         m_selection = other.m_selection;
00058         m_selected = other.m_selected;
00059         m_valid = other.m_valid;
00060 
00061         m_invalidateSignalConnection = m_selection->getChangeCondition()->subscribeSignal( boost::bind( &WItemSelector::invalidate, this ) );
00062     }
00063 
00064     // by convention, always return *this
00065     return *this;
00066 }
00067 
00068 WItemSelector::~WItemSelector()
00069 {
00070     // cleanup
00071     m_invalidateSignalConnection.disconnect();
00072 }
00073 
00074 WItemSelector WItemSelector::newSelector( IndexList selected ) const
00075 {
00076     return createSelector( selected );
00077 }
00078 
00079 WItemSelector WItemSelector::newSelector( size_t selected ) const
00080 {
00081     IndexList n = m_selected;
00082     n.push_back( selected );
00083     return createSelector( n );
00084 }
00085 
00086 WItemSelector WItemSelector::newSelector( const std::string asString ) const
00087 {
00088     std::vector<std::string> tokens;
00089     tokens = string_utils::tokenize( asString, ";" );
00090 
00091     IndexList l;
00092     for( size_t i = 0; i < tokens.size(); ++i )
00093     {
00094         l.push_back( boost::lexical_cast< size_t >( tokens[i] ) );
00095     }
00096 
00097     return createSelector( l );
00098 }
00099 
00100 WItemSelector WItemSelector::newSelector() const
00101 {
00102     WItemSelector s( *this );
00103     s.m_valid = true;
00104     // iterate selected items to remove items with invalid index
00105     for( IndexList::iterator i = s.m_selected.begin(); i != s.m_selected.end(); ++i )
00106     {
00107         if( ( *i ) >= m_selection->size() )
00108         {
00109             s.m_selected.erase( i );
00110         }
00111     }
00112     return s;
00113 }
00114 
00115 std::ostream& WItemSelector::operator<<( std::ostream& out ) const
00116 {
00117     for( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
00118     {
00119         out << ( *iter );
00120         if( ( iter + 1 ) != m_selected.end() )
00121         {
00122             out << ";";
00123         }
00124     }
00125     return out;
00126 }
00127 
00128 std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
00129 {
00130     return other.operator<<( out );
00131 }
00132 
00133 bool WItemSelector::operator==( const WItemSelector& other ) const
00134 {
00135     return ( ( m_selection == other.m_selection ) && ( m_selected == other.m_selected ) && ( m_valid == other.m_valid ) );
00136 }
00137 
00138 size_t WItemSelector::sizeAll() const
00139 {
00140     return m_selection->size();
00141 }
00142 
00143 size_t WItemSelector::size() const
00144 {
00145     return m_selected.size();
00146 }
00147 
00148 const boost::shared_ptr< WItemSelectionItem > WItemSelector::atAll( size_t index ) const
00149 {
00150     return m_selection->at( index );
00151 }
00152 
00153 const boost::shared_ptr< WItemSelectionItem > WItemSelector::at( size_t index ) const
00154 {
00155     return m_selection->at( getItemIndexOfSelected( index ) );
00156 }
00157 
00158 size_t WItemSelector::getItemIndexOfSelected( size_t index ) const
00159 {
00160     return m_selected.at( index );
00161 }
00162 
00163 bool WItemSelector::empty() const
00164 {
00165     return ( size() == 0 );
00166 }
00167 
00168 void WItemSelector::invalidate()
00169 {
00170     m_valid = false;
00171 }
00172 
00173 bool WItemSelector::isValid() const
00174 {
00175     return m_valid;
00176 }
00177 
00178 WItemSelector WItemSelector::createSelector( const IndexList& selected ) const
00179 {
00180     WItemSelector s = WItemSelector( m_selection, selected );
00181     return s;
00182 }
00183 
00184 void WItemSelector::lock()
00185 {
00186     // 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
00187     // ReadTickets are reference counted.
00188     m_lock = m_selection->getReadTicket();
00189 }
00190 
00191 void WItemSelector::unlock()
00192 {
00193     m_lock.reset();
00194 }
00195 
00196 WItemSelector::operator unsigned int() const
00197 {
00198     return getItemIndexOfSelected( 0 );
00199 }
00200 
00201 WItemSelector::IndexList WItemSelector::getIndexList() const
00202 {
00203     return m_selected;
00204 }
00205 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends