OpenWalnut  1.4.0
WItemSelectionItemTyped.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WITEMSELECTIONITEMTYPED_H
26 #define WITEMSELECTIONITEMTYPED_H
27 
28 #include <cstddef>
29 #include <string>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 
35 #include "WItemSelectionItem.h"
36 
37 /**
38  * A derivation of WItemSelection which can store a value of any type.
39  *
40  * \note you can specify a reference type too. When using MyType& as type in this class, you can avoid unnecessary copy operations.
41  *
42  * \tparam the type to encapsulate
43  */
44 template< typename T >
46 {
47 public:
48  /**
49  * Abbreviation for a shared pointer.
50  */
51  typedef boost::shared_ptr< WItemSelectionItemTyped< T > > SPtr;
52 
53  /**
54  * Abbreviation for a const shared pointer.
55  */
56  typedef boost::shared_ptr< const WItemSelectionItemTyped< T > > ConstSPtr;
57 
58  /**
59  * The type of the value stored in here.
60  */
61  typedef T ValueType;
62 
63  /**
64  * Constructs a new item with the specified values.
65  *
66  * \param value Value which is stored by the item.
67  * \param name Name of item.
68  * \param description Description, can be empty.
69  * \param icon Icon, can be NULL.
70  */
71  WItemSelectionItemTyped( T value, std::string name, std::string description = "", const char** icon = NULL ) :
72  WItemSelectionItem( name, description, icon ),
73  m_value( value )
74  {
75  }
76 
77  /**
78  * Destruction. Does NOT delete the icon!
79  */
81  {
82  }
83 
84  /**
85  * Create a instance of the item. This shortens the rather long call which would be needed to create a shared pointer of this class.
86  *
87  * \param value the value to store in the instance
88  * \param name the name of item
89  * \param description Description of the item. Can be empty.
90  * \param icon the icon of the item. Can be NULL.
91  *
92  * \return a new instance pointer
93  */
94  static SPtr create( T value, std::string name, std::string description = "", const char** icon = NULL )
95  {
96  return SPtr( new WItemSelectionItemTyped< T >( value, name, description, icon ) );
97  }
98 
99  /**
100  * Returns the value. This const version is especially useful when using reference types for T.
101  *
102  * \return Value which is stored.
103  */
104  const T getValue() const
105  {
106  return m_value;
107  }
108 
109  /**
110  * Returns the value.
111  *
112  * \return Value which is stored.
113  */
115  {
116  return m_value;
117  }
118 
119  /**
120  * Sets a new value, which is associated with this item.
121  *
122  * \param value new value which should be stored by this item.
123  */
124  void setValue( T value )
125  {
126  m_value = value;
127  }
128 
129 private:
130  /**
131  * Value which is stored by this item.
132  */
134 };
135 
136 #endif // WITEMSELECTIONITEMTYPED_H
boost::shared_ptr< const WItemSelectionItemTyped< T > > ConstSPtr
Abbreviation for a const shared pointer.
T m_value
Value which is stored by this item.
WItemSelectionItemTyped(T value, std::string name, std::string description="", const char **icon=NULL)
Constructs a new item with the specified values.
virtual ~WItemSelectionItemTyped()
Destruction.
static SPtr create(T value, std::string name, std::string description="", const char **icon=NULL)
Create a instance of the item.
T ValueType
The type of the value stored in here.
A derivation of WItemSelection which can store a value of any type.
void setValue(T value)
Sets a new value, which is associated with this item.
boost::shared_ptr< WItemSelectionItemTyped< T > > SPtr
Abbreviation for a shared pointer.
T getValue()
Returns the value.
Class for keeping a single named item in a WItemSelection.
const T getValue() const
Returns the value.