OpenWalnut  1.4.0
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends
WItemSelector Class Reference

This class represents a subset of a WItemSelection. More...

#include <WItemSelector.h>

List of all members.

Public Types

typedef std::vector< size_tIndexList
 The type used for storing index lists.

Public Member Functions

 WItemSelector (const WItemSelector &other)
 Copy constructor.
WItemSelectoroperator= (const WItemSelector &other)
 Copy assignment.
virtual ~WItemSelector ()
 Destructor.
WItemSelector newSelector (IndexList selected) const
 Creates a new valid instance with the specified items selected.
WItemSelector newSelector (size_t selected) const
 Creates a new valid instance with the specified items selected.
WItemSelector newSelector (const std::string asString) const
 Creates a new valid instance with the specified items selected.
WItemSelector newSelector () const
 Creates a new selector, but basing on this instance as old one.
bool operator== (const WItemSelector &other) const
 Compares two selector.
std::ostream & operator<< (std::ostream &out) const
 Write a selection in string representation to the given output stream.
virtual size_t sizeAll () const
 Gives the count of elements in the set of selectable items.
virtual size_t size () const
 The number of selected items.
virtual bool empty () const
 True if the selection is empty.
virtual const
boost::shared_ptr
< WItemSelectionItem
atAll (size_t index) const
 Gets the item with the given index from the WItemSelection.
virtual const
boost::shared_ptr
< WItemSelectionItem
at (size_t index) const
 Gets the selected item with the given index.
virtual size_t getItemIndexOfSelected (size_t index) const
 Helps to get the index of an selected item in the WItemSelection.
virtual bool isValid () const
 Checks whether the selection is valid anymore.
void lock ()
 Read locks the underlying selection.
void unlock ()
 Unlocks the selection again.
 operator unsigned int () const
 Allow cast from selector to unsigned int.
IndexList getIndexList () const
 Casts the selector to a list of indices currently selected.

Protected Member Functions

 WItemSelector (boost::shared_ptr< WItemSelection > selection, IndexList selected)
 Constructor creates an selector for the specified selection of items.

Protected Attributes

boost::shared_ptr< WItemSelectionm_selection
 The selection handled by this selector.
IndexList m_selected
 The list of items currently selected.
boost::signals2::connection m_invalidateSignalConnection
 Stores the connection made using WItemSelection::subscribeInvalidateSignal.

Private Member Functions

WItemSelector createSelector (const IndexList &selected) const
 Creates a new selector instance using the specified index list.
void invalidate ()
 Handles the case of invalidation.

Private Attributes

bool m_valid
 If true the selector is valid.
WItemSelection::ReadTicket m_lock
 This locks prevents the selection to be modified during selector iteration.

Friends

class WItemSelection

Detailed Description

This class represents a subset of a WItemSelection.

It is a class for managing selections. The class is kept very restrictive. The selection can't be edited after the instantiation of the class to keep the interface clean, easily usable and consistent among multiple threads. So please DO NOT extend it to provide methods for changing it!

This class can be seen as some kind of special "iterator" providing access to the underlying set without allowing it to be modified. The class provides methods to access the whole set and the subset represented by itself. The restrictive interface ensures thread-safety and enforces that each new selection is done by a new instance of this class, which is needed by the WPropertyVariable to work properly.

Notes:
the protected constructor avoids instance creation of classes not the WItemSelection. This is restrictive but needed. Nobody can create instances of it, changing the underlying WItemSelection and using it as selector for another ItemSelection instance.

Definition at line 52 of file WItemSelector.h.


Member Typedef Documentation

typedef std::vector< size_t > WItemSelector::IndexList

The type used for storing index lists.

It is a list of integer correlating with the elements in the managed WItemSelection class.

Definition at line 59 of file WItemSelector.h.


Constructor & Destructor Documentation

Copy constructor.

Creates a new copy of the selector and ensure proper signal subscriptions to the underlying selection.

Parameters:
otherthe selector to copy

Definition at line 43 of file WItemSelector.cpp.

References invalidate(), m_invalidateSignalConnection, and m_selection.

Referenced by createSelector().

Destructor.

Definition at line 66 of file WItemSelector.cpp.

References m_invalidateSignalConnection.

WItemSelector::WItemSelector ( boost::shared_ptr< WItemSelection selection,
IndexList  selected 
) [protected]

Constructor creates an selector for the specified selection of items.

Noting is selected after construction.

Parameters:
selectionthe selection handled by this instance
selectedthe set of selected items

Definition at line 33 of file WItemSelector.cpp.

References invalidate(), m_invalidateSignalConnection, and m_selection.


Member Function Documentation

const boost::shared_ptr< WItemSelectionItem > WItemSelector::at ( size_t  index) const [virtual]

Gets the selected item with the given index.

This is not the same index as the element has in the corresponding WItemSelection! This method is especially useful to iterate the through the selected items.

Parameters:
indexthe index
Returns:
the item

Definition at line 151 of file WItemSelector.cpp.

References getItemIndexOfSelected(), and m_selection.

const boost::shared_ptr< WItemSelectionItem > WItemSelector::atAll ( size_t  index) const [virtual]

Gets the item with the given index from the WItemSelection.

This index does not equal the index of the same item for at. This method is useful to go through the list of ALL items (not only the selected).

Parameters:
indexthe index
Returns:
the item

Definition at line 146 of file WItemSelector.cpp.

References m_selection.

WItemSelector WItemSelector::createSelector ( const IndexList selected) const [private]

Creates a new selector instance using the specified index list.

Handles all needed signal subscription stuff.

Parameters:
selectedthe index list of selected items
Returns:
new selector

Definition at line 176 of file WItemSelector.cpp.

References m_selection, and WItemSelector().

Referenced by newSelector().

bool WItemSelector::empty ( ) const [virtual]

True if the selection is empty.

Returns:
true if nothing is selected.

Definition at line 161 of file WItemSelector.cpp.

References size().

Casts the selector to a list of indices currently selected.

It contains the list of index in the corresponding WItemSelection. This is especially useful if the whole index list is needed without nasty iterations.

Returns:
the list of index.

Definition at line 199 of file WItemSelector.cpp.

References m_selected.

Referenced by WGEShaderPropertyDefineOptionsIndexAdapter< WPropSelection >::operator()().

Helps to get the index of an selected item in the WItemSelection.

This is somehow similar to at, but does not return the item but the index to it.

Parameters:
indexthe index in the selection (not the item index in WItemSelection)
Returns:
the index in WItemSelection.

Definition at line 156 of file WItemSelector.cpp.

References m_selected.

Referenced by at(), and operator unsigned int().

void WItemSelector::invalidate ( ) [private]

Handles the case of invalidation.

Definition at line 166 of file WItemSelector.cpp.

References m_valid.

Referenced by operator=(), and WItemSelector().

bool WItemSelector::isValid ( ) const [virtual]

Checks whether the selection is valid anymore.

If a selector is not valid anymore, you should ask the one providing the selectors (most probably a WPropSelection) for a new one.

Returns:
true if valid.

Definition at line 171 of file WItemSelector.cpp.

References m_valid.

Read locks the underlying selection.

This ensure, that the selection stays fixed as long as this selector is locked. This also ensures that no invalidation can be issued as long as this selector has the lock. BUT it is possible that an invalidation occurs while this selector waits. So please always check for validity of the selector ater locking.

Definition at line 182 of file WItemSelector.cpp.

References m_lock, and m_selection.

Creates a new valid instance with the specified items selected.

This is especially useful to simply create a new selection if only the old selection is known.

Notes:
Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using lock and unlock.
Parameters:
selectedthe selected items (their index in WItemSelection).
Returns:
the new selector instance

Definition at line 72 of file WItemSelector.cpp.

References createSelector().

Referenced by PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_SELECTION >::create().

Creates a new valid instance with the specified items selected.

This can be useful to add a certain index. The new selector has the selection from this AND the specified one. If you want to create a selector containing only one selected item, use the method that uses the IndexList.

Notes:
Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using lock and unlock.
Parameters:
selectedthe selected item (the index in WItemSelection).
Returns:
the new selector instance

Definition at line 77 of file WItemSelector.cpp.

References createSelector(), and m_selected.

WItemSelector WItemSelector::newSelector ( const std::string  asString) const

Creates a new valid instance with the specified items selected.

This is especially useful to simply create a new selection if only the string representing it is known. This somehow correlates to the << operator.

Notes:
Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using lock and unlock.
Parameters:
asStringthe selected items
Returns:
the new selector instance

Definition at line 84 of file WItemSelector.cpp.

References createSelector(), and string_utils::tokenize().

Creates a new selector, but basing on this instance as old one.

The new selector tries to keep the old selection but makes the internal selection list valid with the current underlying selection.

Notes:
Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using lock and unlock.
Returns:
the new (valid) selector.

Definition at line 98 of file WItemSelector.cpp.

References m_selected, m_selection, and m_valid.

WItemSelector::operator unsigned int ( ) const

Allow cast from selector to unsigned int.

Returns:
the index of the first selected item in the selection.

Definition at line 194 of file WItemSelector.cpp.

References getItemIndexOfSelected().

std::ostream & WItemSelector::operator<< ( std::ostream &  out) const

Write a selection in string representation to the given output stream.

Parameters:
outthe output stream where to put the information
Returns:
the output stream extended by the information of this selector

Definition at line 113 of file WItemSelector.cpp.

References m_selected.

WItemSelector & WItemSelector::operator= ( const WItemSelector other)

Copy assignment.

Creates a new copy of the selector and ensure proper signal subscriptions to the underlying selection.

Parameters:
otherthe selector to copy
Returns:
this.

Definition at line 51 of file WItemSelector.cpp.

References invalidate(), m_invalidateSignalConnection, m_selected, m_selection, and m_valid.

bool WItemSelector::operator== ( const WItemSelector other) const

Compares two selector.

They are assumed to be equal if the selected items are equal and if the underlying WItemSelection is the same.

Parameters:
otherthe selector
Returns:
true if equal

Definition at line 131 of file WItemSelector.cpp.

References m_selected, m_selection, and m_valid.

size_t WItemSelector::size ( ) const [virtual]

The number of selected items.

Returns:
the number of selected items.

Definition at line 141 of file WItemSelector.cpp.

References m_selected.

Referenced by empty().

size_t WItemSelector::sizeAll ( ) const [virtual]

Gives the count of elements in the set of selectable items.

This is size + number of unselected items.

Returns:
the number of all items in the item set.

Definition at line 136 of file WItemSelector.cpp.

References m_selection.

Unlocks the selection again.

Always call this after a lock.

Definition at line 189 of file WItemSelector.cpp.

References m_lock.


Member Data Documentation

boost::signals2::connection WItemSelector::m_invalidateSignalConnection [protected]

Stores the connection made using WItemSelection::subscribeInvalidateSignal.

Definition at line 259 of file WItemSelector.h.

Referenced by operator=(), WItemSelector(), and ~WItemSelector().

This locks prevents the selection to be modified during selector iteration.

Definition at line 284 of file WItemSelector.h.

Referenced by lock(), and unlock().

The list of items currently selected.

Definition at line 254 of file WItemSelector.h.

Referenced by getIndexList(), getItemIndexOfSelected(), newSelector(), operator<<(), operator=(), operator==(), and size().

boost::shared_ptr< WItemSelection > WItemSelector::m_selection [protected]

The selection handled by this selector.

Definition at line 249 of file WItemSelector.h.

Referenced by at(), atAll(), createSelector(), lock(), newSelector(), operator=(), operator==(), sizeAll(), and WItemSelector().

bool WItemSelector::m_valid [private]

If true the selector is valid.

Definition at line 279 of file WItemSelector.h.

Referenced by invalidate(), isValid(), newSelector(), operator=(), and operator==().


The documentation for this class was generated from the following files: