OpenWalnut  1.4.0
WPropertyGroup.cpp
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 #include <iostream>
00026 #include <map>
00027 #include <string>
00028 #include <vector>
00029 #include <algorithm>
00030 
00031 #include <boost/tokenizer.hpp>
00032 
00033 #include "WLogger.h"
00034 #include "exceptions/WPropertyUnknown.h"
00035 
00036 #include "WPropertyHelper.h"
00037 
00038 #include "WPropertyGroup.h"
00039 
00040 WPropertyGroup::WPropertyGroup( std::string name, std::string description ):
00041     WPropertyGroupBase( name, description )
00042 {
00043     // an empty list is automatically configured for us in WPropertyGroupBase
00044 }
00045 
00046 WPropertyGroup::~WPropertyGroup()
00047 {
00048     // clean up
00049 }
00050 
00051 WPropertyGroup::WPropertyGroup( const WPropertyGroup& from ):
00052     WPropertyGroupBase( from )
00053 {
00054     // an exact (deep) copy already is generated by WPropertyGroupBase. We do not have any additional members
00055 }
00056 
00057 boost::shared_ptr< WPropertyBase > WPropertyGroup::clone()
00058 {
00059     // class copy constructor.
00060     return boost::shared_ptr< WPropertyGroup >( new WPropertyGroup( *this ) );
00061 }
00062 
00063 PROPERTY_TYPE WPropertyGroup::getType() const
00064 {
00065     return PV_GROUP;
00066 }
00067 
00068 bool WPropertyGroup::setAsString( std::string /*value*/ )
00069 {
00070     // groups can't be set in any way. -> ignore it.
00071     return true;
00072 }
00073 
00074 std::string WPropertyGroup::getAsString()
00075 {
00076     // groups can't be set in any way. -> ignore it.
00077     return "";
00078 }
00079 
00080 /**
00081  * Add the default constraints for a certain type of property. By default, nothing is added.
00082  *
00083  * \note Information properties never get constraints by default
00084  *
00085  * \param prop the property
00086  *
00087  * \return the property inserted gets returned.
00088  */
00089 template< typename T >
00090 T _addDefaultConstraints( T prop )
00091 {
00092     return prop;
00093 }
00094 
00095 /**
00096  * Add the default constraints for a certain type of property. For selections, the PC_ISVALID constraint is added.
00097  *
00098  * \note Information properties never get constraints by default
00099  *
00100  * \param prop the property
00101  *
00102  * \return the property inserted gets returned.
00103  */
00104 WPropSelection _addDefaultConstraints( WPropSelection prop )
00105 {
00106     WPropertyHelper::PC_ISVALID::addTo( prop );
00107     return prop;
00108 }
00109 
00110 /**
00111  * Add the default constraints for a certain type of property. For filenames, the PC_NOTEMPTY constraint is added.
00112  *
00113  * \note Information properties never get constraints by default
00114  *
00115  * \param prop the property
00116  *
00117  * \return the property inserted gets returned.
00118  */
00119 WPropFilename _addDefaultConstraints( WPropFilename prop )
00120 {
00121     WPropertyHelper::PC_NOTEMPTY::addTo( prop );
00122     return prop;
00123 }
00124 
00125 /**
00126  * Add the default constraints for a certain type of property. Please specialize _addDefaultConstraints for your special needs and prop types.
00127  *
00128  * \note Information properties never get constraints by default
00129  *
00130  * \param prop the property to add the constraints to
00131  *
00132  * \return the property inserted
00133  */
00134 template< typename T >
00135 T addDefaultConstraints( T prop )
00136 {
00137     if( prop->getPurpose() == PV_PURPOSE_INFORMATION )
00138     {
00139         return prop;
00140     }
00141 
00142     return _addDefaultConstraints( prop );
00143 }
00144 
00145 bool WPropertyGroup::set( boost::shared_ptr< WPropertyBase > value, bool recommendedOnly )
00146 {
00147     // is this the same type as we are?
00148     WPropertyGroup::SPtr v = boost::dynamic_pointer_cast< WPropertyGroup >( value );
00149     if( !v )
00150     {
00151         // it is not a WPropertyStruct with the same type
00152         return false;
00153     }
00154 
00155     // forward, use empty exclude list
00156     return set( v, std::vector< std::string >(), recommendedOnly );
00157 }
00158 
00159 bool WPropertyGroup::set( boost::shared_ptr< WPropertyGroup > value, std::vector< std::string > exclude, bool recommendedOnly )
00160 {
00161     return setImpl( value, "", exclude, recommendedOnly );
00162 }
00163 
00164 bool WPropertyGroup::setImpl( boost::shared_ptr< WPropertyGroup > value, std::string path, std::vector< std::string > exclude, bool recommendedOnly )
00165 {
00166     // go through each of the given child props
00167     WPropertyGroup::PropertySharedContainerType::ReadTicket r = value->getReadTicket();
00168     size_t c = 0;   // number of props we have set
00169     for( WPropertyGroupBase::PropertyConstIterator it = r->get().begin(); it != r->get().end(); ++it )
00170     {
00171         // do we have a property named the same as in the source props?
00172         WPropertyBase::SPtr prop = findProperty( ( *it )->getName() );
00173         if( !prop )
00174         {
00175             // not found. Ignore it. We cannot set the target property as the source did not exist
00176             continue;
00177         }
00178 
00179         // ok there it is. check exclude list.
00180         // first: use the current property name and append it to path
00181         std::string completePath = path + WPropertyGroupBase::separator + ( *it )->getName();
00182         if( path == "" )
00183         {
00184             // no separator if the path is empty now
00185             completePath = ( *it )->getName();
00186         }
00187 
00188         // now check exclude list
00189         if( std::find( exclude.begin(), exclude.end(), completePath ) != exclude.end() )
00190         {
00191             // it is excluded
00192             continue;
00193         }
00194 
00195         // not excluded. Is it a group or something else?
00196         WPropertyGroup::SPtr meAsGroup = boost::dynamic_pointer_cast< WPropertyGroup >( prop );
00197         WPropertyGroup::SPtr inputAsGroup = boost::dynamic_pointer_cast< WPropertyGroup >( *it );
00198         if( inputAsGroup && meAsGroup )
00199         {
00200             // not excluded and is group, recurse:
00201             c += meAsGroup->setImpl( inputAsGroup, completePath, exclude, recommendedOnly );
00202         }
00203         else if( inputAsGroup || meAsGroup ) // one group and one not a group, skip
00204         {
00205             continue;
00206         }
00207         else
00208         {
00209             c += prop->set( *it, recommendedOnly );
00210         }
00211     }
00212 
00213     // success only if all props have been set
00214     // NOTE: it think this only will ever be correct if we have no nested groups ...
00215     return ( c == r->get().size() );
00216 }
00217 
00218 void WPropertyGroup::removeProperty( boost::shared_ptr< WPropertyBase > prop )
00219 {
00220     if( !prop )
00221     {
00222         return;
00223     }
00224 
00225     // lock, unlocked if l looses focus
00226     PropertySharedContainerType::WriteTicket l = m_properties.getWriteTicket();
00227     l->get().erase( std::remove( l->get().begin(), l->get().end(), prop ), l->get().end() );
00228     m_updateCondition->remove( prop->getUpdateCondition() );
00229 }
00230 
00231 WPropGroup WPropertyGroup::addPropertyGroup( std::string name, std::string description, bool hide )
00232 {
00233     WPropGroup p = WPropGroup( new WPropertyGroup( name, description ) );
00234     p->setHidden( hide );
00235     addProperty( p );
00236     return p;
00237 }
00238 
00239 void WPropertyGroup::clear()
00240 {
00241     // lock, unlocked if l looses focus
00242     PropertySharedContainerType::WriteTicket l = m_properties.getWriteTicket();
00243     l->get().clear();
00244 }
00245 
00246 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00247 // convenience methods for
00248 // template< typename T>
00249 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial, bool hide = false );
00250 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00251 
00252 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial, bool hide )
00253 {
00254     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, hide ) );
00255 }
00256 
00257 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial, bool hide )
00258 {
00259     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, hide ) );
00260 }
00261 
00262 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial, bool hide )
00263 {
00264     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, hide ) );
00265 }
00266 
00267 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial, bool hide )
00268 {
00269     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, hide ) );
00270 }
00271 
00272 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial, bool hide )
00273 {
00274     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, hide ) );
00275 }
00276 
00277 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial, bool hide )
00278 {
00279     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, hide ) );
00280 }
00281 
00282 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial, bool hide )
00283 {
00284     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, hide ) );
00285 }
00286 
00287 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial, bool hide )
00288 {
00289     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, hide ) );
00290 }
00291 
00292 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial, bool hide )
00293 {
00294     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, hide ) );
00295 }
00296 
00297 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00298 // convenience methods for
00299 // template< typename T>
00300 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00301 //                                                          boost::shared_ptr< WCondition > condition, bool hide = false );
00302 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00303 
00304 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00305                                      boost::shared_ptr< WCondition > condition, bool hide )
00306 {
00307     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, hide ) );
00308 }
00309 
00310 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00311                                     boost::shared_ptr< WCondition > condition, bool hide )
00312 {
00313     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, hide ) );
00314 }
00315 
00316 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00317                                        boost::shared_ptr< WCondition > condition, bool hide )
00318 {
00319     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, hide ) );
00320 }
00321 
00322 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00323                                        boost::shared_ptr< WCondition > condition, bool hide )
00324 {
00325     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, hide ) );
00326 }
00327 
00328 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00329                                          boost::shared_ptr< WCondition > condition, bool hide )
00330 {
00331     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, hide ) );
00332 }
00333 
00334 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00335                                           boost::shared_ptr< WCondition > condition, bool hide )
00336 {
00337     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, hide ) );
00338 }
00339 
00340 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00341                                          boost::shared_ptr< WCondition > condition, bool hide )
00342 {
00343     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, hide ) );
00344 }
00345 
00346 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00347                                      boost::shared_ptr< WCondition > condition, bool hide )
00348 {
00349     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, hide ) );
00350 }
00351 
00352 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00353                                        boost::shared_ptr< WCondition > condition, bool hide )
00354 {
00355     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, hide ) );
00356 }
00357 
00358 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00359 // convenience methods for
00360 // template< typename T>
00361 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00362 //                                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
00363 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00364 
00365 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00366                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00367 {
00368     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, notifier, hide ) );
00369 }
00370 
00371 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00372                                     WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00373 {
00374     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, notifier, hide ) );
00375 }
00376 
00377 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00378                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00379 {
00380     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, notifier, hide ) );
00381 }
00382 
00383 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00384                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00385 {
00386     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, notifier, hide ) );
00387 }
00388 
00389 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00390                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00391 {
00392     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, notifier, hide ) );
00393 }
00394 
00395 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00396                                           WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00397 {
00398     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, notifier, hide ) );
00399 }
00400 
00401 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00402                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00403 {
00404     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, notifier, hide ) );
00405 }
00406 
00407 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00408                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00409 {
00410     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, notifier, hide ) );
00411 }
00412 
00413 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00414                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00415 {
00416     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, notifier, hide ) );
00417 }
00418 
00419 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00420 // convenience methods for
00421 // template< typename T>
00422 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00423 //                                                          boost::shared_ptr< WCondition > condition,
00424 //                                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
00425 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00426 
00427 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00428                                      boost::shared_ptr< WCondition > condition,
00429                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00430 {
00431     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, notifier, hide ) );
00432 }
00433 
00434 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00435                                      boost::shared_ptr< WCondition > condition,
00436                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00437 {
00438     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, notifier, hide ) );
00439 }
00440 
00441 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00442                                      boost::shared_ptr< WCondition > condition,
00443                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00444 {
00445     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, notifier, hide ) );
00446 }
00447 
00448 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00449                                      boost::shared_ptr< WCondition > condition,
00450                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00451 {
00452     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, notifier, hide ) );
00453 }
00454 
00455 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00456                                      boost::shared_ptr< WCondition > condition,
00457                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00458 {
00459     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, notifier, hide ) );
00460 }
00461 
00462 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00463                                           boost::shared_ptr< WCondition > condition,
00464                                           WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00465 {
00466     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, notifier, hide ) );
00467 }
00468 
00469 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00470                                          boost::shared_ptr< WCondition > condition,
00471                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00472 {
00473     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, notifier, hide ) );
00474 }
00475 
00476 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00477                                      boost::shared_ptr< WCondition > condition,
00478                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00479 {
00480     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, notifier, hide ) );
00481 }
00482 
00483 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00484                                        boost::shared_ptr< WCondition > condition,
00485                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00486 {
00487     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, notifier, hide ) );
00488 }
00489