Commit 8e1d30a5 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - completed selection + selector class

parent bf90b4fe
......@@ -95,3 +95,13 @@ WItemSelector WItemSelection::getSelector( size_t item )
return WItemSelector( shared_from_this(), l );
}
size_t WItemSelection::size() const
{
return m_items.size();
}
WItemSelection::Item WItemSelection::at( size_t index ) const
{
return m_items.at( index );
}
......@@ -44,6 +44,10 @@ class WItemSelector;
class WItemSelection: public boost::enable_shared_from_this< WItemSelection >
{
public:
/**
* For shortening, it is the type of an item
*/
typedef std::pair< std::string, std::string > Item;
/**
* Default constructor.
......@@ -95,12 +99,24 @@ public:
*/
virtual WItemSelector getSelector( size_t item );
protected:
/**
* The number of selectable items.
*
* \return the number of items.
*/
virtual size_t size() const;
/**
* For shortening, it is the type of an item
* Gets the item with the given index. This is not the same index as the element has in the corresponding WItemSelector!
* This method is especially useful to iterate the through all items.
*
* \param index the index
*
* \return the item
*/
typedef std::pair< std::string, std::string > Item;
virtual Item at( size_t index ) const;
protected:
/**
* List of items.
......
......@@ -22,6 +22,12 @@
//
//---------------------------------------------------------------------------
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>
#include "WStringUtils.h"
#include "WItemSelection.h"
#include "WItemSelector.h"
......@@ -38,16 +44,66 @@ WItemSelector::~WItemSelector()
// cleanup
}
WItemSelector WItemSelector::newSelector( IndexList selected )
WItemSelector WItemSelector::newSelector( IndexList selected ) const
{
return WItemSelector( m_selection, selected );
}
std::ostream& operator<<( std::ostream& out, const WItemSelector& c )
WItemSelector WItemSelector::newSelector( const std::string asString ) const
{
std::vector<std::string> tokens;
tokens = string_utils::tokenize( asString, ";" );
IndexList l;
for ( size_t i = 0; i < tokens.size(); ++i )
{
l.push_back( boost::lexical_cast< size_t >( tokens[i] ) );
}
return newSelector( l );
}
std::ostream& WItemSelector::operator<<( std::ostream& out )
{
for ( WItemSelector::IndexList::const_iterator iter = m_selected.begin(); iter != m_selected.end(); ++iter )
{
out << ( *iter );
if ( ( iter + 1 ) != m_selected.end() )
{
out << ";";
}
}
return out;
}
std::ostream& operator<<( std::ostream& out, const WItemSelector& other )
{
out << other;
return out;
}
bool WItemSelector::operator==( const WItemSelector& other ) const
{
return ( ( m_selection == other.m_selection ) && ( m_selected == m_selected ) );
}
size_t WItemSelector::sizeAll() const
{
return m_selection->size();
}
size_t WItemSelector::size() const
{
return m_selected.size();
}
WItemSelection::Item WItemSelector::atAll( size_t index ) const
{
return m_selection->at( index );
}
std::istream& operator>>( std::istream& in, WItemSelector& c )
WItemSelection::Item WItemSelector::at( size_t index ) const
{
return m_selection->at( m_selected.at( index ) );
}
......@@ -28,16 +28,21 @@
#include <istream>
#include <ostream>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
class WItemSelection;
#include "WItemSelection.h"
/**
* 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.
*
* \note 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.
*/
......@@ -64,7 +69,69 @@ public:
*
* \return the new selector instance
*/
WItemSelector newSelector( IndexList selected );
WItemSelector newSelector( IndexList selected ) 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.
*
* \param asString the selected items
*
* \return the new selector instance
*/
WItemSelector newSelector( const std::string asString ) const;
/**
* Compares two selector. They are assumed to be equal if the selected items are equal and if the underlying WItemSelection is the same.
*
* \param other the selector
*
* \return true if equal
*/
bool operator==( const WItemSelector& other ) const;
/**
* Write a selection in string representation to the given output stream.
*
* \param out the output stream where to put the information
*
* \return the output stream extended by the information of this selector
*/
std::ostream& operator<<( std::ostream& out );
/**
* Gives the count of elements in the set of selectable items. This is \ref size + number of unselected items.
*
* \return the number of all items in the item set.
*/
virtual size_t sizeAll() const;
/**
* The number of selected items.
*
* \return the number of selected items.
*/
virtual size_t size() const;
/**
* Gets the item with the given index from the WItemSelection. This index does not equal the index of the same item for \ref at. This method
* is useful to go through the list of ALL items (not only the selected).
*
* \param index the index
*
* \return the item
*/
virtual WItemSelection::Item atAll( size_t index ) const;
/**
* 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.
*
* \param index the index
*
* \return the item
*/
virtual WItemSelection::Item at( size_t index ) const;
protected:
......@@ -91,13 +158,13 @@ private:
/**
* Write a selection in string representation to the given output stream.
*
* \param out the output stream where to put the information
* \param other the instance to write out
*
* \return the output stream extended by the information of this selector
*/
std::ostream& operator<<( std::ostream& out, const WItemSelector& c );
/**
* Read a selection in string representation from the given input stream.
*/
std::istream& operator>>( std::istream& in, WItemSelector& c );
std::ostream& operator<<( std::ostream& out, const WItemSelector& other );
#endif // WITEMSELECTOR_H
......@@ -32,8 +32,10 @@
#include <utility>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include "math/WPosition.h"
#include "WItemSelector.h"
#include "WColor.h"
template < typename T >
......@@ -93,7 +95,7 @@ namespace WPVBaseTypes
typedef bool PV_BOOL; //!< base type used for every WPVBool
typedef std::string PV_STRING; //!< base type used for every WPVString
typedef boost::filesystem::path PV_PATH; //!< base type used for every WPVFilename
typedef void* PV_SELECTION; //!< base type used for every WPVSelection
typedef WItemSelector PV_SELECTION; //!< base type used for every WPVSelection
typedef wmath::WPosition PV_POSITION; //!< base type used for every WPVPosition
typedef WColor PV_COLOR; //!< base type used for every WPVColor
......@@ -262,6 +264,30 @@ namespace PROPERTY_TYPE_HELPER
}
};
/**
* Class helping to create a new instance of the property content from an old one. This might be needed by some types (some need to have a
* predecessor for creation).
* You only need to specialize this class for types not allowing the direct use of boost::lexical_cast.
*/
template< typename T >
class WCreateFromString
{
public:
/**
* Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
*
* \param old the old value
* \param str the new value as string
*
* \return the new instance
*/
T create( const T& /*old*/, const std::string str )
{
return boost::lexical_cast< T >( str );
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
......@@ -370,6 +396,28 @@ namespace PROPERTY_TYPE_HELPER
}
};
/**
* Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
* serializable content which needs to be acquired from its predecessor instance.
*/
template<>
class WCreateFromString< WPVBaseTypes::PV_SELECTION >
{
public:
/**
* Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
*
* \param old the old value
* \param str the new value as string
*
* \return the new instance
*/
WPVBaseTypes::PV_SELECTION create( const WPVBaseTypes::PV_SELECTION& old, const std::string str )
{
return old.newSelector( str );
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
......
......@@ -513,7 +513,9 @@ bool WPropertyVariable< T >::setAsString( std::string value )
{
try
{
set( boost::lexical_cast< T >( value ) );
// use the helper class which can handle different kinds of properties for us
PROPERTY_TYPE_HELPER::WCreateFromString< T > h = PROPERTY_TYPE_HELPER::WCreateFromString< T >();
set( h.create( WFlag< T >::get(), value ) );
}
catch( const boost::bad_lexical_cast &e )
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment