OpenWalnut  1.4.0
WItemSelectionItemTyped.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WITEMSELECTIONITEMTYPED_H
00026 #define WITEMSELECTIONITEMTYPED_H
00027 
00028 #include <cstddef>
00029 #include <string>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include "WItemSelectionItem.h"
00034 
00035 /**
00036  * A derivation of WItemSelection which can store a value of any type.
00037  *
00038  * \note you can specify a reference type too. When using MyType& as type in this class, you can avoid unnecessary copy operations.
00039  *
00040  * \tparam the type to encapsulate
00041  */
00042 template< typename T >
00043 class WItemSelectionItemTyped: public WItemSelectionItem // NOLINT
00044 {
00045 public:
00046     /**
00047      * Abbreviation for a shared pointer.
00048      */
00049     typedef boost::shared_ptr< WItemSelectionItemTyped< T > > SPtr;
00050 
00051     /**
00052      * Abbreviation for a const shared pointer.
00053      */
00054     typedef boost::shared_ptr< const WItemSelectionItemTyped< T > > ConstSPtr;
00055 
00056     /**
00057      * The type of the value stored in here.
00058      */
00059     typedef T ValueType;
00060 
00061     /**
00062      * Constructs a new item with the specified values.
00063      *
00064      * \param value Value which is stored by the item.
00065      * \param name Name of item.
00066      * \param description Description, can be empty.
00067      * \param icon Icon, can be NULL.
00068      */
00069     WItemSelectionItemTyped( T value, std::string name, std::string description = "", const char** icon = NULL ) :
00070         WItemSelectionItem( name, description, icon ),
00071         m_value( value )
00072     {
00073     }
00074 
00075     /**
00076      * Destruction. Does NOT delete the icon!
00077      */
00078     virtual ~WItemSelectionItemTyped()
00079     {
00080     }
00081 
00082     /**
00083      * Create a instance of the item. This shortens the rather long call which would be needed to create a shared pointer of this class.
00084      *
00085      * \param value the value to store in the instance
00086      * \param name the name of item
00087      * \param description Description of the item. Can be empty.
00088      * \param icon the icon of the item. Can be NULL.
00089      *
00090      * \return a new instance pointer
00091      */
00092     static SPtr create( T value, std::string name, std::string description = "", const char** icon = NULL )
00093     {
00094         return SPtr( new WItemSelectionItemTyped< T >( value, name, description, icon ) );
00095     }
00096 
00097     /**
00098      * Returns the value. This const version is especially useful when using reference types for T.
00099      *
00100      * \return Value which is stored.
00101      */
00102     const T getValue() const
00103     {
00104         return m_value;
00105     }
00106 
00107     /**
00108      * Returns the value.
00109      *
00110      * \return Value which is stored.
00111      */
00112     T getValue()
00113     {
00114         return m_value;
00115     }
00116 
00117     /**
00118      * Sets a new value, which is associated with this item.
00119      *
00120      * \param value new value which should be stored by this item.
00121      */
00122     void setValue( T value )
00123     {
00124         m_value = value;
00125     }
00126 
00127 private:
00128     /**
00129      * Value which is stored by this item.
00130      */
00131     T m_value;
00132 };
00133 
00134 #endif  // WITEMSELECTIONITEMTYPED_H