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.GPL  **
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 <vector>
00034 #include <string>
00035 #include <algorithm>
00036 #include <map>
00037 
00038 #include <ICLUtils/Exception.h>
00039 #include <ICLUtils/SmartPtr.h>
00040 #include <ICLUtils/Any.h>
00041 #include <ICLUtils/Function.h>
00042 #include <ICLUtils/UncopiedInstance.h>
00043 #include <ICLUtils/Mutex.h>
00044 
00045 namespace icl{
00046   namespace utils{
00047   
00049 
00193     class Configurable{
00194       public:
00196       struct Property{
00197         Property():configurable(0),volatileness(0){}
00198         Property(Configurable *parent,
00199                  const std::string &name, const std::string &type, const std::string &info, const std::string &value,
00200                  int volatileness, const std::string &tooltip):configurable(parent),name(name),type(type),info(info),value(value),
00201           volatileness(volatileness), tooltip(tooltip){}
00202         Configurable *configurable; 
00203         std::string name;  
00204         std::string type;  
00205         std::string info;  
00206         std::string value; 
00207         int volatileness;  
00208         std::string tooltip; 
00209         std::string childPrefix;
00211         bool operator==(const std::string &name) const { return this->name == name; }
00212       };
00213   
00214       private:
00215       
00217       typedef std::map<std::string,Property> PropertyMap;
00218   
00220       PropertyMap m_properties;
00221       
00223       std::vector<Configurable*> m_childConfigurables;
00224 
00226       Configurable* m_elderConfigurable;
00227       
00229       std::string m_ID;
00230   
00232       static std::map<std::string,Configurable*> m_instances;
00233       
00235       std::vector<std::string> m_deactivated;
00236   
00238 
00239       mutable UncopiedInstance<Mutex> m_mutex;
00240 
00241       protected:
00242       
00244 
00249       void addProperty(const std::string &name, const std::string &type, 
00250                        const std::string &info, const Any &value=Any(), 
00251                        const int volatileness=0, const std::string &tooltip=std::string()) throw (ICLException);
00252       
00254 
00259       void addChildConfigurable(Configurable *configurable, const std::string &childPrefix="");
00260       
00262 
00263       Property &prop(const std::string &propertyName) throw (ICLException);
00264   
00266 
00267       const Property &prop(const std::string &propertyName) const throw (ICLException);
00268       
00270 
00274       Configurable(const std::string &ID="") throw (ICLException);
00275       
00276       public:
00277       
00279       ~Configurable(){}
00280   
00282 
00283       Configurable(const Configurable &other);
00284       
00286 
00287       Configurable &operator=(const Configurable &other);
00288   
00290 
00291       void setConfigurableID(const std::string &ID) throw (ICLException);
00292       
00294       inline const std::string &getConfigurableID() const{
00295         return m_ID;
00296       }
00297   
00299 
00311       void deactivateProperty(const std::string &pattern);
00312   
00314 
00316       void deleteDeactivationPattern(const std::string &pattern);
00317       
00319       std::vector<std::string> getPropertyListWithoutDeactivated() const;
00320   
00322 
00328       virtual void adaptProperty(const std::string &name, const std::string &newType, 
00329                                  const std::string &newInfo, const std::string &newToolTip) throw (ICLException);
00330   
00331   
00333 
00335       static std::string create_default_ID(const std::string &prefix);
00336   
00337   
00339       typedef Function<void,const Property&> Callback;
00340       
00342       void registerCallback(const Callback &cb){
00343         callbacks.push_back(cb);
00344       }
00345       
00347       void removedCallback(const Callback &cb);
00348   
00350 
00351       void syncChangesTo(Configurable *others, int num=1);
00352       
00353       protected:
00355       std::vector<Callback> callbacks;
00356   
00358       void call_callbacks(const std::string &propertyName) const;
00359   
00360       public:
00361   
00363       static const std::vector<std::string> EMPTY_VEC;
00364       
00366 
00367       static Configurable* get(const std::string &id);
00368       
00369       
00371 
00375       virtual void setPropertyValue(const std::string &propertyName, const Any &value) throw (ICLException);
00376       
00378 
00380       virtual std::vector<std::string> getPropertyList() const;
00381        
00383 
00385       virtual bool supportsProperty(const std::string &propertyName) const;
00386        
00388 
00391       virtual void saveProperties(const std::string &filename, const std::vector<std::string> &propertiesToSkip=EMPTY_VEC) const;
00392   
00394 
00395       virtual void loadProperties(const std::string &filename,const std::vector<std::string> &propertiesToSkip=EMPTY_VEC);
00396   
00398 
00422       virtual std::string getPropertyType(const std::string &propertyName) const{
00423         return prop(propertyName).type;
00424       }
00425        
00427 
00441       virtual std::string getPropertyInfo(const std::string &propertyName) const{
00442         return prop(propertyName).info;
00443       }
00444   
00446 
00448       virtual Any getPropertyValue(const std::string &propertyName) const;
00449       
00451       virtual std::string getPropertyToolTip(const std::string &propertyName) const{
00452         return prop(propertyName).tooltip;
00453       }
00454   
00456 
00464       virtual int getPropertyVolatileness(const std::string &propertyName) const{
00465         return prop(propertyName).volatileness;
00466       }
00467       
00469 
00470       static void register_configurable_type(const std::string &classname, Function<Configurable*> creator) throw (ICLException);
00471       
00473 
00474       static std::vector<std::string> get_registered_configurables();
00475       
00477 
00478       static Configurable *create_configurable(const std::string &classname) throw (ICLException);
00479     };
00480   
00482 
00483   #define REGISTER_CONFIGURABLE(NAME,CREATE)                              \
00484     struct StaticConfigurableRegistrationFor_##NAME{                      \
00485       typedef StaticConfigurableRegistrationFor_##NAME This;              \
00486       static Configurable *create(){                                      \
00487         CREATE;                                                           \
00488       }                                                                   \
00489       StaticConfigurableRegistrationFor_##NAME(){                         \
00490         Configurable::register_configurable_type(#NAME, &This::create);   \
00491       }                                                                   \
00492     } staticConfigurableRegistrationFor_##NAME;
00493   
00494     
00496 
00497   #define REGISTER_CONFIGURABLE_DEFAULT(NAME) REGISTER_CONFIGURABLE(NAME,return new NAME)
00498     
00499   } // namespace utils
00500 }
00501 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines