Commit b10998ff authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD] - simple and restrictive selection classes useful for WPVSelection properties

parent 24086589
......@@ -140,7 +140,7 @@ private:
std::ostream& operator<<( std::ostream& out, const WColor& c );
/**
* Write a color in string representation to the given input stream.
* Read a color in string representation from the given input stream.
*/
std::istream& operator>>( std::istream& in, WColor& c );
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WLogger.h"
#include "exceptions/WOutOfBounds.h"
#include "WItemSelector.h"
#include "WItemSelection.h"
WItemSelection::WItemSelection():
m_modifyable( true )
{
// initialize members
}
WItemSelection::~WItemSelection()
{
// cleanup
}
void WItemSelection::addItem( std::string name, std::string description )
{
if ( m_modifyable )
{
m_items.push_back( Item( name, description ) );
}
else
{
wlog::warn( "WItemSelection " ) << "You can not modify the selection list after a selector has been created.";
}
}
WItemSelector WItemSelection::getSelectorAll()
{
m_modifyable = false;
WItemSelector::IndexList l;
for ( size_t i = 0; i < m_items.size(); ++i )
{
l.push_back( i );
}
return WItemSelector( shared_from_this(), l );
}
WItemSelector WItemSelection::getSelectorNone()
{
m_modifyable = false;
WItemSelector::IndexList l;
return WItemSelector( shared_from_this(), l );
}
WItemSelector WItemSelection::getSelectorFirst()
{
m_modifyable = false;
WItemSelector::IndexList l;
if ( m_items.size() >= 1 )
{
l.push_back( 0 );
}
return WItemSelector( shared_from_this(), l );
}
WItemSelector WItemSelection::getSelector( size_t item )
{
m_modifyable = false;
WItemSelector::IndexList l;
if ( m_items.size() <= item )
{
throw WOutOfBounds( "The specified item does not exist." );
}
l.push_back( item );
return WItemSelector( shared_from_this(), l );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WITEMSELECTION_H
#define WITEMSELECTION_H
#include <vector>
#include <string>
#include <utility>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
class WItemSelector;
/**
* A class containing a list of named items. It is mainly a container for an std::vector but with the difference that there can be so called
* Selectors which are able to select some subset of the item set. This is especially useful in properties where item selection is needed. The
* class is kept very restrictive to keep the interface clean and sleek and to keep the item set consistent among several threads. So please do
* not implement any function that might change the item list. The would cause odd behaviour of all the WItemSelector instances. Items can only
* be added until the first Selector instance is created.
*/
class WItemSelection: public boost::enable_shared_from_this< WItemSelection >
{
public:
/**
* Default constructor.
*/
WItemSelection();
/**
* Destructor.
*/
virtual ~WItemSelection();
/**
* Adds an item to the list of selectable items.
*
* \param name the name
* \param description the description
*/
virtual void addItem( std::string name, std::string description );
/**
* Creates an default selection (all items selected). After the first call of this function, no more items can be added using \ref addItem.
*
* \return an selector.
*/
virtual WItemSelector getSelectorAll();
/**
* Creates an default selection (no items selected). After the first call of this function, no more items can be added using \ref addItem.
*
* \return an selector.
*/
virtual WItemSelector getSelectorNone();
/**
* Creates an default selection (first item selected). After the first call of this function, no more items can be added using \ref addItem.
* If no item is in the selection, nothing is selected.
*
* \return an selector.
*/
virtual WItemSelector getSelectorFirst();
/**
* Creates an default selection (a specified items selected). After the first call of this function, no more items can be added
* using \ref addItem.
*
* \param item the item to select.
*
* \return an selector.
*/
virtual WItemSelector getSelector( size_t item );
protected:
/**
* For shortening, it is the type of an item
*/
typedef std::pair< std::string, std::string > Item;
/**
* List of items.
*/
std::vector< Item > m_items;
/**
* True if the selection can be modified.
*/
bool m_modifyable;
private:
};
#endif // WITEMSELECTION_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WItemSelection.h"
#include "WItemSelector.h"
WItemSelector::WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected ):
m_selection( selection ),
m_selected( selected )
{
// initialize members
}
WItemSelector::~WItemSelector()
{
// cleanup
}
WItemSelector WItemSelector::newSelector( IndexList selected )
{
return WItemSelector( m_selection, selected );
}
std::ostream& operator<<( std::ostream& out, const WItemSelector& c )
{
}
std::istream& operator>>( std::istream& in, WItemSelector& c )
{
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WITEMSELECTOR_H
#define WITEMSELECTOR_H
#include <istream>
#include <ostream>
#include <vector>
#include <boost/shared_ptr.hpp>
class WItemSelection;
/**
* 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!
*
* \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.
*/
class WItemSelector
{
friend class WItemSelection;
public:
/**
* The type used for storing index lists. It is a list of integer correlating with the elements in the managed WItemSelection class.
*/
typedef std::vector< size_t > IndexList;
/**
* Destructor.
*/
virtual ~WItemSelector();
/**
* 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.
*
* \param selected the selected items (their index in WItemSelection).
*
* \return the new selector instance
*/
WItemSelector newSelector( IndexList selected );
protected:
/**
* Constructor creates an selector for the specified selection of items. Noting is selected after construction.
*
* \param selection the selection handled by this instance
* \param selected the set of selected items
*/
WItemSelector( boost::shared_ptr< WItemSelection > selection, IndexList selected );
/**
* The selection handled by this selector.
*/
boost::shared_ptr< WItemSelection > m_selection;
/**
* The list of items currently selected.
*/
IndexList m_selected;
private:
};
/**
* Write a selection in string representation to the given output stream.
*/
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 );
#endif // WITEMSELECTOR_H
......@@ -94,7 +94,6 @@ namespace WPVBaseTypes
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 std::list< std::pair< std::string, bool > > 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
......
......@@ -303,6 +303,23 @@ public:
*/
PropertyConstraintMax getMax();
/**
* This replaces all existing constraints of a certain type by a new specified constraint.
*
* \param constraint the new constraint
* \param type the type of constraints to replace
*/
void replaceConstraint( boost::shared_ptr< PropertyConstraint > constraint, PROPERTYCONSTRAINT_TYPE type );
/**
* This replaces all existing constraints of a certain type by a new specified constraint.
*
* \param constraint the new constraint
* \param type the type of constraints to replace
* \return the constraint created
*/
boost::shared_ptr< PropertyConstraint > replaceConstraint( PROPERTYCONSTRAINT_TYPE constraint, PROPERTYCONSTRAINT_TYPE type );
/**
* Method searching the first appearance of a constrained with the specified type.
*
......@@ -389,7 +406,9 @@ template < typename T >
WPropertyVariable< T >::WPropertyVariable( std::string name, std::string description, const T& initial ):
WFlag< T >( new WCondition(), initial ),
WPropertyBase( name, description ),
m_constraintsChanged( new WCondition() )
m_constraintsChanged( new WCondition() ),
m_constraints(),
m_constraintsAccess( m_constraints.getAccessObject() )
{
updateType();
......@@ -402,7 +421,9 @@ template < typename T >
WPropertyVariable< T >::WPropertyVariable( std::string name, std::string description, const T& initial, boost::shared_ptr< WCondition > condition ):
WFlag< T >( condition, initial ),
WPropertyBase( name, description ),
m_constraintsChanged( new WCondition() )
m_constraintsChanged( new WCondition() ),
m_constraints(),
m_constraintsAccess( m_constraints.getAccessObject() )
{
updateType();
......@@ -416,7 +437,9 @@ WPropertyVariable< T >::WPropertyVariable( std::string name, std::string descrip
PropertyChangeNotifierType notifier ):
WFlag< T >( new WCondition(), initial ),
WPropertyBase( name, description ),
m_constraintsChanged( new WCondition() )
m_constraintsChanged( new WCondition() ),
m_constraints(),
m_constraintsAccess( m_constraints.getAccessObject() )
{
updateType();
......@@ -434,7 +457,9 @@ WPropertyVariable< T >::WPropertyVariable( std::string name, std::string descrip
PropertyChangeNotifierType notifier ):
WFlag< T >( condition, initial ),
WPropertyBase( name, description ),
m_constraintsChanged( new WCondition() )
m_constraintsChanged( new WCondition() ),
m_constraints(),
m_constraintsAccess( m_constraints.getAccessObject() )
{
updateType();
......@@ -569,11 +594,7 @@ template < typename T >
boost::shared_ptr< WPropertyConstraintMin< T > > WPropertyVariable< T >::setMin( T min )
{
boost::shared_ptr< WPropertyConstraintMin< T > > c = minConstraint( min );
m_constraintsAccess->beginWrite();
removeConstraints( PC_MIN, false );
m_constraintsAccess->get().insert( c );
m_constraintsAccess->endWrite();
m_constraintsChanged->notify();
replaceConstraint( c, PC_MIN );
return c;
}
......@@ -581,11 +602,26 @@ template < typename T >
boost::shared_ptr< WPropertyConstraintMax< T > > WPropertyVariable< T >::setMax( T max )
{
boost::shared_ptr< WPropertyConstraintMax< T > > c = maxConstraint( max );
replaceConstraint( c, PC_MAX );
return c;
}
template < typename T >
void WPropertyVariable< T >::replaceConstraint( boost::shared_ptr< PropertyConstraint > constraint, PROPERTYCONSTRAINT_TYPE type )
{
m_constraintsAccess->beginWrite();
removeConstraints( PC_MAX, false );
m_constraintsAccess->get().insert( c );
removeConstraints( type, false );
m_constraintsAccess->get().insert( constraint );
m_constraintsAccess->endWrite();
m_constraintsChanged->notify();
}
template < typename T >
boost::shared_ptr< typename WPropertyVariable< T >::PropertyConstraint >
WPropertyVariable< T >::replaceConstraint( PROPERTYCONSTRAINT_TYPE constraint, PROPERTYCONSTRAINT_TYPE type )
{
boost::shared_ptr< typename WPropertyVariable< T >::PropertyConstraint > c = PropertyConstraint::create( constraint );
replaceConstraint( c, type );
return c;
}
......
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