Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Configurable.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/Configurable.h                   **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Exception.h>
00035 #include <ICLUtils/SmartPtr.h>
00036 #include <ICLUtils/Any.h>
00037 #include <ICLUtils/Function.h>
00038 #include <ICLUtils/UncopiedInstance.h>
00039 #include <ICLUtils/Mutex.h>
00040 
00041 #include <vector>
00042 #include <string>
00043 #include <algorithm>
00044 #include <map>
00045 
00046 namespace icl{
00047   namespace utils{
00048   
00050 
00194     class ICLUtils_API Configurable{
00195       public:
00197       struct Property{
00198         Property():configurable(0),volatileness(0){}
00199         Property(Configurable *parent,
00200                  const std::string &name, const std::string &type, const std::string &info, const std::string &value,
00201                  int volatileness, const std::string &tooltip):configurable(parent),name(name),type(type),info(info),value(value),
00202           volatileness(volatileness), tooltip(tooltip){}
00203         Configurable *configurable; 
00204         std::string name;  
00205         std::string type;  
00206         std::string info;  
00207         std::string value; 
00208         int volatileness;  
00209         std::string tooltip; 
00210         std::string childPrefix;
00212         bool operator==(const std::string &name) const { return this->name == name; }
00213       };
00214   
00215       private:
00216       
00218       typedef std::map<std::string,Property> PropertyMap;
00219   
00221       PropertyMap m_properties;
00222 
00224       bool m_isOrdered;
00226       std::map<int,std::string> m_ordering;
00227 
00229       std::map<const Configurable*, std::string> m_childConfigurables;
00230 
00232       Configurable* m_elderConfigurable;
00233       
00235       std::string m_ID;
00236   
00238       static std::map<std::string,Configurable*> m_instances;
00239       
00241       std::vector<std::string> m_deactivated;
00242   
00244 
00245       mutable UncopiedInstance<Mutex> m_mutex;
00246 
00247       protected:
00248       
00250 
00255       void addProperty(const std::string &name, const std::string &type, 
00256                        const std::string &info, const Any &value=Any(), 
00257                        const int volatileness=0, const std::string &tooltip=std::string()) throw (ICLException);
00258       
00260 
00265       void addChildConfigurable(Configurable *configurable, const std::string &childPrefix="");
00266       
00268       void removeChildConfigurable(Configurable *configurable);
00269       
00271 
00272       Property &prop(const std::string &propertyName) throw (ICLException);
00273   
00275 
00276       const Property &prop(const std::string &propertyName) const throw (ICLException);
00277       
00279 
00283       Configurable(const std::string &ID="", bool ordered=true) throw (ICLException);
00284       
00285       public:
00286       
00288       virtual ~Configurable(){}
00289   
00291 
00292       Configurable(const Configurable &other);
00293       
00295 
00296       Configurable &operator=(const Configurable &other);
00297   
00299 
00300       void setConfigurableID(const std::string &ID) throw (ICLException);
00301       
00303       inline const std::string &getConfigurableID() const{
00304         return m_ID;
00305       }
00306   
00308 
00320       void deactivateProperty(const std::string &pattern);
00321   
00323 
00325       void deleteDeactivationPattern(const std::string &pattern);
00326       
00328       std::vector<std::string> getPropertyListWithoutDeactivated() const;
00329   
00331 
00337       virtual void adaptProperty(const std::string &name, const std::string &newType, 
00338                                  const std::string &newInfo, const std::string &newToolTip) throw (ICLException);
00339   
00340   
00342 
00344       static std::string create_default_ID(const std::string &prefix);
00345   
00346   
00348       typedef Function<void,const Property&> Callback;
00349       
00351       void registerCallback(const Callback &cb){
00352         callbacks.push_back(cb);
00353       }
00354       
00356       void removedCallback(const Callback &cb);
00357   
00359 
00360       void syncChangesTo(Configurable *others, int num=1);
00361       
00362       protected:
00364       std::vector<Callback> callbacks;
00365   
00367 
00371       void call_callbacks(const std::string &propertyName,const Configurable* caller) const;
00372   
00373       public:
00374   
00376       static const std::vector<std::string> EMPTY_VEC;
00377       
00379 
00380       static Configurable* get(const std::string &id);
00381       
00382       
00384 
00388       virtual void setPropertyValue(const std::string &propertyName, const Any &value) throw (ICLException);
00389       
00391 
00393       virtual std::vector<std::string> getPropertyList() const;
00394        
00396 
00398       virtual bool supportsProperty(const std::string &propertyName) const;
00399        
00401 
00404       virtual void saveProperties(const std::string &filename, const std::vector<std::string> &propertiesToSkip=EMPTY_VEC) const;
00405   
00407 
00408       virtual void loadProperties(const std::string &filename,const std::vector<std::string> &propertiesToSkip=EMPTY_VEC);
00409   
00411 
00435       virtual std::string getPropertyType(const std::string &propertyName) const{
00436         return prop(propertyName).type;
00437       }
00438        
00440 
00454       virtual std::string getPropertyInfo(const std::string &propertyName) const{
00455         return prop(propertyName).info;
00456       }
00457   
00459 
00461       virtual Any getPropertyValue(const std::string &propertyName) const;
00462       
00464       virtual std::string getPropertyToolTip(const std::string &propertyName) const{
00465         return prop(propertyName).tooltip;
00466       }
00467   
00469 
00477       virtual int getPropertyVolatileness(const std::string &propertyName) const{
00478         return prop(propertyName).volatileness;
00479       }
00480       
00482 
00483       static void register_configurable_type(const std::string &classname, Function<Configurable*> creator) throw (ICLException);
00484       
00486 
00487       static std::vector<std::string> get_registered_configurables();
00488       
00490 
00491       static Configurable *create_configurable(const std::string &classname) throw (ICLException);
00492     };
00493   
00495 
00496   #define REGISTER_CONFIGURABLE(NAME,CREATE)                              \
00497     struct StaticConfigurableRegistrationFor_##NAME{ \
00498       typedef StaticConfigurableRegistrationFor_##NAME This;              \
00499       static Configurable *create(){                                      \
00500         CREATE;                                                           \
00501       }                                                                   \
00502       StaticConfigurableRegistrationFor_##NAME(){                         \
00503         Configurable::register_configurable_type(#NAME, &This::create);   \
00504       }                                                                   \
00505     } staticConfigurableRegistrationFor_##NAME;
00506   
00507     
00509 
00510   #define REGISTER_CONFIGURABLE_DEFAULT(NAME) REGISTER_CONFIGURABLE(NAME,return new NAME)
00511     
00512   } // namespace utils
00513 }
00514 
00515 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines