~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/WItemSelector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
45
WItemSelector::WItemSelector( const WItemSelector& other ):
 
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
 
 
53
WItemSelector& WItemSelector::operator=( const WItemSelector & other )
 
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
 
 
68
WItemSelector::~WItemSelector()
 
69
{
 
70
    // cleanup
 
71
    m_invalidateSignalConnection.disconnect();
 
72
}
 
73
 
 
74
WItemSelector WItemSelector::newSelector( IndexList selected ) const
 
75
{
 
76
    return createSelector( selected );
 
77
}
 
78
 
 
79
WItemSelector WItemSelector::newSelector( size_t selected ) const
 
80
{
 
81
    IndexList n = m_selected;
 
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
 
 
100
WItemSelector WItemSelector::newSelector() const
 
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
 
 
138
size_t WItemSelector::sizeAll() const
 
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
 
 
163
bool WItemSelector::empty() const
 
164
{
 
165
    return ( size() == 0 );
 
166
}
 
167
 
 
168
void WItemSelector::invalidate()
 
169
{
 
170
    m_valid = false;
 
171
}
 
172
 
 
173
bool WItemSelector::isValid() const
 
174
{
 
175
    return m_valid;
 
176
}
 
177
 
 
178
WItemSelector WItemSelector::createSelector( const IndexList& selected ) const
 
179
{
 
180
    WItemSelector s = WItemSelector( m_selection, selected );
 
181
    return s;
 
182
}
 
183
 
 
184
void WItemSelector::lock()
 
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
 
 
191
void WItemSelector::unlock()
 
192
{
 
193
    m_lock.reset();
 
194
}
 
195
 
 
196
WItemSelector::operator unsigned int() const
 
197
{
 
198
    return getItemIndexOfSelected( 0 );
 
199
}
 
200
 
 
201
WItemSelector::IndexList WItemSelector::getIndexList() const
 
202
{
 
203
    return m_selected;
 
204
}
 
205