Image Component Library (ICL)
Configurable.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Configurable.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Exception.h>
35 #include <ICLUtils/SmartPtr.h>
36 #include <ICLUtils/Any.h>
37 #include <ICLUtils/Function.h>
39 #include <ICLUtils/Mutex.h>
40 
41 #include <vector>
42 #include <string>
43 #include <algorithm>
44 #include <map>
45 
46 namespace icl{
47  namespace utils{
48 
50 
195 
196  friend class ConfigurableProxy;
197 
198  public:
200  struct Property{
201  Property():configurable(0),volatileness(0){}
203  const std::string &name, const std::string &type, const std::string &info, const std::string &value,
204  int volatileness, const std::string &tooltip):configurable(parent),name(name),type(type),info(info),value(value),
205  volatileness(volatileness), tooltip(tooltip){}
207  std::string name;
208  std::string type;
209  std::string info;
210  std::string value;
212  std::string tooltip;
213  std::string childPrefix;
215  bool operator==(const std::string &name) const { return this->name == name; }
216  };
217 
218  private:
219 
221  typedef std::map<std::string,Property> PropertyMap;
222 
225 
229  std::map<int,std::string> m_ordering;
230 
232  std::map<const Configurable*, std::string> m_childConfigurables;
233 
236 
238  std::string m_ID;
239 
241  static std::map<std::string,Configurable*> m_instances;
242 
244  std::vector<std::string> m_deactivated;
245 
247 
249 
250  protected:
251 
253 
258  void addProperty(const std::string &name, const std::string &type,
259  const std::string &info, const Any &value=Any(),
260  const int volatileness=0, const std::string &tooltip=std::string()) ;
261 
263 
268  void addChildConfigurable(Configurable *configurable, const std::string &childPrefix="");
269 
271  void removeChildConfigurable(Configurable *configurable);
272 
274 
275  Property &prop(const std::string &propertyName) ;
276 
278 
279  const Property &prop(const std::string &propertyName) const ;
280 
282 
286  Configurable(const std::string &ID="", bool ordered=true) ;
287 
288  public:
289 
291  virtual ~Configurable(){}
292 
294 
295  Configurable(const Configurable &other);
296 
298 
299  Configurable &operator=(const Configurable &other);
300 
302 
303  void setConfigurableID(const std::string &ID) ;
304 
306  inline const std::string &getConfigurableID() const{
307  return m_ID;
308  }
309 
311  bool isOrderedFlagSet() const{
312  return m_isOrdered;
313  }
314 
316 
328  void deactivateProperty(const std::string &pattern);
329 
331 
333  void deleteDeactivationPattern(const std::string &pattern);
334 
336  std::vector<std::string> getPropertyListWithoutDeactivated() const;
337 
339 
345  virtual void adaptProperty(const std::string &name, const std::string &newType,
346  const std::string &newInfo, const std::string &newToolTip) ;
347 
348 
350 
352  static std::string create_default_ID(const std::string &prefix);
353 
354 
357 
359  void registerCallback(const Callback &cb){
360  callbacks.push_back(cb);
361  }
362 
364  void removedCallback(const Callback &cb);
365 
367 
368  void syncChangesTo(Configurable *others, int num=1);
369 
370  protected:
372  std::vector<Callback> callbacks;
373 
375 
379  void call_callbacks(const std::string &propertyName,const Configurable* caller) const;
380 
381  public:
382 
384  static const std::vector<std::string> EMPTY_VEC;
385 
387 
388  static Configurable* get(const std::string &id);
389 
390 
392 
396  virtual void setPropertyValue(const std::string &propertyName, const Any &value) ;
397 
399 
401  virtual std::vector<std::string> getPropertyList() const;
402 
404 
406  virtual bool supportsProperty(const std::string &propertyName) const;
407 
409 
412  virtual void saveProperties(const std::string &filename, const std::vector<std::string> &propertiesToSkip=EMPTY_VEC) const;
413 
415 
416  virtual void loadProperties(const std::string &filename,const std::vector<std::string> &propertiesToSkip=EMPTY_VEC);
417 
419 
443  virtual std::string getPropertyType(const std::string &propertyName) const{
444  return prop(propertyName).type;
445  }
446 
448 
462  virtual std::string getPropertyInfo(const std::string &propertyName) const{
463  return prop(propertyName).info;
464  }
465 
467 
469  virtual Any getPropertyValue(const std::string &propertyName) const;
470 
472  virtual std::string getPropertyToolTip(const std::string &propertyName) const{
473  return prop(propertyName).tooltip;
474  }
475 
477 
485  virtual int getPropertyVolatileness(const std::string &propertyName) const{
486  return prop(propertyName).volatileness;
487  }
488 
490 
491  static void register_configurable_type(const std::string &classname, Function<Configurable*> creator) ;
492 
494 
495  static std::vector<std::string> get_registered_configurables();
496 
498 
499  static Configurable *create_configurable(const std::string &classname) ;
500  };
501 
503 
504  #define REGISTER_CONFIGURABLE(NAME,CREATE) \
505  struct StaticConfigurableRegistrationFor_##NAME{ \
506  typedef StaticConfigurableRegistrationFor_##NAME This; \
507  static Configurable *create(){ \
508  CREATE; \
509  } \
510  StaticConfigurableRegistrationFor_##NAME(){ \
511  Configurable::register_configurable_type(#NAME, &This::create); \
512  } \
513  } staticConfigurableRegistrationFor_##NAME;
514 
515 
517 
518  #define REGISTER_CONFIGURABLE_DEFAULT(NAME) REGISTER_CONFIGURABLE(NAME,return new NAME)
519 
520  } // namespace utils
521 }
522 
523 
int volatileness
volatileness of a this property (0= no-volatileness, X=expected update every X msec)
Definition: Configurable.h:211
The General Function Template.
Definition: Function.h:284
std::map< std::string, Property > PropertyMap
by default internally use property list
Definition: Configurable.h:221
std::string childPrefix
Definition: Configurable.h:213
UncopiedInstance< Mutex > m_mutex
locks all accesses to property values
Definition: Configurable.h:248
undocument this line if you encounter any issues!
Definition: Any.h:37
static const std::vector< std::string > EMPTY_VEC
used as shortcut – just an empty vector of std::strings
Definition: Configurable.h:384
virtual std::string getPropertyInfo(const std::string &propertyName) const
get information of a properties valid values
Definition: Configurable.h:462
std::string type
property-type (menu, range,....);
Definition: Configurable.h:208
const std::string & getConfigurableID() const
returns the configurables static ID
Definition: Configurable.h:306
std::string info
property-information (depends on type)
Definition: Configurable.h:209
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
virtual std::string getPropertyType(const std::string &propertyName) const
get type of property
Definition: Configurable.h:443
Property()
Definition: Configurable.h:201
std::map< int, std::string > m_ordering
ordering of the properties
Definition: Configurable.h:229
std::map< const Configurable *, std::string > m_childConfigurables
internal list of child configurables
Definition: Configurable.h:232
virtual ~Configurable()
virtual destructor
Definition: Configurable.h:291
PropertyMap m_properties
list of all properties
Definition: Configurable.h:224
Configurable * configurable
corresponding Configurable
Definition: Configurable.h:206
This class provides the getter and setter methods of an internally set Configurable.
Definition: ConfigurableProxy.h:40
Configurable * m_elderConfigurable
internal pointer to elder configurable
Definition: Configurable.h:235
bool operator==(const std::string &name) const
for more efficient find
Definition: Configurable.h:215
std::string tooltip
property description, that is also used as tooltip
Definition: Configurable.h:212
void registerCallback(const Callback &cb)
add a callback for changed properties
Definition: Configurable.h:359
Function< void, const Property & > Callback
Function type for changed properties.
Definition: Configurable.h:356
std::string value
(optional) property-value this can be use to store current property value
Definition: Configurable.h:210
bool isOrderedFlagSet() const
returns whether the ordered flag is set
Definition: Configurable.h:311
std::string name
property-ID
Definition: Configurable.h:207
std::string m_ID
internal ID, that is used to provide global access to all instantiated configurables at runtime by gi...
Definition: Configurable.h:238
Interface for classes that can be configured from configuration-files and GUI-Components.
Definition: Configurable.h:194
static std::map< std::string, Configurable * > m_instances
static list of all instantiated Configurables
Definition: Configurable.h:241
virtual int getPropertyVolatileness(const std::string &propertyName) const
Returns whether this property may be changed internally.
Definition: Configurable.h:485
std::vector< std::string > m_deactivated
list of patterns for deactiavted properties
Definition: Configurable.h:244
virtual std::string getPropertyToolTip(const std::string &propertyName) const
returns the tooltip description for a given property
Definition: Configurable.h:472
Property(Configurable *parent, const std::string &name, const std::string &type, const std::string &info, const std::string &value, int volatileness, const std::string &tooltip)
Definition: Configurable.h:202
std::vector< Callback > callbacks
internally managed list of callbacks
Definition: Configurable.h:372
Represents a single property.
Definition: Configurable.h:200
bool m_isOrdered
whether to use property ordering
Definition: Configurable.h:227
Simple generic data type implementation that uses a string based data representation.
Definition: Any.h:109
Utility class for class instances that are created brand new on copy.
Definition: UncopiedInstance.h:72