OpenWalnut  1.4.0
WItemSelection.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 "WLogger.h"
00029 
00030 #include "exceptions/WOutOfBounds.h"
00031 #include "exceptions/WNameNotUnique.h"
00032 #include "WItemSelector.h"
00033 
00034 #include "WItemSelection.h"
00035 
00036 WItemSelection::WItemSelection() :
00037                 WSharedSequenceContainer< std::vector< boost::shared_ptr< WItemSelectionItem > > >()
00038 {
00039     // initialize members
00040 }
00041 
00042 WItemSelection::~WItemSelection()
00043 {
00044     // cleanup
00045 }
00046 
00047 WItemSelector WItemSelection::getSelectorAll()
00048 {
00049     WItemSelector::IndexList l;
00050     ReadTicket r = getReadTicket();
00051 
00052     for( size_t i = 0; i < r->get().size(); ++i )
00053     {
00054         l.push_back( i );
00055     }
00056     return WItemSelector( shared_from_this(), l );
00057 }
00058 
00059 WItemSelector WItemSelection::getSelectorNone()
00060 {
00061     WItemSelector::IndexList l;
00062     ReadTicket r = getReadTicket();
00063     return WItemSelector( shared_from_this(), l );
00064 }
00065 
00066 WItemSelector WItemSelection::getSelectorFirst()
00067 {
00068     WItemSelector::IndexList l;
00069     ReadTicket r = getReadTicket();
00070 
00071     if( r->get().size() >= 1 )
00072     {
00073         l.push_back( 0 );
00074     }
00075     return WItemSelector( shared_from_this(), l );
00076 }
00077 
00078 WItemSelector WItemSelection::getSelectorLast()
00079 {
00080     WItemSelector::IndexList l;
00081     ReadTicket r = getReadTicket();
00082 
00083     if( r->get().size() >= 1 )
00084     {
00085         l.push_back( r->get().size() - 1 );
00086     }
00087     return WItemSelector( shared_from_this(), l );
00088 }
00089 
00090 WItemSelector WItemSelection::getSelector( size_t item )
00091 {
00092     WItemSelector::IndexList l;
00093     ReadTicket r = getReadTicket();
00094 
00095     if( r->get().size() <= item )
00096     {
00097         throw WOutOfBounds( std::string( "The specified item does not exist." ) );
00098     }
00099     l.push_back( item );
00100     return WItemSelector( shared_from_this(), l );
00101 }
00102 
00103 void WItemSelection::addItem( std::string name, std::string description, const char** icon )
00104 {
00105     push_back( boost::shared_ptr< WItemSelectionItem >( new WItemSelectionItem( name, description, icon ) ) );
00106 }
00107 
00108 void WItemSelection::addItem( boost::shared_ptr< WItemSelectionItem > item )
00109 {
00110     push_back( item );
00111 }
00112