OpenWalnut  1.3.1
WPropertyGroup.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/tokenizer.hpp>
32 
33 #include "WLogger.h"
34 #include "exceptions/WPropertyUnknown.h"
35 
36 #include "WPropertyHelper.h"
37 
38 #include "WPropertyGroup.h"
39 
40 WPropertyGroup::WPropertyGroup( std::string name, std::string description ):
41  WPropertyGroupBase( name, description )
42 {
43  // an empty list is automatically configured for us in WPropertyGroupBase
44 }
45 
47 {
48  // clean up
49 }
50 
52  WPropertyGroupBase( from )
53 {
54  // an exact (deep) copy already is generated by WPropertyGroupBase. We do not have any additional members
55 }
56 
57 boost::shared_ptr< WPropertyBase > WPropertyGroup::clone()
58 {
59  // class copy constructor.
60  return boost::shared_ptr< WPropertyGroup >( new WPropertyGroup( *this ) );
61 }
62 
63 PROPERTY_TYPE WPropertyGroup::getType() const
64 {
65  return PV_GROUP;
66 }
67 
68 bool WPropertyGroup::setAsString( std::string /*value*/ )
69 {
70  // groups can't be set in any way. -> ignore it.
71  return true;
72 }
73 
75 {
76  // groups can't be set in any way. -> ignore it.
77  return "";
78 }
79 
80 /**
81  * Add the default constraints for a certain type of property. By default, nothing is added.
82  *
83  * \note Information properties never get constraints by default
84  *
85  * \param prop the property
86  *
87  * \return the property inserted gets returned.
88  */
89 template< typename T >
90 T _addDefaultConstraints( T prop )
91 {
92  return prop;
93 }
94 
95 /**
96  * Add the default constraints for a certain type of property. For selections, the PC_ISVALID constraint is added.
97  *
98  * \note Information properties never get constraints by default
99  *
100  * \param prop the property
101  *
102  * \return the property inserted gets returned.
103  */
104 WPropSelection _addDefaultConstraints( WPropSelection prop )
105 {
107  return prop;
108 }
109 
110 /**
111  * Add the default constraints for a certain type of property. For filenames, the PC_NOTEMPTY constraint is added.
112  *
113  * \note Information properties never get constraints by default
114  *
115  * \param prop the property
116  *
117  * \return the property inserted gets returned.
118  */
119 WPropFilename _addDefaultConstraints( WPropFilename prop )
120 {
122  return prop;
123 }
124 
125 /**
126  * Add the default constraints for a certain type of property. Please specialize _addDefaultConstraints for your special needs and prop types.
127  *
128  * \note Information properties never get constraints by default
129  *
130  * \param prop the property to add the constraints to
131  *
132  * \return the property inserted
133  */
134 template< typename T >
135 T addDefaultConstraints( T prop )
136 {
137  if( prop->getPurpose() == PV_PURPOSE_INFORMATION )
138  {
139  return prop;
140  }
141 
142  return _addDefaultConstraints( prop );
143 }
144 
145 bool WPropertyGroup::set( boost::shared_ptr< WPropertyBase > value, bool recommendedOnly )
146 {
147  // is this the same type as we are?
148  WPropertyGroup::SPtr v = boost::shared_dynamic_cast< WPropertyGroup >( value );
149  if( !v )
150  {
151  // it is not a WPropertyStruct with the same type
152  return false;
153  }
154 
155  // go through each of the given child props
157  size_t c = 0; // number of props we have set
158  for( WPropertyGroupBase::PropertyConstIterator it = r->get().begin(); it != r->get().end(); ++it )
159  {
160  // do we have a property named the same as in the source props?
161  WPropertyBase::SPtr prop = findProperty( ( *it )->getName() );
162  if( !prop )
163  {
164  // not found. Ignore it. We cannot set the target property as the source did not exist
165  continue;
166  }
167  // ok there it is -> set
168  prop->set( *it, recommendedOnly );
169  c++;
170  }
171 
172  // success only if all props have been set
173  return ( c == r->get().size() );
174 }
175 
176 void WPropertyGroup::removeProperty( boost::shared_ptr< WPropertyBase > prop )
177 {
178  if( !prop )
179  {
180  return;
181  }
182 
183  // lock, unlocked if l looses focus
185  l->get().erase( std::remove( l->get().begin(), l->get().end(), prop ), l->get().end() );
186  m_updateCondition->remove( prop->getUpdateCondition() );
187 }
188 
189 WPropGroup WPropertyGroup::addPropertyGroup( std::string name, std::string description, bool hide )
190 {
191  WPropGroup p = WPropGroup( new WPropertyGroup( name, description ) );
192  p->setHidden( hide );
193  addProperty( p );
194  return p;
195 }
196 
198 {
199  // lock, unlocked if l looses focus
201  l->get().clear();
202 }
203 
204 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205 // convenience methods for
206 // template< typename T>
207 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial, bool hide = false );
208 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 
210 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial, bool hide )
211 {
212  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, hide ) );
213 }
214 
215 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial, bool hide )
216 {
217  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, hide ) );
218 }
219 
220 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial, bool hide )
221 {
222  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, hide ) );
223 }
224 
225 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial, bool hide )
226 {
227  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, hide ) );
228 }
229 
230 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial, bool hide )
231 {
232  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, hide ) );
233 }
234 
235 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial, bool hide )
236 {
237  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, hide ) );
238 }
239 
240 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial, bool hide )
241 {
242  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, hide ) );
243 }
244 
245 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial, bool hide )
246 {
247  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, hide ) );
248 }
249 
250 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial, bool hide )
251 {
252  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, hide ) );
253 }
254 
255 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
256 // convenience methods for
257 // template< typename T>
258 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
259 // boost::shared_ptr< WCondition > condition, bool hide = false );
260 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
261 
262 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
263  boost::shared_ptr< WCondition > condition, bool hide )
264 {
265  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, hide ) );
266 }
267 
268 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
269  boost::shared_ptr< WCondition > condition, bool hide )
270 {
271  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, hide ) );
272 }
273 
274 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
275  boost::shared_ptr< WCondition > condition, bool hide )
276 {
277  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, hide ) );
278 }
279 
280 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
281  boost::shared_ptr< WCondition > condition, bool hide )
282 {
283  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, hide ) );
284 }
285 
286 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
287  boost::shared_ptr< WCondition > condition, bool hide )
288 {
289  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, hide ) );
290 }
291 
292 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
293  boost::shared_ptr< WCondition > condition, bool hide )
294 {
295  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, hide ) );
296 }
297 
298 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
299  boost::shared_ptr< WCondition > condition, bool hide )
300 {
301  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, hide ) );
302 }
303 
304 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
305  boost::shared_ptr< WCondition > condition, bool hide )
306 {
307  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, hide ) );
308 }
309 
310 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
311  boost::shared_ptr< WCondition > condition, bool hide )
312 {
313  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, hide ) );
314 }
315 
316 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
317 // convenience methods for
318 // template< typename T>
319 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
320 // WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
321 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
322 
323 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
324  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
325 {
326  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, notifier, hide ) );
327 }
328 
329 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
330  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
331 {
332  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, notifier, hide ) );
333 }
334 
335 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
336  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
337 {
338  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, notifier, hide ) );
339 }
340 
341 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
342  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
343 {
344  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, notifier, hide ) );
345 }
346 
347 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
348  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
349 {
350  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, notifier, hide ) );
351 }
352 
353 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
354  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
355 {
356  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, notifier, hide ) );
357 }
358 
359 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
360  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
361 {
362  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, notifier, hide ) );
363 }
364 
365 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
366  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
367 {
368  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, notifier, hide ) );
369 }
370 
371 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
372  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
373 {
374  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, notifier, hide ) );
375 }
376 
377 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
378 // convenience methods for
379 // template< typename T>
380 // boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
381 // boost::shared_ptr< WCondition > condition,
382 // WPropertyBase::PropertyChangeNotifierType notifier, bool hide = false );
383 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
384 
385 WPropBool WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_BOOL& initial,
386  boost::shared_ptr< WCondition > condition,
387  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
388 {
389  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_BOOL >( name, description, initial, condition, notifier, hide ) );
390 }
391 
392 WPropInt WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_INT& initial,
393  boost::shared_ptr< WCondition > condition,
394  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
395 {
396  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_INT >( name, description, initial, condition, notifier, hide ) );
397 }
398 
399 WPropDouble WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_DOUBLE& initial,
400  boost::shared_ptr< WCondition > condition,
401  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
402 {
403  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_DOUBLE >( name, description, initial, condition, notifier, hide ) );
404 }
405 
406 WPropString WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_STRING& initial,
407  boost::shared_ptr< WCondition > condition,
408  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
409 {
410  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_STRING >( name, description, initial, condition, notifier, hide ) );
411 }
412 
413 WPropFilename WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_PATH& initial,
414  boost::shared_ptr< WCondition > condition,
415  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
416 {
417  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_PATH >( name, description, initial, condition, notifier, hide ) );
418 }
419 
420 WPropSelection WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_SELECTION& initial,
421  boost::shared_ptr< WCondition > condition,
422  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
423 {
424  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_SELECTION >( name, description, initial, condition, notifier, hide ) );
425 }
426 
427 WPropPosition WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_POSITION& initial,
428  boost::shared_ptr< WCondition > condition,
429  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
430 {
431  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_POSITION >( name, description, initial, condition, notifier, hide ) );
432 }
433 
434 WPropColor WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_COLOR& initial,
435  boost::shared_ptr< WCondition > condition,
436  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
437 {
438  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_COLOR >( name, description, initial, condition, notifier, hide ) );
439 }
440 
441 WPropTrigger WPropertyGroup::addProperty( std::string name, std::string description, const WPVBaseTypes::PV_TRIGGER& initial,
442  boost::shared_ptr< WCondition > condition,
443  WPropertyBase::PropertyChangeNotifierType notifier, bool hide )
444 {
445  return addDefaultConstraints( addProperty< WPVBaseTypes::PV_TRIGGER >( name, description, initial, condition, notifier, hide ) );
446 }
447