Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DataStore.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   : ICLQt/src/ICLQt/DataStore.h                            **
00010 ** Module : ICLQt                                                  **
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 <ICLUtils/MultiTypeMap.h>
00034 #include <ICLUtils/Exception.h>
00035 #include <ICLUtils/Function.h>
00036 #include <ICLUtils/StringUtils.h>
00037 #include <ICLQt/MouseEvent.h>
00038 
00039 namespace icl{
00040   namespace qt{
00041   
00042    
00044 
00046     class DataStore : public utils::MultiTypeMap{
00047       public:
00048   
00050       struct KeyNotFoundException : public utils::ICLException{
00051         KeyNotFoundException(const std::string &key):utils::ICLException("Key not found: " + key){}
00052       };
00053       
00055       struct UnassignableTypesException : public utils::ICLException{
00056         UnassignableTypesException(const std::string &tSrc, const std::string &tDst):
00057         utils::ICLException("Unable to assign "+ tDst+ " = "+ tSrc){}
00058       };
00059       
00060       
00062       class Data{
00063         
00065         DataArray *data;
00066         
00068         inline Data(DataArray *data):data(data){}
00069         
00071         static void assign(void *src, const std::string &srcType, void *dst, 
00072                            const std::string &dstType) throw (UnassignableTypesException); 
00073   
00074         public:
00075         
00077         struct Event{
00078           Event(const std::string &msg="", void *data=0):message(msg),data(data){}
00079         Event(const std::string &msg, const utils::Function<void> &cb): message(msg),data(0),cb(cb){}
00080         Event(const std::string &msg, const utils::Function<void,const std::string&> &cb2): message(msg),data(0),cb2(cb2){}
00081           std::string message;
00082           void *data;
00083           utils::Function<void> cb;
00084           utils::Function<void,const std::string&> cb2;
00085         };
00086         
00087         friend class DataStore;
00088         
00090 
00091         template<class T>
00092         inline void operator=(const T &t) throw (UnassignableTypesException){
00093           assign(const_cast<void*>(reinterpret_cast<const void*>(&t)),
00094                  get_type_name<T>(),data->data,data->type);
00095         }
00096   
00098 
00100         template<class T>
00101         inline T as() const throw (UnassignableTypesException){
00102           T t;
00103           assign(data->data,data->type,&t,get_type_name<T>());
00104           return t;
00105         }
00106   
00108         template<class T>
00109         operator T() const throw (UnassignableTypesException){
00110           return as<T>();
00111         }
00112   
00114         const std::string &getTypeID() const { return data->type; }
00115         
00116   
00118         void render() throw (UnassignableTypesException){ 
00119           *this = Event("render"); 
00120         }
00121   
00123         void link(void *data) throw (UnassignableTypesException){ 
00124           *this = Event("link", data); 
00125         }
00126         
00128         void install(void *data){
00129           *this  = Event("install",data);
00130         }
00131         
00133         void install(utils::Function<void,const MouseEvent &> f);
00134   
00135         // installs a global function (should be implicit)
00136         //void install(void (*f)(const MouseEvent &)){
00137         //  install(function(f));
00138         //}
00139         
00141         void registerCallback(const utils::Function<void> &cb){
00142           *this = Event("register",cb);
00143         }
00144   
00146         void registerCallback(const utils::Function<void,const std::string&> &cb){
00147           *this = Event("register-complex",cb);
00148         }
00149         
00151         void enable(){
00152           *this = Event("enable");
00153         }
00154         
00156         void disable(){
00157           *this = Event("disable");
00158         }
00159       };
00160       
00161   
00163 
00202       Data operator[](const std::string &key) throw (KeyNotFoundException);
00203       
00204       
00206 
00208       template<class T>
00209       std::vector<T> collect(const std::vector<std::string> &keys){
00210         std::vector<T> v(keys.size());
00211         for(unsigned int i=0;i<keys.size();++i) v[i] = operator[](keys[i]);
00212         return v;
00213       }
00214   
00216       static void list_possible_assignments(const std::string &srcType, const std::string &dstType);
00217       
00219       struct Assign{
00220         std::string srcType,dstType,srcRTTI,dstRTTI;
00221         Assign(const std::string &srcType, const std::string &dstType, 
00222                const std::string &srcRTTI, const std::string &dstRTTI):
00223         srcType(srcType),dstType(dstType),srcRTTI(srcRTTI),dstRTTI(dstRTTI){}
00224 
00225         virtual bool operator()(void *src, void *dst){ return false; }
00226       };
00227   
00228 
00229       private:
00230       
00232       static void register_assignment_rule(Assign *assign);
00233       
00234       public:
00235       
00237 
00239       template<class SRC, class DST>
00240       static inline void register_assignment_rule(const std::string &srcTypeName,
00241                                                   const std::string &dstTypeName,
00242                                                   utils::Function<void,const SRC&,DST&> assign){
00243         struct TypeDependentAssign : public Assign{
00244           utils::Function<void,const SRC&,DST&> assign;
00245           TypeDependentAssign(const std::string &srcTypeName, const std::string &dstTypeName,
00246                               utils::Function<void,const SRC&,DST&> assign):
00247           Assign(srcTypeName, dstType, get_type_name<SRC>(), get_type_name<DST>()),assign(assign){}
00248           
00249           virtual bool operator()(void *src, void *dst){ 
00250             assign( *(const SRC*)src, *(DST*)dst );
00251             return true;
00252           }
00253         };
00254         register_assignment_rule(new TypeDependentAssign(srcTypeName,dstTypeName,assign));
00255       }
00256 
00258 
00260       template<class SRC, class DST>
00261       static inline void register_trivial_assignment_rule(const std::string &srcTypeName,
00262                                                           const std::string &dstTypeName){
00263         struct TypeDependentTrivialAssign : public Assign{
00264           TypeDependentTrivialAssign(const std::string &srcTypeName, const std::string &dstTypeName):
00265           Assign(srcTypeName, dstTypeName, get_type_name<SRC>(),  get_type_name<DST>()){}
00266           
00267           virtual bool operator()(void *src, void *dst){ 
00268             *(DST*)dst = *(const SRC*)src;
00269             return true;
00270           }
00271         };
00272         register_assignment_rule(new TypeDependentTrivialAssign(srcTypeName,dstTypeName));
00273       }
00274       
00275     };
00276   } // namespace qt
00277 }
00278 
00279 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines