OpenWalnut  1.5.0dev
WPropertyBase.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 WPROPERTYBASE_H
26 #define WPROPERTYBASE_H
27 
28 #include <memory>
29 #include <string>
30 
31 #include <boost/function.hpp>
32 #include <boost/signals2/signal.hpp>
33 
34 #include "WCondition.h"
35 #include "WConditionSet.h"
36 #include "WProperties_Fwd.h"
37 #include "WStringUtils.h"
38 
39 /**
40  * Forward declaration for the PropertyGroupBase.
41  */
42 class WPropertyGroupBase;
43 
44 /**
45  * Abstract base class for all properties. Simply provides name and type information.
46  */
47 class WPropertyBase: public std::enable_shared_from_this< WPropertyBase >
48 {
49 public:
50  /**
51  * Convenience typedef for a std::shared_ptr< WPropertyBase >
52  */
53  typedef std::shared_ptr< WPropertyBase > SPtr;
54 
55  /**
56  * Convenience typedef for a std::shared_ptr< const WPropertyBase >
57  */
58  typedef std::shared_ptr< const WPropertyBase > ConstSPtr;
59 
60  /**
61  * Create an empty named property.
62  *
63  * \param name the name of the property
64  * \param description the description of the property
65  */
66  WPropertyBase( std::string name, std::string description );
67 
68  /**
69  * Copy constructor. Creates a deep copy of this property. As boost::signals2 and condition variables are non-copyable, new instances get
70  * created. The subscriptions to a signal are LOST as well as all listeners to a condition.
71  *
72  * \param from the instance to copy.
73  */
74  explicit WPropertyBase( const WPropertyBase& from );
75 
76  /**
77  * Destructor.
78  */
79  virtual ~WPropertyBase();
80 
81  /**
82  * This method clones a property and returns the clone. It does a deep copy and, in contrast to a copy constructor, creates property with the
83  * correct type without explicitly requiring the user to specify it. It creates a NEW change condition and change signal. This means, alls
84  * subscribed signal handlers are NOT copied.
85  *
86  * \note this simply ensures the copy constructor of the runtime type is issued.
87  *
88  * \return the deep clone of this property.
89  */
90  virtual std::shared_ptr< WPropertyBase > clone() = 0;
91 
92  /**
93  * Gets the name of the class.
94  *
95  * \return the name.
96  */
97  std::string getName() const;
98 
99  /**
100  * Gets the description of the property.
101  *
102  * \return the description
103  */
104  std::string getDescription() const;
105 
106  /**
107  * Determines whether the property is hidden or not.
108  *
109  * \return true if hidden
110  */
111  bool isHidden() const;
112 
113  /**
114  * Sets the property hidden. This flag is especially used by the GUI.
115  *
116  * \param hidden true if it should be hidden.
117  */
118  void setHidden( bool hidden = true );
119 
120  /**
121  * Gets the real WPropertyVariable type of this instance.
122  *
123  * \return the real type.
124  */
125  virtual PROPERTY_TYPE getType() const;
126 
127  /**
128  * Gets the purpose of a property. See PROPERTY_PURPOSE for more details. For short: it helps the GUI and others to understand what a module
129  * (or whomever created this property) intents with this property. Typically this value is PV_PURPOSE_PARAMETER, meaning that it is used to
130  * tune the behaviour of a module.
131  *
132  * \note always assume this to be a hint. It does not actually prevent someone from writing or interpreting a parameter property as an
133  * information property.
134  *
135  * \see PROPERTY_PURPOSE
136  * \return the purpose.
137  */
138  virtual PROPERTY_PURPOSE getPurpose() const;
139 
140  /**
141  * Sets the purpose of the property. See \ref getPurpose for more details. You generally should avoid setting this value after
142  * initialization.
143  *
144  * \param purpose the purpose to set.
145  */
146  virtual void setPurpose( PROPERTY_PURPOSE purpose );
147 
148  /**
149  * This methods allows properties to be set by a string value. This is especially useful when a property is only available as string and the
150  * real type of the property is unknown. This is a shortcut for casting the property and then setting the lexically casted value.
151  *
152  * \param value the new value to set.
153  *
154  * \return true if value could be set.
155  */
156  virtual bool setAsString( std::string value ) = 0;
157 
158  /**
159  * Returns the current value as a string. This is useful for debugging or project files. It is not implemented as << operator, since the <<
160  * should also print min/max constraints and so on. This simply is the value.
161  *
162  * \return the value as a string.
163  */
164  virtual std::string getAsString() = 0;
165 
166  /**
167  * Shortcut to set a property with a given value. This basically is a shortcut to \ref setAsString with the value being cast to string.
168  *
169  * \tparam T type of the input value
170  * \param value the value to set
171  *
172  * \return true if successful.
173  */
174  template< typename T >
175  bool set( const T& value );
176 
177  /**
178  * This method returns a condition which gets fired whenever the property changes somehow. It is fired when:
179  * \li \ref setHidden is called and the hidden state changes
180  * \li \ref setAsString is called and the value changes
181  * \li WPropertyVariable::set is called and the value changes (regardless of suppression during set)
182  * \li WPropertyVariable::setMin/setMax is called and the value changes
183  * \li WPropertyVariable::addConstraint is called
184  * \li WPropertyVariable::removeConstraints is called
185  * \li WProperties::addProperty is called
186  * \li WProperties::removeProperty is called
187  * \li WProperties::addPropertyGroup is called
188  * This is especially useful if you simply want to know that something has happened.
189  *
190  * \return a condition notified whenever something changes.
191  */
192  virtual std::shared_ptr< WCondition > getUpdateCondition() const;
193 
194  /**
195  * Sets the value from the specified property to this one. This is especially useful to copy a value without explicitly casting/knowing the
196  * dynamic type of the property.
197  *
198  * \param value the new value.
199  * \param recommendedOnly if true, property types which support recommended values apply the given value as recommendation.
200  *
201  * \return true if the value has been accepted.
202  */
203  virtual bool set( std::shared_ptr< WPropertyBase > value, bool recommendedOnly = false ) = 0;
204 
205  /////////////////////////////////////////////////////////////////////////////////////////////
206  // Helpers for easy conversion to the possible types
207  /////////////////////////////////////////////////////////////////////////////////////////////
208 
209  /**
210  * Helper converts this instance to its native type.
211  *
212  * \return the property as integer property
213  */
214  WPropInt toPropInt();
215 
216  /**
217  * Helper converts this instance to its native type.
218  *
219  * \return the property as double property
220  */
221  WPropDouble toPropDouble();
222 
223  /**
224  * Helper converts this instance to its native type.
225  *
226  * \return the property as bool property
227  */
228  WPropBool toPropBool();
229 
230  /**
231  * Helper converts this instance to its native type.
232  *
233  * \return the property as string property
234  */
235  WPropString toPropString();
236 
237  /**
238  * Helper converts this instance to its native type.
239  *
240  * \return the property as path property
241  */
242  WPropFilename toPropFilename();
243 
244  /**
245  * Helper converts this instance to its native type.
246  *
247  * \return the property as selection property
248  */
249  WPropSelection toPropSelection();
250 
251  /**
252  * Helper converts this instance to its native type.
253  *
254  * \return the property as color property
255  */
256  WPropColor toPropColor();
257 
258  /**
259  * Helper converts this instance to its native type.
260  *
261  * \return the property as position property
262  */
263  WPropPosition toPropPosition();
264 
265  /**
266  * Helper converts this instance to its native type.
267  *
268  * \return the property as trigger property
269  */
270  WPropTrigger toPropTrigger();
271 
272  /**
273  * Helper converts this instance to its native type.
274  *
275  * \return the property as matrix4x4 property
276  */
277  WPropMatrix4X4 toPropMatrix4X4();
278 
279  /**
280  * Helper converts this instance to its native type.
281  *
282  * \return the property as transfer function property
283  */
284  WPropTransferFunction toPropTransferFunction();
285 
286  /**
287  * Helper converts this instance to its native type.
288  *
289  * \return the property as group
290  */
291  WPropGroup toPropGroup();
292 
293  /**
294  * Helper converts this instance to its native type.
295  *
296  * \return the property as interval property
297  */
298  WPropInterval toPropInterval();
299 
300  /**
301  * Convert the property to a WPropertyGroupBase. This can be done with property structs and groups-
302  *
303  * \return the property as base group.
304  */
305  std::shared_ptr< WPropertyGroupBase > toPropGroupBase();
306 
307  /**
308  * Helper converts this instance to an arbitrary type.
309  *
310  * \return the property of given type of NULL if not valid type
311  */
312  template< typename T >
313  std::shared_ptr< WPropertyVariable< T > > toPropertyVariable();
314 
315  /**
316  * Signal signature emitted during set operations
317  */
318  typedef boost::function<void ( std::shared_ptr< WPropertyBase > )> PropertyChangeNotifierType;
319 
320 protected:
321  /**
322  * Name of the property.
323  */
324  std::string m_name;
325 
326  /**
327  * Description of the property.
328  */
329  std::string m_description;
330 
331  /**
332  * Flag denoting whether the property is hidden or not.
333  */
334  bool m_hidden;
335 
336  /**
337  * Type of the PropertyVariable instance
338  */
339  PROPERTY_TYPE m_type;
340 
341  /**
342  * The purpose of this property. PropertyBase always initializes it with PV_PURPOSE_PARAMETER.
343  */
344  PROPERTY_PURPOSE m_purpose;
345 
346  /**
347  * Calculates the type of the property. This has to be done by the implementing class.
348  */
349  virtual void updateType();
350 
351  /**
352  * Signal used for firing change signals
353  */
354  typedef boost::signals2::signal<void ( std::shared_ptr< WPropertyBase > )> PropertyChangeSignalType;
355 
356  /**
357  * Signal getting fired whenever the property changes.
358  */
360 
361  /**
362  * Condition notified whenever something changes. See getUpdateCondition for more details.
363  * \see getUpdateCondition
364  */
365  std::shared_ptr< WConditionSet > m_updateCondition;
366 
367 private:
368 };
369 
370 template< typename T >
371 std::shared_ptr< WPropertyVariable< T > > WPropertyBase::toPropertyVariable()
372 {
373  return std::dynamic_pointer_cast< WPropertyVariable< T > >( shared_from_this() );
374 }
375 
376 template< typename T >
377 bool WPropertyBase::set( const T& value )
378 {
379  return setAsString( string_utils::toString( value ) );
380 }
381 
382 #endif // WPROPERTYBASE_H
383 
Abstract base class for all properties.
Definition: WPropertyBase.h:48
bool isHidden() const
Determines whether the property is hidden or not.
PROPERTY_TYPE m_type
Type of the PropertyVariable instance.
std::shared_ptr< WPropertyGroupBase > toPropGroupBase()
Convert the property to a WPropertyGroupBase.
WPropInterval toPropInterval()
Helper converts this instance to its native type.
virtual bool set(std::shared_ptr< WPropertyBase > value, bool recommendedOnly=false)=0
Sets the value from the specified property to this one.
WPropString toPropString()
Helper converts this instance to its native type.
WPropDouble toPropDouble()
Helper converts this instance to its native type.
std::string m_description
Description of the property.
WPropTransferFunction toPropTransferFunction()
Helper converts this instance to its native type.
PROPERTY_PURPOSE m_purpose
The purpose of this property.
std::string getName() const
Gets the name of the class.
virtual std::shared_ptr< WPropertyBase > clone()=0
This method clones a property and returns the clone.
std::shared_ptr< const WPropertyBase > ConstSPtr
Convenience typedef for a std::shared_ptr< const WPropertyBase >
Definition: WPropertyBase.h:58
std::shared_ptr< WConditionSet > m_updateCondition
Condition notified whenever something changes.
std::shared_ptr< WPropertyVariable< T > > toPropertyVariable()
Helper converts this instance to an arbitrary type.
WPropBool toPropBool()
Helper converts this instance to its native type.
virtual PROPERTY_TYPE getType() const
Gets the real WPropertyVariable type of this instance.
bool m_hidden
Flag denoting whether the property is hidden or not.
WPropColor toPropColor()
Helper converts this instance to its native type.
void setHidden(bool hidden=true)
Sets the property hidden.
std::string getDescription() const
Gets the description of the property.
std::string m_name
Name of the property.
WPropFilename toPropFilename()
Helper converts this instance to its native type.
virtual std::shared_ptr< WCondition > getUpdateCondition() const
This method returns a condition which gets fired whenever the property changes somehow.
PropertyChangeSignalType signal_PropertyChange
Signal getting fired whenever the property changes.
WPropMatrix4X4 toPropMatrix4X4()
Helper converts this instance to its native type.
WPropInt toPropInt()
Helper converts this instance to its native type.
WPropPosition toPropPosition()
Helper converts this instance to its native type.
virtual void setPurpose(PROPERTY_PURPOSE purpose)
Sets the purpose of the property.
boost::signals2::signal< void(std::shared_ptr< WPropertyBase >)> PropertyChangeSignalType
Signal used for firing change signals.
virtual bool setAsString(std::string value)=0
This methods allows properties to be set by a string value.
WPropertyBase(std::string name, std::string description)
Create an empty named property.
virtual PROPERTY_PURPOSE getPurpose() const
Gets the purpose of a property.
virtual std::string getAsString()=0
Returns the current value as a string.
WPropTrigger toPropTrigger()
Helper converts this instance to its native type.
virtual void updateType()
Calculates the type of the property.
WPropSelection toPropSelection()
Helper converts this instance to its native type.
virtual ~WPropertyBase()
Destructor.
boost::function< void(std::shared_ptr< WPropertyBase >)> PropertyChangeNotifierType
Signal signature emitted during set operations.
WPropGroup toPropGroup()
Helper converts this instance to its native type.
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
bool set(const T &value)
Shortcut to set a property with a given value.
This is the base class and interface for property groups.
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120