Commit b59a73a9 authored by Christof Pieloth's avatar Christof Pieloth
Browse files

[ADD] Add WItemSelectionItemTyped which can stored a value. Modify ...

[ADD] Add WItemSelectionItemTyped which can stored a value. Modify  WItemSelectionItem and WItemSelection to add and cast to new type.
parent 6eb8bf84
......@@ -33,8 +33,8 @@
#include "WItemSelection.h"
WItemSelection::WItemSelection():
WSharedSequenceContainer< std::vector< boost::shared_ptr< WItemSelectionItem > > >()
WItemSelection::WItemSelection() :
WSharedSequenceContainer< std::vector< boost::shared_ptr< WItemSelectionItem > > >()
{
// initialize members
}
......@@ -105,3 +105,8 @@ void WItemSelection::addItem( std::string name, std::string description, const c
push_back( boost::shared_ptr< WItemSelectionItem >( new WItemSelectionItem( name, description, icon ) ) );
}
void WItemSelection::addItem( boost::shared_ptr< WItemSelectionItem > item )
{
push_back (item);
}
......@@ -37,7 +37,6 @@
#include "WSharedSequenceContainer.h"
#include "WItemSelectionItem.h"
class WItemSelector;
/**
......@@ -47,10 +46,10 @@ class WItemSelector;
* not implement any function that might change the item list, use the provided ones. If the item list changes, existing selectors get invalid
* automatically using the change condition of the inherited WSharedSequenceContainer.
*/
class WItemSelection: public boost::enable_shared_from_this< WItemSelection >,
public WSharedSequenceContainer< std::vector< boost::shared_ptr< WItemSelectionItem > > >
class WItemSelection: public boost::enable_shared_from_this< WItemSelection >, public WSharedSequenceContainer<
std::vector< boost::shared_ptr< WItemSelectionItem > > >
{
friend class WItemSelector; // for proper locking and unlocking
friend class WItemSelector; // for proper locking and unlocking
public:
/**
* Convenience typedef for a boost::shared_ptr< WItemSelection >
......@@ -118,7 +117,8 @@ public:
*
* \return the Item.
*/
static boost::shared_ptr< WItemSelectionItem > Item( std::string name, std::string description = "", const char** icon = NULL )
static boost::shared_ptr< WItemSelectionItem > Item( std::string name, std::string description = "",
const char** icon = NULL )
{
return boost::shared_ptr< WItemSelectionItem >( new WItemSelectionItem( name, description, icon ) );
}
......@@ -133,8 +133,12 @@ public:
*/
void addItem( std::string name, std::string description = "", const char** icon = NULL );
/**
* Method to add a new item, which can be derived from WItemSelectionItem.
*/
void addItem( boost::shared_ptr< WItemSelectionItem > item );
private:
};
#endif // WITEMSELECTION_H
......@@ -27,8 +27,6 @@
#include <string>
/**
* Class for keeping a single named item in a WItemSelection.
*/
......@@ -70,6 +68,14 @@ public:
*/
const char** getIcon() const;
/**
* Dynamic cast of the object, if a derivative of WItemSelectionItem was add to WItemSelection.
*/
template< typename T > T* getAs()
{
return dynamic_cast< T* >( this );
}
/**
* Compares this and another item using their names only.
*
......@@ -99,4 +105,3 @@ private:
};
#endif // WITEMSELECTIONITEM_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 "WItemSelectionItemTyped.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/>.
//
//---------------------------------------------------------------------------
#ifndef WITEMSELECTIONITEMTYPED_H
#define WITEMSELECTIONITEMTYPED_H
#include <cstddef>
#include <string>
#include "WItemSelectionItem.h"
/**
* A derivation of WItemSelection which can store a value of any type.
*/
template< typename T > class WItemSelectionItemTyped: public WItemSelectionItem // NOLINT
{
public:
/**
* Constructs a new item with the specified values.
*
* \param value Value which is stored by the item.
* \param name Name of item.
* \param description Description, can be empty.
* \param icon Icon, can be NULL.
*/
WItemSelectionItemTyped( T value, std::string name, std::string description = "", const char** icon = NULL ) :
WItemSelectionItem( name, description, icon ), m_value( value )
{
}
/**
* Destruction. Does NOT delete the icon!
*/
virtual ~WItemSelectionItemTyped()
{
}
/**
* Returns the value.
*/
T getValue() const
{
return m_value;
}
/**
* Sets a new value, which is associated with this item.
*/
void setValue( T value )
{
m_value = value;
}
private:
T m_value;
};
#endif // WITEMSELECTIONITEMTYPED_H
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