OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WPropertyTypes.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPROPERTYTYPES_H
00026 #define WPROPERTYTYPES_H
00027 
00028 #include <stdint.h>
00029 
00030 #include <list>
00031 #include <string>
00032 #include <vector>
00033 #include <utility>
00034 
00035 // Use filesystem version 2 for compatibility with newer boost versions.
00036 #ifndef BOOST_FILESYSTEM_VERSION
00037     #define BOOST_FILESYSTEM_VERSION 2
00038 #endif
00039 #include <boost/filesystem.hpp>
00040 #include <boost/lexical_cast.hpp>
00041 
00042 #include "math/linearAlgebra/WLinearAlgebra.h"
00043 #include "math/linearAlgebra/WMatrixFixed.h"
00044 #include "math/linearAlgebra/WVectorFixed.h"
00045 #include "WAssert.h"
00046 #include "WColor.h"
00047 #include "WItemSelector.h"
00048 
00049 template < typename T >
00050 class WPropertyVariable;
00051 class WProperties;
00052 
00053 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00054 // NOTE: If you add new types here, please also add corresponding addProperty methods to WProperties
00055 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00056 
00057 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00058 // NOTE: Always use the WPVBaseTypes in all your declarations to allow easy type modifications later on
00059 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00060 
00061 /**
00062  * Enum of all possible types, that can be used with WProperty.
00063  */
00064 typedef enum
00065 {
00066     PV_UNKNOWN,        //!< type not known
00067     PV_GROUP,          //!< the group property
00068     PV_INT,            //!< integer value
00069     PV_DOUBLE,         //!< floating point value
00070     PV_BOOL,           //!< boolean
00071     PV_STRING,         //!< a string
00072     PV_PATH,           //!< a Boost Path object denoting a filename/path
00073     PV_SELECTION,      //!< a list of strings, selectable
00074     PV_POSITION,       //!< a position property
00075     PV_COLOR,          //!< a color property
00076     PV_TRIGGER,        //!< for triggering an event
00077     PV_MATRIX4X4       //!< for 4x4 matrices
00078 }
00079 PROPERTY_TYPE;
00080 
00081 /**
00082  * 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
00083  * 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
00084  * from the creator who wants to inform the outside world about values, states or whatever.
00085  */
00086 typedef enum
00087 {
00088     PV_PURPOSE_INFORMATION,     //!< information property not meant to be modified from someone (except the creating object)
00089     PV_PURPOSE_PARAMETER        //!< a parameter meant to be modified by others to manipulate the behaviour of the module (or whomever created
00090                                 //!< the property)
00091 }
00092 PROPERTY_PURPOSE;
00093 
00094 /**
00095  * Namespace containing all base types of the WPropertyVariables. Use these types instead of issuing int32_t, double, bool, ...
00096  * directly. It also contains some user defined types including the needed operators.
00097  *
00098  * \note You can use only types which overwrite the << and >> operators!
00099  */
00100 namespace WPVBaseTypes
00101 {
00102     typedef int32_t                                         PV_INT;         //!< base type used for every WPVInt
00103     typedef double                                          PV_DOUBLE;      //!< base type used for every WPVDouble
00104     typedef bool                                            PV_BOOL;        //!< base type used for every WPVBool
00105     typedef std::string                                     PV_STRING;      //!< base type used for every WPVString
00106     typedef boost::filesystem::path                         PV_PATH;        //!< base type used for every WPVFilename
00107     typedef WItemSelector                                   PV_SELECTION;   //!< base type used for every WPVSelection
00108     typedef WPosition                                     PV_POSITION;    //!< base type used for every WPVPosition
00109     typedef WColor                                          PV_COLOR;       //!< base type used for every WPVColor
00110     typedef WMatrix4d                                     PV_MATRIX4X4;   //!< base type used for every WPVMatrix4X4
00111 
00112     /**
00113      * Enum denoting the possible trigger states. It is used for trigger properties.
00114      */
00115     typedef enum
00116     {
00117         PV_TRIGGER_READY = 0,                                               //!< Trigger property: is ready to be triggered (again)
00118         PV_TRIGGER_TRIGGERED                                                //!< Trigger property: got triggered
00119     }
00120                                                             PV_TRIGGER;     //!< base type used for every WPVTrigger
00121 
00122     /**
00123      * Write a PV_TRIGGER in string representation to the given output stream.
00124      *
00125      * \param out the output stream to print the value to
00126      * \param c the trigger value to output
00127      *
00128      * \return the output stream extended by the trigger value.
00129      */
00130     std::ostream& operator<<( std::ostream& out, const PV_TRIGGER& c );
00131 
00132     /**
00133      * Write a PV_TRIGGER in string representation to the given input stream.
00134      *
00135      * \param in the input stream to read the value from
00136      * \param c  set the value red to this
00137      *
00138      * \return the input stream.
00139      */
00140     std::istream& operator>>( std::istream& in, PV_TRIGGER& c );
00141 }
00142 
00143 /**
00144  * Some convenience type alias for a even more easy usage of WPropertyVariable.
00145  * These typedefs are useful for casts, as they alias the PropertyVariable types. Please use these types instead of directly
00146  * int32_t, double, bool, ... so we are able to change the type later on without modifications of thousands of modules.
00147  */
00148 
00149 /**
00150  * Group properties.
00151  */
00152 typedef WProperties WPVGroup;
00153 
00154 /**
00155  * Int properties.
00156  */
00157 typedef WPropertyVariable< WPVBaseTypes::PV_INT > WPVInt;
00158 
00159 /**
00160  * Floating point properties.
00161  */
00162 typedef WPropertyVariable< WPVBaseTypes::PV_DOUBLE > WPVDouble;
00163 
00164 /**
00165  * Boolean properties.
00166  */
00167 typedef WPropertyVariable< WPVBaseTypes::PV_BOOL > WPVBool;
00168 
00169 /**
00170  * String properties.
00171  */
00172 typedef WPropertyVariable< WPVBaseTypes::PV_STRING > WPVString;
00173 
00174 /**
00175  * Filename properties.
00176  */
00177 typedef WPropertyVariable< WPVBaseTypes::PV_PATH > WPVFilename;
00178 
00179 /**
00180  * Selection properties
00181  */
00182 typedef WPropertyVariable< WPVBaseTypes::PV_SELECTION > WPVSelection;
00183 
00184 /**
00185  * position (vec3d) properties
00186  */
00187 typedef WPropertyVariable< WPVBaseTypes::PV_POSITION > WPVPosition;
00188 
00189 /**
00190  * Color properties
00191  */
00192 typedef WPropertyVariable< WPVBaseTypes::PV_COLOR > WPVColor;
00193 
00194 /**
00195  * Trigger properties
00196  */
00197 typedef WPropertyVariable< WPVBaseTypes::PV_TRIGGER > WPVTrigger;
00198 
00199 /**
00200  * Trigger properties
00201  */
00202 typedef WPropertyVariable< WPVBaseTypes::PV_MATRIX4X4 > WPVMatrix4X4;
00203 
00204 /**
00205  * Some convenience type alias for a even more easy usage of WPropertyVariable.
00206  * These typdefs define some pointer alias.
00207  */
00208 
00209 /**
00210  * Alias for int32_t property variables.
00211  */
00212 typedef boost::shared_ptr< WPVInt > WPropInt;
00213 
00214 /**
00215  * Alias for int32_t property variables.
00216  */
00217 typedef boost::shared_ptr< WPVDouble > WPropDouble;
00218 
00219 /**
00220  * Alias for bool property variables.
00221  */
00222 typedef boost::shared_ptr< WPVBool > WPropBool;
00223 
00224 /**
00225  * Alias for string property variables.
00226  */
00227 typedef boost::shared_ptr< WPVString > WPropString;
00228 
00229 /**
00230  * Alias for filename property variables.
00231  */
00232 typedef boost::shared_ptr< WPVFilename > WPropFilename;
00233 
00234 /**
00235  * Alias for string list property variables.
00236  */
00237 typedef boost::shared_ptr< WPVSelection > WPropSelection;
00238 
00239 /**
00240  * Alias for position property variables.
00241  */
00242 typedef boost::shared_ptr< WPVPosition > WPropPosition;
00243 
00244 /**
00245  * Alias for color property variables.
00246  */
00247 typedef boost::shared_ptr< WPVColor > WPropColor;
00248 
00249 /**
00250  * Alias for the group properties.
00251  */
00252 typedef boost::shared_ptr< WPVGroup > WPropGroup;
00253 
00254 /**
00255  * Alias for the trigger properties.
00256  */
00257 typedef boost::shared_ptr< WPVTrigger > WPropTrigger;
00258 
00259 /**
00260  * Alias for the 4x4 matrix properties.
00261  */
00262 typedef boost::shared_ptr< WPVMatrix4X4 > WPropMatrix4X4;
00263 
00264 
00265 /**
00266  * This namespace contains several helper classes which translate their template type to an enum.
00267  */
00268 namespace PROPERTY_TYPE_HELPER
00269 {
00270     /**
00271      * Class helping to adapt types specified as template parameter into an enum.
00272      */
00273     template< typename T >
00274     class WTypeIdentifier
00275     {
00276     public:
00277         /**
00278          * Get type identifier of the template type T.
00279          *
00280          * \return type identifier-
00281          */
00282         PROPERTY_TYPE getType()
00283         {
00284             return PV_UNKNOWN;
00285         }
00286     };
00287 
00288     /**
00289      * 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
00290      * predecessor for creation).
00291      * You only need to specialize this class for types not allowing the direct use of boost::lexical_cast.
00292      */
00293     template< typename T >
00294     class WStringConversion
00295     {
00296     public:
00297         /**
00298          * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
00299          *
00300          * \param str the new value as string
00301          *
00302          * \return the new instance
00303          */
00304         T create( const T& /*old*/, const std::string str )
00305         {
00306             return boost::lexical_cast< T >( str );
00307         }
00308 
00309         /**
00310          * Creates a string from the specified value.
00311          *
00312          * \param v the value to convert
00313          *
00314          * \return the string representation
00315          */
00316         std::string asString( const T& v )
00317         {
00318             return boost::lexical_cast< std::string >( v );
00319         }
00320     };
00321 
00322     /**
00323      * Class helping to adapt types specified as template parameter into an enum.
00324      */
00325     template<>
00326     class WTypeIdentifier< WPVBaseTypes::PV_BOOL >
00327     {
00328     public:
00329         /**
00330          * Get type identifier of the template type T.
00331          *
00332          * \return type identifier-
00333          */
00334         PROPERTY_TYPE getType()
00335         {
00336             return PV_BOOL;
00337         }
00338     };
00339 
00340     /**
00341      * Class helping to adapt types specified as template parameter into an enum.
00342      */
00343     template<>
00344     class WTypeIdentifier< WPVBaseTypes::PV_INT >
00345     {
00346     public:
00347         /**
00348          * Get type identifier of the template type T.
00349          *
00350          * \return type identifier-
00351          */
00352         PROPERTY_TYPE getType()
00353         {
00354             return PV_INT;
00355         }
00356     };
00357 
00358     /**
00359      * Class helping to adapt types specified as template parameter into an enum.
00360      */
00361     template<>
00362     class WTypeIdentifier< WPVBaseTypes::PV_DOUBLE >
00363     {
00364     public:
00365         /**
00366          * Get type identifier of the template type T.
00367          *
00368          * \return type identifier-
00369          */
00370         PROPERTY_TYPE getType()
00371         {
00372             return PV_DOUBLE;
00373         }
00374     };
00375 
00376     /**
00377      * Class helping to adapt types specified as template parameter into an enum.
00378      */
00379     template<>
00380     class WTypeIdentifier< WPVBaseTypes::PV_STRING >
00381     {
00382     public:
00383         /**
00384          * Get type identifier of the template type T.
00385          *
00386          * \return type identifier-
00387          */
00388         PROPERTY_TYPE getType()
00389         {
00390             return PV_STRING;
00391         }
00392     };
00393 
00394     /**
00395      * Class helping to adapt types specified as template parameter into an enum.
00396      */
00397     template<>
00398     class WTypeIdentifier< WPVBaseTypes::PV_PATH >
00399     {
00400     public:
00401         /**
00402          * Get type identifier of the template type T.
00403          *
00404          * \return type identifier-
00405          */
00406         PROPERTY_TYPE getType()
00407         {
00408             return PV_PATH;
00409         }
00410     };
00411 
00412     /**
00413      * Class helping to adapt types specified as template parameter into an enum.
00414      */
00415     template<>
00416     class WTypeIdentifier< WPVBaseTypes::PV_SELECTION >
00417     {
00418     public:
00419         /**
00420          * Get type identifier of the template type T.
00421          *
00422          * \return type identifier-
00423          */
00424         PROPERTY_TYPE getType()
00425         {
00426             return PV_SELECTION;
00427         }
00428     };
00429 
00430     /**
00431      * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
00432      * serializable content which needs to be acquired from its predecessor instance.
00433      */
00434     template<>
00435     class WStringConversion< WPVBaseTypes::PV_SELECTION >
00436     {
00437     public:
00438         /**
00439          * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
00440          *
00441          * \param old the old value
00442          * \param str the new value as string
00443          *
00444          * \return the new instance
00445          */
00446         WPVBaseTypes::PV_SELECTION  create( const WPVBaseTypes::PV_SELECTION& old, const std::string str )
00447         {
00448             return old.newSelector( str );
00449         }
00450 
00451         /**
00452          * Creates a string from the specified value.
00453          *
00454          * \param v the value to convert
00455          *
00456          * \return the string representation
00457          */
00458         std::string asString( const WPVBaseTypes::PV_SELECTION& v )
00459         {
00460             return boost::lexical_cast< std::string >( v );
00461         }
00462     };
00463 
00464     /**
00465      * Class helping to adapt types specified as template parameter into an enum.
00466      */
00467     template<>
00468     class WTypeIdentifier< WPVBaseTypes::PV_POSITION >
00469     {
00470     public:
00471         /**
00472          * Get type identifier of the template type T.
00473          *
00474          * \return type identifier-
00475          */
00476         PROPERTY_TYPE getType()
00477         {
00478             return PV_POSITION;
00479         }
00480     };
00481 
00482     /**
00483      * Class helping to adapt types specified as template parameter into an enum.
00484      */
00485     template<>
00486     class WTypeIdentifier< WPVBaseTypes::PV_COLOR >
00487     {
00488     public:
00489         /**
00490          * Get type identifier of the template type T.
00491          *
00492          * \return type identifier-
00493          */
00494         PROPERTY_TYPE getType()
00495         {
00496             return PV_COLOR;
00497         }
00498     };
00499 
00500     /**
00501      * Class helping to adapt types specified as template parameter into an enum.
00502      */
00503     template<>
00504     class WTypeIdentifier< WPVBaseTypes::PV_TRIGGER >
00505     {
00506     public:
00507         /**
00508          * Get type identifier of the template type T.
00509          *
00510          * \return type identifier-
00511          */
00512         PROPERTY_TYPE getType()
00513         {
00514             return PV_TRIGGER;
00515         }
00516     };
00517 
00518     /**
00519      * Class helping to adapt types specified as template parameter into an enum.
00520      */
00521     template<>
00522     class WTypeIdentifier< WPVBaseTypes::PV_MATRIX4X4 >
00523     {
00524     public:
00525         /**
00526          * Get type identifier of the template type T.
00527          *
00528          * \return type identifier-
00529          */
00530         PROPERTY_TYPE getType()
00531         {
00532             return PV_MATRIX4X4;
00533         }
00534     };
00535 
00536     /**
00537      * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
00538      * serializable content which needs to be acquired from its predecessor instance.
00539      */
00540     template<>
00541     class WStringConversion< WPVBaseTypes::PV_MATRIX4X4 >
00542     {
00543     public:
00544         /**
00545          * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
00546          *
00547          * \param str the new value as string
00548          *
00549          * \return the new instance
00550          */
00551         WPVBaseTypes::PV_MATRIX4X4 create( const WPVBaseTypes::PV_MATRIX4X4& /*old*/, const std::string str )
00552         {
00553             WMatrix4d c;
00554             std::vector< std::string > tokens;
00555             tokens = string_utils::tokenize( str, ";" );
00556             WAssert( tokens.size() >= 16, "There weren't 16 values for a 4x4 Matrix" );
00557 
00558             size_t idx = 0;
00559             for( size_t row = 0; row < 4; ++row )
00560             {
00561                 for( size_t col = 0; col < 4; ++col )
00562                 {
00563                     c( row, col ) = boost::lexical_cast< double >( tokens[ idx ] );
00564                     idx++;
00565                 }
00566             }
00567 
00568             return c;
00569         }
00570 
00571         /**
00572          * Creates a string from the specified value.
00573          *
00574          * \param v the value to convert
00575          *
00576          * \return the string representation
00577          */
00578         std::string asString( const WPVBaseTypes::PV_MATRIX4X4& v )
00579         {
00580             std::ostringstream out;
00581             for( size_t row = 0; row < 4; ++row )
00582             {
00583                 for( size_t col = 0; col < 4; ++col )
00584                 {
00585                     out << v( row, col ) << ";";
00586                 }
00587             }
00588             return out.str();
00589         }
00590     };
00591 
00592     /**
00593      * Class helping to create a new instance of the property content from an old one. Selections need this special care since they contain not
00594      * serializable content which needs to be acquired from its predecessor instance.
00595      */
00596     template<>
00597     class WStringConversion< WPVBaseTypes::PV_POSITION >
00598     {
00599     public:
00600         /**
00601          * Creates a new instance of the type from a given string. Some classes need a predecessor which is also specified here.
00602          *
00603          * \param str the new value as string
00604          *
00605          * \return the new instance
00606          */
00607         WPVBaseTypes::PV_POSITION create( const WPVBaseTypes::PV_POSITION& /*old*/, const std::string str )
00608         {
00609             WPVBaseTypes::PV_POSITION c;
00610             std::vector< std::string > tokens;
00611             tokens = string_utils::tokenize( str, ";" );
00612             WAssert( tokens.size() >= 3, "There weren't 3 values for a 3D vector" );
00613 
00614             size_t idx = 0;
00615             for( size_t col = 0; col < 3; ++col )
00616             {
00617                 c[ col ] = boost::lexical_cast< double >( tokens[ idx ] );
00618                 idx++;
00619             }
00620             return c;
00621         }
00622 
00623         /**
00624          * Creates a string from the specified value.
00625          *
00626          * \param v the value to convert
00627          *
00628          * \return the string representation
00629          */
00630         std::string asString( const WPVBaseTypes::PV_POSITION& v )
00631         {
00632             std::ostringstream out;
00633             for( size_t col = 0; col < 3; ++col )
00634             {
00635                 out << v[ col ] << ";";
00636             }
00637             return out.str();
00638         }
00639     };
00640 }
00641 
00642 #endif  // WPROPERTYTYPES_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends