Image Component Library (ICL)
|
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.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/MultiTypeMap.h> 00035 #include <ICLUtils/Exception.h> 00036 #include <ICLUtils/Function.h> 00037 #include <ICLUtils/StringUtils.h> 00038 #include <ICLQt/MouseEvent.h> 00039 00040 namespace icl{ 00041 namespace qt{ 00042 00043 00045 00047 class ICLQt_API DataStore : public utils::MultiTypeMap{ 00048 public: 00049 00051 struct KeyNotFoundException : public utils::ICLException{ 00052 KeyNotFoundException(const std::string &key):utils::ICLException("Key not found: " + key){} 00053 }; 00054 00056 struct UnassignableTypesException : public utils::ICLException{ 00057 UnassignableTypesException(const std::string &tSrc, const std::string &tDst): 00058 utils::ICLException("Unable to assign "+ tDst+ " = "+ tSrc){} 00059 }; 00060 00061 00063 class Data{ 00064 00066 DataArray *data; 00067 00069 inline Data(DataArray *data):data(data){} 00070 00072 ICLQt_API static void assign(void *src, const std::string &srcType, void *dst, 00073 const std::string &dstType) throw (UnassignableTypesException); 00074 00075 public: 00076 00078 struct Event{ 00079 Event(const std::string &msg="", void *data=0):message(msg),data(data){} 00080 Event(const std::string &msg, const utils::Function<void> &cb): message(msg),data(0),cb(cb){} 00081 Event(const std::string &msg, const utils::Function<void,const std::string&> &cb2): message(msg),data(0),cb2(cb2){} 00082 std::string message; 00083 void *data; 00084 utils::Function<void> cb; 00085 utils::Function<void,const std::string&> cb2; 00086 }; 00087 00088 friend class DataStore; 00089 00091 00092 template<class T> 00093 inline void operator=(const T &t) throw (UnassignableTypesException){ 00094 assign(const_cast<void*>(reinterpret_cast<const void*>(&t)), 00095 get_type_name<T>(),data->data,data->type); 00096 } 00097 00099 00101 template<class T> 00102 inline T as() const throw (UnassignableTypesException){ 00103 T t; 00104 assign(data->data,data->type,&t,get_type_name<T>()); 00105 return t; 00106 } 00107 00109 template<class T> 00110 operator T() const throw (UnassignableTypesException){ 00111 return as<T>(); 00112 } 00113 00115 const std::string &getTypeID() const { return data->type; } 00116 00117 00119 void render() throw (UnassignableTypesException){ 00120 *this = Event("render"); 00121 } 00122 00124 void link(void *data) throw (UnassignableTypesException){ 00125 *this = Event("link", data); 00126 } 00127 00129 void install(void *data){ 00130 *this = Event("install",data); 00131 } 00132 00134 ICLQt_API void install(utils::Function<void, const MouseEvent &> f); 00135 00136 // installs a global function (should be implicit) 00137 //void install(void (*f)(const MouseEvent &)){ 00138 // install(function(f)); 00139 //} 00140 00142 void registerCallback(const utils::Function<void> &cb){ 00143 *this = Event("register",cb); 00144 } 00145 00147 void registerCallback(const utils::Function<void,const std::string&> &cb){ 00148 *this = Event("register-complex",cb); 00149 } 00150 00152 void enable(){ 00153 *this = Event("enable"); 00154 } 00155 00157 void disable(){ 00158 *this = Event("disable"); 00159 } 00160 }; 00161 00162 00164 00203 Data operator[](const std::string &key) throw (KeyNotFoundException); 00204 00205 00207 00209 template<class T> 00210 std::vector<T> collect(const std::vector<std::string> &keys){ 00211 std::vector<T> v(keys.size()); 00212 for(unsigned int i=0;i<keys.size();++i) v[i] = operator[](keys[i]); 00213 return v; 00214 } 00215 00217 static void list_possible_assignments(const std::string &srcType, const std::string &dstType); 00218 00220 struct Assign{ 00221 std::string srcType,dstType,srcRTTI,dstRTTI; 00222 Assign(const std::string &srcType, const std::string &dstType, 00223 const std::string &srcRTTI, const std::string &dstRTTI): 00224 srcType(srcType),dstType(dstType),srcRTTI(srcRTTI),dstRTTI(dstRTTI){} 00225 00226 virtual bool operator()(void *src, void *dst){ return false; } 00227 }; 00228 00229 00230 private: 00231 00233 static void register_assignment_rule(Assign *assign); 00234 00235 public: 00236 00238 00240 template<class SRC, class DST> 00241 static inline void register_assignment_rule(const std::string &srcTypeName, 00242 const std::string &dstTypeName, 00243 utils::Function<void,const SRC&,DST&> assign){ 00244 struct TypeDependentAssign : public Assign{ 00245 utils::Function<void,const SRC&,DST&> assign; 00246 TypeDependentAssign(const std::string &srcTypeName, const std::string &dstTypeName, 00247 utils::Function<void,const SRC&,DST&> assign): 00248 Assign(srcTypeName, dstType, get_type_name<SRC>(), get_type_name<DST>()),assign(assign){} 00249 00250 virtual bool operator()(void *src, void *dst){ 00251 assign( *(const SRC*)src, *(DST*)dst ); 00252 return true; 00253 } 00254 }; 00255 register_assignment_rule(new TypeDependentAssign(srcTypeName,dstTypeName,assign)); 00256 } 00257 00259 00261 template<class SRC, class DST> 00262 static inline void register_trivial_assignment_rule(const std::string &srcTypeName, 00263 const std::string &dstTypeName){ 00264 struct TypeDependentTrivialAssign : public Assign{ 00265 TypeDependentTrivialAssign(const std::string &srcTypeName, const std::string &dstTypeName): 00266 Assign(srcTypeName, dstTypeName, get_type_name<SRC>(), get_type_name<DST>()){} 00267 00268 virtual bool operator()(void *src, void *dst){ 00269 *(DST*)dst = *(const SRC*)src; 00270 return true; 00271 } 00272 }; 00273 register_assignment_rule(new TypeDependentTrivialAssign(srcTypeName,dstTypeName)); 00274 } 00275 00276 }; 00277 } // namespace qt 00278 } 00279 00280