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