OpenWalnut 1.3.1
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::shared_dynamic_cast< WPropertyGroup >( value );
00149     if( !v )
00150     {
00151         // it is not a WPropertyStruct with the same type
00152         return false;
00153     }
00154 
00155     // go through each of the given child props
00156     WPropertyGroup::PropertySharedContainerType::ReadTicket r = v->getReadTicket();
00157     size_t c = 0;   // number of props we have set
00158     for( WPropertyGroupBase::PropertyConstIterator it = r->get().begin(); it != r->get().end(); ++it )
00159     {
00160         // do we have a property named the same as in the source props?
00161         WPropertyBase::SPtr prop = findProperty( ( *it )->getName() );
00162         if( !prop )
00163         {
00164             // not found. Ignore it. We cannot set the target property as the source did not exist
00165             continue;
00166         }
00167         // ok there it is -> set
00168         prop->set( *it, recommendedOnly );
00169         c++;
00170     }
00171 
00172     // success only if all props have been set
00173     return ( c == r->get().size() );
00174 }
00175 
00176 void WPropertyGroup::removeProperty( boost::shared_ptr< WPropertyBase > prop )
00177 {
00178     if( !prop )
00179     {
00180         return;
00181     }
00182 
00183     // lock, unlocked if l looses focus
00184     PropertySharedContainerType::WriteTicket l = m_properties.getWriteTicket();
00185     l->get().erase( std::remove( l->get().begin(), l->get().end(), prop ), l->get().end() );
00186     m_updateCondition->remove( prop->getUpdateCondition() );
00187 }
00188 
00189 WPropGroup WPropertyGroup::addPropertyGroup( std::string name, std::string description, bool hide )
00190 {
00191     WPropGroup p = WPropGroup( new WPropertyGroup( name, description ) );
00192     p->setHidden( hide );
00193     addProperty( p );
00194     return p;
00195 }
00196 
00197 void WPropertyGroup::clear()
00198 {
00199     // lock, unlocked if l looses focus
00200     PropertySharedContainerType::WriteTicket l = m_properties.getWriteTicket();
00201     l->get().clear();
00202 }
00203 
00204 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00205 // convenience methods for
00206 // template< typename T>
00207 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial, bool hide = false );
00208 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00209 
00210 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial, bool hide )
00211 {
00212     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, hide ) );
00213 }
00214 
00215 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial, bool hide )
00216 {
00217     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, hide ) );
00218 }
00219 
00220 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial, bool hide )
00221 {
00222     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, hide ) );
00223 }
00224 
00225 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial, bool hide )
00226 {
00227     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, hide ) );
00228 }
00229 
00230 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial, bool hide )
00231 {
00232     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, hide ) );
00233 }
00234 
00235 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial, bool hide )
00236 {
00237     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, hide ) );
00238 }
00239 
00240 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial, bool hide )
00241 {
00242     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, hide ) );
00243 }
00244 
00245 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial, bool hide )
00246 {
00247     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, hide ) );
00248 }
00249 
00250 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial, bool hide )
00251 {
00252     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, hide ) );
00253 }
00254 
00255 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00256 // convenience methods for
00257 // template< typename T>
00258 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00259 //                                                          boost::shared_ptr< WCondition > condition, bool hide = false );
00260 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00261 
00262 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00263                                      boost::shared_ptr< WCondition > condition, bool hide )
00264 {
00265     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, hide ) );
00266 }
00267 
00268 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00269                                     boost::shared_ptr< WCondition > condition, bool hide )
00270 {
00271     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, hide ) );
00272 }
00273 
00274 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00275                                        boost::shared_ptr< WCondition > condition, bool hide )
00276 {
00277     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, hide ) );
00278 }
00279 
00280 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00281                                        boost::shared_ptr< WCondition > condition, bool hide )
00282 {
00283     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, hide ) );
00284 }
00285 
00286 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00287                                          boost::shared_ptr< WCondition > condition, bool hide )
00288 {
00289     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, hide ) );
00290 }
00291 
00292 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00293                                           boost::shared_ptr< WCondition > condition, bool hide )
00294 {
00295     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, hide ) );
00296 }
00297 
00298 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00299                                          boost::shared_ptr< WCondition > condition, bool hide )
00300 {
00301     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, hide ) );
00302 }
00303 
00304 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00305                                      boost::shared_ptr< WCondition > condition, bool hide )
00306 {
00307     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, hide ) );
00308 }
00309 
00310 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00311                                        boost::shared_ptr< WCondition > condition, bool hide )
00312 {
00313     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, hide ) );
00314 }
00315 
00316 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00317 // convenience methods for
00318 // template< typename T>
00319 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00320 //                                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
00321 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00322 
00323 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00324                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00325 {
00326     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, notifier, hide ) );
00327 }
00328 
00329 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00330                                     WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00331 {
00332     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, notifier, hide ) );
00333 }
00334 
00335 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00336                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00337 {
00338     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, notifier, hide ) );
00339 }
00340 
00341 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00342                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00343 {
00344     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, notifier, hide ) );
00345 }
00346 
00347 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00348                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00349 {
00350     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, notifier, hide ) );
00351 }
00352 
00353 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00354                                           WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00355 {
00356     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, notifier, hide ) );
00357 }
00358 
00359 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00360                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00361 {
00362     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, notifier, hide ) );
00363 }
00364 
00365 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00366                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00367 {
00368     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, notifier, hide ) );
00369 }
00370 
00371 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00372                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00373 {
00374     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, notifier, hide ) );
00375 }
00376 
00377 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00378 // convenience methods for
00379 // template< typename T>
00380 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
00381 //                                                          boost::shared_ptr< WCondition > condition,
00382 //                                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
00383 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00384 
00385 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL&   initial,
00386                                      boost::shared_ptr< WCondition > condition,
00387                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00388 {
00389     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, notifier, hide ) );
00390 }
00391 
00392 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT&    initial,
00393                                      boost::shared_ptr< WCondition > condition,
00394                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00395 {
00396     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, notifier, hide ) );
00397 }
00398 
00399 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
00400                                      boost::shared_ptr< WCondition > condition,
00401                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00402 {
00403     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, notifier, hide ) );
00404 }
00405 
00406 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
00407                                      boost::shared_ptr< WCondition > condition,
00408                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00409 {
00410     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, notifier, hide ) );
00411 }
00412 
00413 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH&   initial,
00414                                      boost::shared_ptr< WCondition > condition,
00415                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00416 {
00417     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, notifier, hide ) );
00418 }
00419 
00420 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION&   initial,
00421                                           boost::shared_ptr< WCondition > condition,
00422                                           WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00423 {
00424     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, notifier, hide ) );
00425 }
00426 
00427 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION&   initial,
00428                                          boost::shared_ptr< WCondition > condition,
00429                                          WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00430 {
00431     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, notifier, hide ) );
00432 }
00433 
00434 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR&   initial,
00435                                      boost::shared_ptr< WCondition > condition,
00436                                      WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00437 {
00438     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, notifier, hide ) );
00439 }
00440 
00441 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER&   initial,
00442                                        boost::shared_ptr< WCondition > condition,
00443                                        WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
00444 {
00445     return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, notifier, hide ) );
00446 }
00447