OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WPropertyTypes.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 WPROPERTYTYPES_H
26 #define WPROPERTYTYPES_H
27 
28 #include <stdint.h>
29 
30 #include <list>
31 #include <string>
32 #include <vector>
33 #include <utility>
34 
35 // Use filesystem version 2 for compatibility with newer boost versions.
36 #ifndef BOOST_FILESYSTEM_VERSION
37  #define BOOST_FILESYSTEM_VERSION 2
38 #endif
39 #include <boost/filesystem.hpp>
40 #include <boost/lexical_cast.hpp>
41 
42 #include "math/linearAlgebra/WLinearAlgebra.h"
43 #include "math/linearAlgebra/WMatrixFixed.h"
44 #include "math/linearAlgebra/WVectorFixed.h"
45 #include "WAssert.h"
46 #include "WColor.h"
47 #include "WItemSelector.h"
48 
49 template < typename T >
50 class WPropertyVariable;
51 class WProperties;
52 
53 ////////////////////////////////////////////////////////////////////////////////////////////////////////
54 // NOTE: If you add new types here, please also add corresponding addProperty methods to WProperties
55 ////////////////////////////////////////////////////////////////////////////////////////////////////////
56 
57 ////////////////////////////////////////////////////////////////////////////////////////////////////////
58 // NOTE: Always use the WPVBaseTypes in all your declarations to allow easy type modifications later on
59 ////////////////////////////////////////////////////////////////////////////////////////////////////////
60 
61 /**
62  * Enum of all possible types, that can be used with WProperty.
63  */
64 typedef enum
65 {
66  PV_UNKNOWN, //!< type not known
67  PV_GROUP, //!< the group property
68  PV_INT, //!< integer value
69  PV_DOUBLE, //!< floating point value
70  PV_BOOL, //!< boolean
71  PV_STRING, //!< a string
72  PV_PATH, //!< a Boost Path object denoting a filename/path
73  PV_SELECTION, //!< a list of strings, selectable
74  PV_POSITION, //!< a position property
75  PV_COLOR, //!< a color property
76  PV_TRIGGER, //!< for triggering an event
77  PV_MATRIX4X4 //!< for 4x4 matrices
78 }
79 PROPERTY_TYPE;
80 
81 /**
82  * Enum of all possible purpose of a property. The purpose describes which meaning a property has for the creator of it. A PP_PARAMETER is a
83  * property which is meant to be modified to adopt the behaviour of the module (or whomever has created it). A PP_INFORMATION is only an output
84  * from the creator who wants to inform the outside world about values, states or whatever.
85  */
86 typedef enum
87 {
88  PV_PURPOSE_INFORMATION, //!< information property not meant to be modified from someone (except the creating object)
89  PV_PURPOSE_PARAMETER //!< a parameter meant to be modified by others to manipulate the behaviour of the module (or whomever created
90  //!< the property)
91 }
92 PROPERTY_PURPOSE;
93 
94 /**
95  * Namespace containing all base types of the WPropertyVariables. Use these types instead of issuing int32_t, double, bool, ...
96  * directly. It also contains some user defined types including the needed operators.
97  *
98  * \note You can use only types which overwrite the << and >> operators!
99  */
100 namespace WPVBaseTypes
101 {
102  typedef int32_t PV_INT; //!< base type used for every WPVInt
103  typedef double PV_DOUBLE; //!< base type used for every WPVDouble
104  typedef bool PV_BOOL; //!< base type used for every WPVBool
105  typedef std::string PV_STRING; //!< base type used for every WPVString
106  typedef boost::filesystem::path PV_PATH; //!< base type used for every WPVFilename
107  typedef WItemSelector PV_SELECTION; //!< base type used for every WPVSelection
108  typedef WPosition PV_POSITION; //!< base type used for every WPVPosition
109  typedef WColor PV_COLOR; //!< base type used for every WPVColor
110  typedef WMatrix4d PV_MATRIX4X4; //!< base type used for every WPVMatrix4X4
111 
112  /**
113  * Enum denoting the possible trigger states. It is used for trigger properties.
114  */
115  typedef enum
116  {
117  PV_TRIGGER_READY = 0, //!< Trigger property: is ready to be triggered (again)
118  PV_TRIGGER_TRIGGERED //!< Trigger property: got triggered
119  }
120  PV_TRIGGER; //!< base type used for every WPVTrigger
121 
122  /**
123  * Write a PV_TRIGGER in string representation to the given output stream.
124  *
125  * \param out the output stream to print the value to
126  * \param c the trigger value to output
127  *
128  * \return the output stream extended by the trigger value.
129  */
130  std::ostream& operator<<( std::ostream& out, const PV_TRIGGER& c );
131 
132  /**
133  * Write a PV_TRIGGER in string representation to the given input stream.
134  *
135  * \param in the input stream to read the value from
136  * \param c set the value red to this
137  *
138  * \return the input stream.
139  */
140  std::istream& operator>>( std::istream& in, PV_TRIGGER& c );
141 }
142 
143 /**
144  * Some convenience type alias for a even more easy usage of WPropertyVariable.
145  * These typedefs are useful for casts, as they alias the PropertyVariable types. Please use these types instead of directly
146  * int32_t, double, bool, ... so we are able to change the type later on without modifications of thousands of modules.
147  */
148 
149 /**
150  * Group properties.
151  */
152 typedef WProperties WPVGroup;
153 
154 /**
155  * Int properties.
156  */
158 
159 /**
160  * Floating point properties.
161  */
163 
164 /**
165  * Boolean properties.
166  */
168 
169 /**
170  * String properties.
171  */
173 
174 /**
175  * Filename properties.
176  */
178 
179 /**
180  * Selection properties
181  */
183 
184 /**
185  * position (vec3d) properties
186  */
188 
189 /**
190  * Color properties
191  */
193 
194 /**
195  * Trigger properties
196  */
198 
199 /**
200  * Trigger properties
201  */
203 
204 /**
205  * Some convenience type alias for a even more easy usage of WPropertyVariable.
206  * These typdefs define some pointer alias.
207  */
208 
209 /**
210  * Alias for int32_t property variables.
211  */
212 typedef boost::shared_ptr< WPVInt > WPropInt;
213 
214 /**
215  * Alias for int32_t property variables.
216  */
217 typedef boost::shared_ptr< WPVDouble > WPropDouble;
218 
219 /**
220  * Alias for bool property variables.
221  */
222 typedef boost::shared_ptr< WPVBool > WPropBool;
223 
224 /**
225  * Alias for string property variables.
226  */
227 typedef boost::shared_ptr< WPVString > WPropString;
228 
229 /**
230  * Alias for filename property variables.
231  */
232 typedef boost::shared_ptr< WPVFilename > WPropFilename;
233 
234 /**
235  * Alias for string list property variables.
236  */
237 typedef boost::shared_ptr< WPVSelection > WPropSelection;
238 
239 /**
240  * Alias for position property variables.
241  */
242 typedef boost::shared_ptr< WPVPosition > WPropPosition;
243 
244 /**
245  * Alias for color property variables.
246  */
247 typedef boost::shared_ptr< WPVColor > WPropColor;
248 
249 /**
250  * Alias for the group properties.
251  */
252 typedef boost::shared_ptr< WPVGroup > WPropGroup;
253 
254 /**
255  * Alias for the trigger properties.
256  */
257 typedef boost::shared_ptr< WPVTrigger > WPropTrigger;
258 
259 /**
260  * Alias for the 4x4 matrix properties.
261  */
262 typedef boost::shared_ptr< WPVMatrix4X4 > WPropMatrix4X4;
263 
264 
265 /**
266  * This namespace contains several helper classes which translate their template type to an enum.
267  */
268 namespace PROPERTY_TYPE_HELPER
269 {
270  /**
271  * Class helping to adapt types specified as template parameter into an enum.
272  */
273  template< typename T >
275  {
276  public:
277  /**
278  * Get type identifier of the template type T.
279  *
280  * \return type identifier-
281  */
282  PROPERTY_TYPE getType()
283  {
284  return PV_UNKNOWN;
285  }
286  };
287 
288  /**
289  * Class helping to create a new instance of the property content from an old one. This might be needed by some types (some need to have a
290  * predecessor for creation).
291  * You only need to specialize this class for types not allowing the direct use of boost::lexical_cast.
292  */
293  template< typename T >
295  {
296  public:
297  /**
298  * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
299  *
300  * \param str the new value as string
301  *
302  * \return the new instance
303  */
304  T create( const T& /*old*/, const std::string str )
305  {
306  return boost::lexical_cast< T >( str );
307  }
308 
309  /**
310  * Creates a string from the specified value.
311  *
312  * \param v the value to convert
313  *
314  * \return the string representation
315  */
316  std::string asString( const T& v )
317  {
318  return boost::lexical_cast< std::string >( v );
319  }
320  };
321 
322  /**
323  * Class helping to adapt types specified as template parameter into an enum.
324  */
325  template<>
326  class WTypeIdentifier< WPVBaseTypes::PV_BOOL >
327  {
328  public:
329  /**
330  * Get type identifier of the template type T.
331  *
332  * \return type identifier-
333  */
334  PROPERTY_TYPE getType()
335  {
336  return PV_BOOL;
337  }
338  };
339 
340  /**
341  * Class helping to adapt types specified as template parameter into an enum.
342  */
343  template<>
344  class WTypeIdentifier< WPVBaseTypes::PV_INT >
345  {
346  public:
347  /**
348  * Get type identifier of the template type T.
349  *
350  * \return type identifier-
351  */
352  PROPERTY_TYPE getType()
353  {
354  return PV_INT;
355  }
356  };
357 
358  /**
359  * Class helping to adapt types specified as template parameter into an enum.
360  */
361  template<>
362  class WTypeIdentifier< WPVBaseTypes::PV_DOUBLE >
363  {
364  public:
365  /**
366  * Get type identifier of the template type T.
367  *
368  * \return type identifier-
369  */
370  PROPERTY_TYPE getType()
371  {
372  return PV_DOUBLE;
373  }
374  };
375 
376  /**
377  * Class helping to adapt types specified as template parameter into an enum.
378  */
379  template<>
380  class WTypeIdentifier< WPVBaseTypes::PV_STRING >
381  {
382  public:
383  /**
384  * Get type identifier of the template type T.
385  *
386  * \return type identifier-
387  */
388  PROPERTY_TYPE getType()
389  {
390  return PV_STRING;
391  }
392  };
393 
394  /**
395  * Class helping to adapt types specified as template parameter into an enum.
396  */
397  template<>
398  class WTypeIdentifier< WPVBaseTypes::PV_PATH >
399  {
400  public:
401  /**
402  * Get type identifier of the template type T.
403  *
404  * \return type identifier-
405  */
406  PROPERTY_TYPE getType()
407  {
408  return PV_PATH;
409  }
410  };
411 
412  /**
413  * Class helping to adapt types specified as template parameter into an enum.
414  */
415  template<>
416  class WTypeIdentifier< WPVBaseTypes::PV_SELECTION >
417  {
418  public:
419  /**
420  * Get type identifier of the template type T.
421  *
422  * \return type identifier-
423  */
424  PROPERTY_TYPE getType()
425  {
426  return PV_SELECTION;
427  }
428  };
429 
430  /**
431  * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
432  * serializable content which needs to be acquired from its predecessor instance.
433  */
434  template<>
435  class WStringConversion< WPVBaseTypes::PV_SELECTION >
436  {
437  public:
438  /**
439  * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
440  *
441  * \param old the old value
442  * \param str the new value as string
443  *
444  * \return the new instance
445  */
446  WPVBaseTypes::PV_SELECTION create( const WPVBaseTypes::PV_SELECTION& old, const std::string str )
447  {
448  return old.newSelector( str );
449  }
450 
451  /**
452  * Creates a string from the specified value.
453  *
454  * \param v the value to convert
455  *
456  * \return the string representation
457  */
458  std::string asString( const WPVBaseTypes::PV_SELECTION& v )
459  {
460  return boost::lexical_cast< std::string >( v );
461  }
462  };
463 
464  /**
465  * Class helping to adapt types specified as template parameter into an enum.
466  */
467  template<>
468  class WTypeIdentifier< WPVBaseTypes::PV_POSITION >
469  {
470  public:
471  /**
472  * Get type identifier of the template type T.
473  *
474  * \return type identifier-
475  */
476  PROPERTY_TYPE getType()
477  {
478  return PV_POSITION;
479  }
480  };
481 
482  /**
483  * Class helping to adapt types specified as template parameter into an enum.
484  */
485  template<>
486  class WTypeIdentifier< WPVBaseTypes::PV_COLOR >
487  {
488  public:
489  /**
490  * Get type identifier of the template type T.
491  *
492  * \return type identifier-
493  */
494  PROPERTY_TYPE getType()
495  {
496  return PV_COLOR;
497  }
498  };
499 
500  /**
501  * Class helping to adapt types specified as template parameter into an enum.
502  */
503  template<>
504  class WTypeIdentifier< WPVBaseTypes::PV_TRIGGER >
505  {
506  public:
507  /**
508  * Get type identifier of the template type T.
509  *
510  * \return type identifier-
511  */
512  PROPERTY_TYPE getType()
513  {
514  return PV_TRIGGER;
515  }
516  };
517 
518  /**
519  * Class helping to adapt types specified as template parameter into an enum.
520  */
521  template<>
522  class WTypeIdentifier< WPVBaseTypes::PV_MATRIX4X4 >
523  {
524  public:
525  /**
526  * Get type identifier of the template type T.
527  *
528  * \return type identifier-
529  */
530  PROPERTY_TYPE getType()
531  {
532  return PV_MATRIX4X4;
533  }
534  };
535 
536  /**
537  * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
538  * serializable content which needs to be acquired from its predecessor instance.
539  */
540  template<>
541  class WStringConversion< WPVBaseTypes::PV_MATRIX4X4 >
542  {
543  public:
544  /**
545  * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
546  *
547  * \param str the new value as string
548  *
549  * \return the new instance
550  */
551  WPVBaseTypes::PV_MATRIX4X4 create( const WPVBaseTypes::PV_MATRIX4X4& /*old*/, const std::string str )
552  {
553  WMatrix4d c;
554  std::vector< std::string > tokens;
555  tokens = string_utils::tokenize( str, ";" );
556  WAssert( tokens.size() >= 16, "There weren't 16 values for a 4x4 Matrix" );
557 
558  size_t idx = 0;
559  for( size_t row = 0; row < 4; ++row )
560  {
561  for( size_t col = 0; col < 4; ++col )
562  {
563  c( row, col ) = boost::lexical_cast< double >( tokens[ idx ] );
564  idx++;
565  }
566  }
567 
568  return c;
569  }
570 
571  /**
572  * Creates a string from the specified value.
573  *
574  * \param v the value to convert
575  *
576  * \return the string representation
577  */
578  std::string asString( const WPVBaseTypes::PV_MATRIX4X4& v )
579  {
580  std::ostringstream out;
581  for( size_t row = 0; row < 4; ++row )
582  {
583  for( size_t col = 0; col < 4; ++col )
584  {
585  out << v( row, col ) << ";";
586  }
587  }
588  return out.str();
589  }
590  };
591 
592  /**
593  * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
594  * serializable content which needs to be acquired from its predecessor instance.
595  */
596  template<>
597  class WStringConversion< WPVBaseTypes::PV_POSITION >
598  {
599  public:
600  /**
601  * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
602  *
603  * \param str the new value as string
604  *
605  * \return the new instance
606  */
607  WPVBaseTypes::PV_POSITION create( const WPVBaseTypes::PV_POSITION& /*old*/, const std::string str )
608  {
610  std::vector< std::string > tokens;
611  tokens = string_utils::tokenize( str, ";" );
612  WAssert( tokens.size() >= 3, "There weren't 3 values for a 3D vector" );
613 
614  size_t idx = 0;
615  for( size_t col = 0; col < 3; ++col )
616  {
617  c[ col ] = boost::lexical_cast< double >( tokens[ idx ] );
618  idx++;
619  }
620  return c;
621  }
622 
623  /**
624  * Creates a string from the specified value.
625  *
626  * \param v the value to convert
627  *
628  * \return the string representation
629  */
630  std::string asString( const WPVBaseTypes::PV_POSITION& v )
631  {
632  std::ostringstream out;
633  for( size_t col = 0; col < 3; ++col )
634  {
635  out << v[ col ] << ";";
636  }
637  return out.str();
638  }
639  };
640 }
641 
642 #endif // WPROPERTYTYPES_H