OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WProperties.cpp
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 #include <iostream>
26 #include <map>
27 #include <string>
28 #include <vector>
29 #include <algorithm>
30 
31 #include <boost/lexical_cast.hpp>
32 #include <boost/tokenizer.hpp>
33 
34 #include "WLogger.h"
35 #include "exceptions/WPropertyUnknown.h"
36 
37 #include "WPropertyHelper.h"
38 
39 #include "WProperties.h"
40 
41 WProperties::WProperties( std::string name, std::string description ):
42  WPropertyBase( name, description ),
43  m_properties(),
44  m_childUpdateCondition( new WConditionSet() )
45 {
47 }
48 
50 {
51 }
52 
54  WPropertyBase( from ),
55  m_properties(),
56  m_childUpdateCondition( new WConditionSet() )
57 {
58  // copy the properties inside
59 
60  // lock, unlocked if l looses focus
62 
63  // we need to make a deep copy here.
64  for( PropertyConstIterator iter = l->get().begin(); iter != l->get().end(); ++iter )
65  {
66  // clone them to keep dynamic type
67  addProperty( ( *iter )->clone() );
68  }
69 
70  // unlock explicitly
71  l.reset();
72 
73  // add the change condition of the prop list
75 }
76 
77 boost::shared_ptr< WPropertyBase > WProperties::clone()
78 {
79  // class copy constructor.
80  return boost::shared_ptr< WProperties >( new WProperties( *this ) );
81 }
82 
83 PROPERTY_TYPE WProperties::getType() const
84 {
85  return PV_GROUP;
86 }
87 
88 bool WProperties::setAsString( std::string /*value*/ )
89 {
90  // groups can't be set in any way. -> ignore it.
91  return true;
92 }
93 
95 {
96  // groups can't be set in any way. -> ignore it.
97  return "";
98 }
99 
100 /**
101  * Add the default constraints for a certain type of property. By default, nothing is added.
102  *
103  * \note Information properties never get constraints by default
104  *
105  * \param prop the property
106  *
107  * \return the property inserted gets returned.
108  */
109 template< typename T >
110 T _addDefaultConstraints( T prop )
111 {
112  return prop;
113 }
114 
115 /**
116  * Add the default constraints for a certain type of property. For ints, a min and max is set to 0 and 100.
117  *
118  * \note Information properties never get constraints by default
119  *
120  * \param prop the property
121  *
122  * \return the property inserted gets returned.
123  */
124 WPropInt _addDefaultConstraints( WPropInt prop )
125 {
126  prop->setMin( 0 );
127  prop->setMax( 100 );
128  return prop;
129 }
130 
131 /**
132  * Add the default constraints for a certain type of property. For doubles, a min and max is set to 0 and 100.
133  *
134  * \note Information properties never get constraints by default
135  *
136  * \param prop the property
137  *
138  * \return the property inserted gets returned.
139  */
140 WPropDouble _addDefaultConstraints( WPropDouble prop )
141 {
142  prop->setMin( 0.0 );
143  prop->setMax( 100.0 );
144  return prop;
145 }
146 
147 /**
148  * Add the default constraints for a certain type of property. For selections, the PC_ISVALID constraint is added.
149  *
150  * \note Information properties never get constraints by default
151  *
152  * \param prop the property
153  *
154  * \return the property inserted gets returned.
155  */
156 WPropSelection _addDefaultConstraints( WPropSelection prop )
157 {
159  return prop;
160 }
161 
162 /**
163  * Add the default constraints for a certain type of property. For filenames, the PC_NOTEMPTY constraint is added.
164  *
165  * \note Information properties never get constraints by default
166  *
167  * \param prop the property
168  *
169  * \return the property inserted gets returned.
170  */
171 WPropFilename _addDefaultConstraints( WPropFilename prop )
172 {
174  return prop;
175 }
176 
177 /**
178  * Add the default constraints for a certain type of property. Please specialize _addDefaultConstraints for your special needs and prop types.
179  *
180  * \note Information properties never get constraints by default
181  *
182  * \param prop the property to add the constraints to
183  *
184  * \return the property inserted
185  */
186 template< typename T >
187 T addDefaultConstraints( T prop )
188 {
189  if( prop->getPurpose() == PV_PURPOSE_INFORMATION )
190  {
191  return prop;
192  }
193 
194  return _addDefaultConstraints( prop );
195 }
196 
197 bool WProperties::set( boost::shared_ptr< WPropertyBase > /*value*/ )
198 {
199  return true;
200 }
201 
202 bool WProperties::propNamePredicate( boost::shared_ptr< WPropertyBase > prop1, boost::shared_ptr< WPropertyBase > prop2 ) const
203 {
204  return ( prop1->getName() == prop2->getName() );
205 }
206 void WProperties::removeProperty( boost::shared_ptr< WPropertyBase > prop )
207 {
208  if( !prop )
209  {
210  return;
211  }
212 
213  // lock, unlocked if l looses focus
215  l->get().erase( std::remove( l->get().begin(), l->get().end(), prop ), l->get().end() );
216  m_childUpdateCondition->remove( prop->getUpdateCondition() );
217 }
218 
219 boost::shared_ptr< WPropertyBase > WProperties::findProperty( const WProperties* const props, std::string name ) const
220 {
221  boost::shared_ptr< WPropertyBase > result = boost::shared_ptr< WPropertyBase >();
222 
223  // lock, unlocked if l looses focus
225 
226  // iterate over the items
227  for( PropertyContainerType::const_iterator it = l->get().begin(); it != l->get().end(); ++it )
228  {
229  if( ( *it )->getName() == name )
230  {
231  result = ( *it );
232  break;
233  }
234  }
235 
236  // done. Unlocked after l looses focus.
237  return result;
238 }
239 
240 boost::shared_ptr< WPropertyBase > WProperties::findProperty( std::string name ) const
241 {
242  boost::shared_ptr< WPropertyBase > result = boost::shared_ptr< WPropertyBase >();
243 
244  // tokenize the name -> contains any paths?
245  typedef boost::tokenizer<boost::char_separator< char > > tokenizer;
246  boost::char_separator< char > sep( "/" ); // separate by /
247  tokenizer tok( name, sep );
248 
249  // iterate along the path
250  const WProperties* curProps = this; // the group currently in while traversing the path
251  for( tokenizer::iterator it = tok.begin(); it != tok.end(); ++it )
252  {
253  // was the last token not a group?
254  if( result && ( result->getType() != PV_GROUP ) )
255  {
256  // no it wasn't. This means that one token inside the path is no group, but it needs to be one
257  return boost::shared_ptr< WPropertyBase >();
258  }
259 
260  // get the properties along the path
261  result = findProperty( curProps, boost::lexical_cast< std::string >( *it ) );
262  if( !result )
263  {
264  // not found? Return NULL.
265  return boost::shared_ptr< WPropertyBase >();
266  }
267  else if( result && ( result->getType() == PV_GROUP ) )
268  {
269  // it is a group. Go down
270  curProps = result->toPropGroup().get();
271  }
272  }
273 
274  return result;
275 }
276 
277 bool WProperties::existsProperty( std::string name )
278 {
279  return ( findProperty( name ) != boost::shared_ptr< WPropertyBase >() );
280 }
281 
282 boost::shared_ptr< WPropertyBase > WProperties::getProperty( std::string name )
283 {
284  boost::shared_ptr< WPropertyBase > p = findProperty( name );
285  if( p == boost::shared_ptr< WPropertyBase >() )
286  {
287  throw WPropertyUnknown( std::string( "Property \"" + name + "\" can't be found." ) );
288  }
289 
290  return p;
291 }
292 
294 {
295  return m_properties.getReadTicket();
296 }
297 
299 {
300  return m_properties.getReadTicket();
301 }
302 
303 WPropGroup WProperties::addPropertyGroup( std::string name, std::string description, bool hide )
304 {
305  WPropGroup p = WPropGroup( new WProperties( name, description ) );
306  p->setHidden( hide );
307  addProperty( p );
308  return p;
309 }
310 
312 {
313  // lock, unlocked if l looses focus
315  l->get().clear();
316 }
317 
318 boost::shared_ptr< WCondition > WProperties::getChildUpdateCondition() const
319 {
320  return m_childUpdateCondition;
321 }
322 
323 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
324 // convenience methods for
325 // template< typename T>
326 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial, bool hide = false );
327 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
328 
329 WPropBool WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial, bool hide )
330 {
331  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, hide ) );
332 }
333 
334 WPropInt WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial, bool hide )
335 {
336  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, hide ) );
337 }
338 
339 WPropDouble WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial, bool hide )
340 {
341  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, hide ) );
342 }
343 
344 WPropString WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial, bool hide )
345 {
346  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, hide ) );
347 }
348 
349 WPropFilename WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial, bool hide )
350 {
351  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, hide ) );
352 }
353 
354 WPropSelection WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial, bool hide )
355 {
356  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, hide ) );
357 }
358 
359 WPropPosition WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial, bool hide )
360 {
361  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, hide ) );
362 }
363 
364 WPropColor WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial, bool hide )
365 {
366  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, hide ) );
367 }
368 
369 WPropTrigger WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial, bool hide )
370 {
371  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, hide ) );
372 }
373 
374 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
375 // convenience methods for
376 // template< typename T>
377 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
378 // boost::shared_ptr< WCondition > condition, bool hide = false );
379 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
380 
381 WPropBool WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
382  boost::shared_ptr< WCondition > condition, bool hide )
383 {
384  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, hide ) );
385 }
386 
387 WPropInt WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
388  boost::shared_ptr< WCondition > condition, bool hide )
389 {
390  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, hide ) );
391 }
392 
393 WPropDouble WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
394  boost::shared_ptr< WCondition > condition, bool hide )
395 {
396  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, hide ) );
397 }
398 
399 WPropString WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
400  boost::shared_ptr< WCondition > condition, bool hide )
401 {
402  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, hide ) );
403 }
404 
405 WPropFilename WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
406  boost::shared_ptr< WCondition > condition, bool hide )
407 {
408  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, hide ) );
409 }
410 
411 WPropSelection WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
412  boost::shared_ptr< WCondition > condition, bool hide )
413 {
414  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, hide ) );
415 }
416 
417 WPropPosition WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
418  boost::shared_ptr< WCondition > condition, bool hide )
419 {
420  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, hide ) );
421 }
422 
423 WPropColor WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
424  boost::shared_ptr< WCondition > condition, bool hide )
425 {
426  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, hide ) );
427 }
428 
429 WPropTrigger WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
430  boost::shared_ptr< WCondition > condition, bool hide )
431 {
432  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, hide ) );
433 }
434 
435 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
436 // convenience methods for
437 // template< typename T>
438 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
439 // WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
440 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
441 
442 WPropBool WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
443  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
444 {
445  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, notifier, hide ) );
446 }
447 
448 WPropInt WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
449  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
450 {
451  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, notifier, hide ) );
452 }
453 
454 WPropDouble WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
455  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
456 {
457  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, notifier, hide ) );
458 }
459 
460 WPropString WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
461  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
462 {
463  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, notifier, hide ) );
464 }
465 
466 WPropFilename WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
467  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
468 {
469  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, notifier, hide ) );
470 }
471 
472 WPropSelection WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
473  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
474 {
475  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, notifier, hide ) );
476 }
477 
478 WPropPosition WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
479  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
480 {
481  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, notifier, hide ) );
482 }
483 
484 WPropColor WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
485  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
486 {
487  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, notifier, hide ) );
488 }
489 
490 WPropTrigger WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
491  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
492 {
493  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, notifier, hide ) );
494 }
495 
496 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
497 // convenience methods for
498 // template< typename T>
499 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
500 // boost::shared_ptr< WCondition > condition,
501 // WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
502 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
503 
504 WPropBool WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
505  boost::shared_ptr< WCondition > condition,
506  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
507 {
508  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, notifier, hide ) );
509 }
510 
511 WPropInt WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
512  boost::shared_ptr< WCondition > condition,
513  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
514 {
515  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, notifier, hide ) );
516 }
517 
518 WPropDouble WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
519  boost::shared_ptr< WCondition > condition,
520  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
521 {
522  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, notifier, hide ) );
523 }
524 
525 WPropString WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
526  boost::shared_ptr< WCondition > condition,
527  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
528 {
529  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, notifier, hide ) );
530 }
531 
532 WPropFilename WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
533  boost::shared_ptr< WCondition > condition,
534  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
535 {
536  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, notifier, hide ) );
537 }
538 
539 WPropSelection WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
540  boost::shared_ptr< WCondition > condition,
541  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
542 {
543  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, notifier, hide ) );
544 }
545 
546 WPropPosition WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
547  boost::shared_ptr< WCondition > condition,
548  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
549 {
550  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, notifier, hide ) );
551 }
552 
553 WPropColor WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
554  boost::shared_ptr< WCondition > condition,
555  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
556 {
557  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, notifier, hide ) );
558 }
559 
560 WPropTrigger WProperties::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
561  boost::shared_ptr< WCondition > condition,
562  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
563 {
564  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, notifier, hide ) );
565 }
566