OpenWalnut  1.4.0
WGEShaderPropertyDefineOptions.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 WGESHADERPROPERTYDEFINEOPTIONS_H
00026 #define WGESHADERPROPERTYDEFINEOPTIONS_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 #include "boost/tuple/tuple.hpp"
00033 #include <boost/signals2.hpp>
00034 
00035 #include "../../common/WProperties.h"
00036 #include "../../common/WPropertyTypes.h"
00037 #include "../../common/exceptions/WPreconditionNotMet.h"
00038 
00039 #include "WGEShaderDefineOptions.h"
00040 
00041 template< typename PropType >
00042 class WGEShaderPropertyDefineOptionsIndexAdapter;
00043 
00044 
00045 /**
00046  * This is a WGEShaderDefineOptions class which additionally uses a property to automatically control the active options. This is very useful if
00047  * you have some WPropInt or WPropSelection which controls some features in your shader. Especially with WPropSelection Instances, you can even
00048  * activate multiple options if your selection allows this ( see WPropertyVariable<>::PropertyConstraint for details ). If used with a WPropBool,
00049  * it is useful to switch on/off an option for example.
00050  *
00051  * \note You can use inherited WGEShaderDefineOptions methods too. This might create some kind of inconsistency since they of course do not
00052  * update the property.
00053  */
00054 template< typename PropType = WPropSelection, typename PropIndexAdapter = WGEShaderPropertyDefineOptionsIndexAdapter< PropType > >
00055 class WGEShaderPropertyDefineOptions: public WGEShaderDefineOptions
00056 {
00057 public:
00058     /**
00059      * Convenience typedef for a boost_shared_ptr< WGEShaderPropertyDefineOptions >.
00060      */
00061     typedef boost::shared_ptr< WGEShaderPropertyDefineOptions > SPtr;
00062 
00063     /**
00064      * Convenience typedef for a boost_shared_ptr< const WGEShaderPropertyDefineOptions >.
00065      */
00066     typedef boost::shared_ptr< const WGEShaderPropertyDefineOptions > ConstSPtr;
00067 
00068     /**
00069      * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
00070      *
00071      * \param prop the property controlling this thing.
00072      *
00073      * \param first fist option. Is default.
00074      * \param option2 another option
00075      * \param option3 another option
00076      * \param option4 another option
00077      * \param option5 another option
00078      * \param option6 another option
00079      * \param option7 another option
00080      * \param option8 another option
00081      * \param option9 another option
00082      * \param option10 another option
00083      */
00084     WGEShaderPropertyDefineOptions( PropType prop, std::string first,
00085                                     std::string option2 = "", std::string option3 = "", std::string option4 = "", std::string option5 = "",
00086                                     std::string option6 = "", std::string option7 = "", std::string option8 = "", std::string option9 = "",
00087                                     std::string option10 = "" );
00088 
00089     /**
00090      * Create a new instance of this class. The first option is mandatory and is set as default. The specified property controls the activations.
00091      *
00092      * \param prop the property controlling this thing.
00093      *
00094      * \param options the list of options. Must have a size greater 0.
00095      */
00096     WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options );
00097 
00098     /**
00099      * Destructor.
00100      */
00101     virtual ~WGEShaderPropertyDefineOptions();
00102 
00103     /**
00104      * Returns the property associated with this instance.
00105      *
00106      * \return
00107      */
00108     PropType getProperty() const;
00109 
00110 protected:
00111 private:
00112     /**
00113      * The property controlling this instance and the active options list.
00114      */
00115     PropType m_property;
00116 
00117     /**
00118      * The connection associated with the properties update condition.
00119      */
00120     boost::signals2::connection m_connection;
00121 
00122     /**
00123      * Called by the property update mechanism. This handles the new value in the property.
00124      */
00125     void propUpdated();
00126 };
00127 
00128 /**
00129  * Contains some utility functions related to the WGEShaderPropertyDefineOptions class.
00130  */
00131 namespace WGEShaderPropertyDefineOptionsTools
00132 {
00133     /**
00134      * This tuple contains name, description and define-name of an option.
00135      */
00136     typedef boost::tuple< std::string, std::string, std::string > NameDescriptionDefineTuple;
00137 
00138     /**
00139      * A little bit more comfortable way to create a list of shader-defines and the corresponding property.
00140      *
00141      * \param propName the name of the property to create
00142      * \param propDescription the description of the property to create
00143      * \param propGroup the owning group of the property
00144      * \param defines the list of names, descriptions and defines
00145      *
00146      * \return a WGEShaderPropertyDefineOptions instance associated with a new property. This can be acquired using getProperty().
00147      */
00148     WGEShaderPropertyDefineOptions< WPropSelection >::SPtr createSelection( std::string propName, std::string propDescription,
00149                                                                                                WProperties::SPtr propGroup,
00150                                                                                                std::vector< NameDescriptionDefineTuple > defines );
00151 }
00152 
00153 /**
00154  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. WPropSelection
00155  * is a specialization of this class.
00156  *
00157  * \tparam PropType The property. WPropInt for example.
00158  */
00159 template< typename PropType >
00160 class WGEShaderPropertyDefineOptionsIndexAdapter
00161 {
00162 public:
00163     /**
00164      * The type of the index-list to create.
00165      */
00166     typedef typename WGEShaderPropertyDefineOptions< PropType >::IdxList IdxList;
00167 
00168     /**
00169      *  Converts the specified property value to an index list.
00170      *
00171      * \param in the value to convert to an index list
00172      *
00173      * \return the new index list
00174      */
00175     IdxList operator()( const typename PropType::element_type::ValueType& in ) const
00176     {
00177         return IdxList( 1, typename IdxList::value_type( in ) );
00178     }
00179 };
00180 
00181 /**
00182  * Class converts the specified property value to an index list. The generic case for all int-castable property types is trivial. This is the
00183  * specialization for WPropSelection which allows multiple options to be active if the selection has multiple selected items.
00184  *
00185  * \tparam PropType The property. WPropInt for example.
00186  */
00187 template<>
00188 class WGEShaderPropertyDefineOptionsIndexAdapter< WPropSelection >
00189 {
00190 public:
00191     /**
00192      * The type of the index-list to create.
00193      */
00194     typedef WGEShaderPropertyDefineOptions< WPropSelection >::IdxList IdxList;
00195 
00196     /**
00197      *  Converts the specified property value to an index list.
00198      *
00199      * \param in the value to convert to an index list
00200      *
00201      * \return the new index list
00202      */
00203     IdxList operator()( const WPVBaseTypes::PV_SELECTION& in ) const
00204     {
00205         return in.getIndexList();
00206     }
00207 };
00208 
00209 template< typename PropType, typename PropIndexAdapter >
00210 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::WGEShaderPropertyDefineOptions( PropType prop, std::string first,
00211                             std::string option2, std::string option3, std::string option4, std::string option5,
00212                             std::string option6, std::string option7, std::string option8, std::string option9,
00213                             std::string option10 ):
00214     WGEShaderDefineOptions( first, option2, option3, option4, option5, option6, option7, option8, option9, option10 ),
00215     m_property( prop )
00216 {
00217     // if the prop changes -> update options
00218     m_connection = m_property->getValueChangeCondition()->subscribeSignal(
00219         boost::bind( &WGEShaderPropertyDefineOptions< PropType >::propUpdated, this )
00220     );
00221     propUpdated();
00222 }
00223 
00224 template< typename PropType, typename PropIndexAdapter >
00225 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::WGEShaderPropertyDefineOptions( PropType prop, std::vector< std::string > options ):
00226     WGEShaderDefineOptions( options ),
00227     m_property( prop )
00228 {
00229     // if the prop changes -> update options
00230     m_connection = m_property->getValueChangeCondition()->subscribeSignal(
00231         boost::bind( &WGEShaderPropertyDefineOptions< PropType >::propUpdated, this )
00232     );
00233     propUpdated();
00234 }
00235 
00236 template< typename PropType, typename PropIndexAdapter >
00237 WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::~WGEShaderPropertyDefineOptions()
00238 {
00239     // cleanup
00240     m_connection.disconnect();
00241 }
00242 
00243 template< typename PropType, typename PropIndexAdapter >
00244 void WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::propUpdated()
00245 {
00246     PropIndexAdapter functor;
00247     setActivationList( functor( m_property->get() ) );
00248 }
00249 
00250 template< typename PropType, typename PropIndexAdapter >
00251 PropType WGEShaderPropertyDefineOptions< PropType, PropIndexAdapter >::getProperty() const
00252 {
00253     return m_property;
00254 }
00255 
00256 #endif  // WGESHADERPROPERTYDEFINEOPTIONS_H
00257