OpenWalnut  1.5.0dev
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 <memory>
30 #include <string>
31 
32 
33 #include "WItemSelectionItem.h"
34 
35 /**
36  * A derivation of WItemSelection which can store a value of any type.
37  *
38  * \note you can specify a reference type too. When using MyType& as type in this class, you can avoid unnecessary copy operations.
39  *
40  * \tparam the type to encapsulate
41  */
42 template< typename T >
44 {
45 public:
46  /**
47  * Abbreviation for a shared pointer.
48  */
49  typedef std::shared_ptr< WItemSelectionItemTyped< T > > SPtr;
50 
51  /**
52  * Abbreviation for a const shared pointer.
53  */
54  typedef std::shared_ptr< const WItemSelectionItemTyped< T > > ConstSPtr;
55 
56  /**
57  * The type of the value stored in here.
58  */
59  typedef T ValueType;
60 
61  /**
62  * Constructs a new item with the specified values.
63  *
64  * \param value Value which is stored by the item.
65  * \param name Name of item.
66  * \param description Description, can be empty.
67  * \param icon Icon, can be NULL.
68  */
69  WItemSelectionItemTyped( T value, std::string name, std::string description = "", const char** icon = NULL ) :
70  WItemSelectionItem( name, description, icon ),
71  m_value( value )
72  {
73  }
74 
75  /**
76  * Destruction. Does NOT delete the icon!
77  */
79  {
80  }
81 
82  /**
83  * Create a instance of the item. This shortens the rather long call which would be needed to create a shared pointer of this class.
84  *
85  * \param value the value to store in the instance
86  * \param name the name of item
87  * \param description Description of the item. Can be empty.
88  * \param icon the icon of the item. Can be NULL.
89  *
90  * \return a new instance pointer
91  */
92  static SPtr create( T value, std::string name, std::string description = "", const char** icon = NULL )
93  {
94  return SPtr( new WItemSelectionItemTyped< T >( value, name, description, icon ) );
95  }
96 
97  /**
98  * Returns the value. This const version is especially useful when using reference types for T.
99  *
100  * \return Value which is stored.
101  */
102  const T getValue() const
103  {
104  return m_value;
105  }
106 
107  /**
108  * Returns the value.
109  *
110  * \return Value which is stored.
111  */
113  {
114  return m_value;
115  }
116 
117  /**
118  * Sets a new value, which is associated with this item.
119  *
120  * \param value new value which should be stored by this item.
121  */
122  void setValue( T value )
123  {
124  m_value = value;
125  }
126 
127 private:
128  /**
129  * Value which is stored by this item.
130  */
132 };
133 
134 #endif // WITEMSELECTIONITEMTYPED_H
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.
static SPtr create(T value, std::string name, std::string description="", const char **icon=NULL)
Create a instance of the item.
const T getValue() const
Returns the value.
std::shared_ptr< WItemSelectionItemTyped< T > > SPtr
Abbreviation for a 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.
T getValue()
Returns the value.
virtual ~WItemSelectionItemTyped()
Destruction.
std::shared_ptr< const WItemSelectionItemTyped< T > > ConstSPtr
Abbreviation for a const shared pointer.
T ValueType
The type of the value stored in here.
Class for keeping a single named item in a WItemSelection.
std::shared_ptr< WItemSelectionItem > SPtr
Abbreviation for a shared pointer.