Image Component Library (ICL)
DataStore.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 : ICLQt/src/ICLQt/DataStore.h **
10 ** Module : ICLQt **
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/MultiTypeMap.h>
35 #include <ICLUtils/Exception.h>
36 #include <ICLUtils/Function.h>
37 #include <ICLUtils/StringUtils.h>
38 #include <ICLQt/MouseEvent.h>
39 
40 namespace icl{
41  namespace qt{
42 
43 
45 
48  public:
49 
52  KeyNotFoundException(const std::string &key):utils::ICLException("Key not found: " + key){}
53  };
54 
57  UnassignableTypesException(const std::string &tSrc, const std::string &tDst):
58  utils::ICLException("Unable to assign "+ tDst+ " = "+ tSrc){}
59  };
60 
61 
63  class Data{
64 
67 
69  inline Data(DataArray *data):data(data){}
70 
72  ICLQt_API static void assign(void *src, const std::string &srcType, void *dst,
73  const std::string &dstType) ;
74 
75  public:
76 
78  struct Event{
79  Event(const std::string &msg="", void *data=0):message(msg),data(data){}
80  Event(const std::string &msg, const utils::Function<void> &cb): message(msg),data(0),cb(cb){}
81  Event(const std::string &msg, const utils::Function<void,const std::string&> &cb2): message(msg),data(0),cb2(cb2){}
82  std::string message;
83  void *data;
86  };
87 
88  friend class DataStore;
89 
91 
92  template<class T>
93  inline void operator=(const T &t) {
94  assign(const_cast<void*>(reinterpret_cast<const void*>(&t)),
95  get_type_name<T>(),data->data,data->type);
96  }
97 
99 
101  template<class T>
102  inline T as() const {
103  T t;
104  assign(data->data,data->type,&t,get_type_name<T>());
105  return t;
106  }
107 
109  template<class T>
110  operator T() const {
111  return as<T>();
112  }
113 
115  const std::string &getTypeID() const { return data->type; }
116 
117 
119  void render() {
120  *this = Event("render");
121  }
122 
124  void link(void *data) {
125  *this = Event("link", data);
126  }
127 
129  void install(void *data){
130  *this = Event("install",data);
131  }
132 
135 
136  // installs a global function (should be implicit)
137  //void install(void (*f)(const MouseEvent &)){
138  // install(function(f));
139  //}
140 
143  *this = Event("register",cb);
144  }
145 
148  *this = Event("register-complex",cb);
149  }
150 
152  void enable(){
153  *this = Event("enable");
154  }
155 
157  void disable(){
158  *this = Event("disable");
159  }
160  };
161 
162 
164 
203  Data operator[](const std::string &key) ;
204 
205 
207 
209  template<class T>
210  std::vector<T> collect(const std::vector<std::string> &keys){
211  std::vector<T> v(keys.size());
212  for(unsigned int i=0;i<keys.size();++i) v[i] = operator[](keys[i]);
213  return v;
214  }
215 
217  static void list_possible_assignments(const std::string &srcType, const std::string &dstType);
218 
220  struct Assign{
221  std::string srcType,dstType,srcRTTI,dstRTTI;
222  Assign(const std::string &srcType, const std::string &dstType,
223  const std::string &srcRTTI, const std::string &dstRTTI):
224  srcType(srcType),dstType(dstType),srcRTTI(srcRTTI),dstRTTI(dstRTTI){}
225 
226  virtual bool operator()(void *src, void *dst){ return false; }
227  };
228 
229 
230  private:
231 
233  static void register_assignment_rule(Assign *assign);
234 
235  public:
236 
238 
240  template<class SRC, class DST>
241  static inline void register_assignment_rule(const std::string &srcTypeName,
242  const std::string &dstTypeName,
244  struct TypeDependentAssign : public Assign{
246  TypeDependentAssign(const std::string &srcTypeName, const std::string &dstTypeName,
248  Assign(srcTypeName, dstType, get_type_name<SRC>(), get_type_name<DST>()),assign(assign){}
249 
250  virtual bool operator()(void *src, void *dst){
251  assign( *(const SRC*)src, *(DST*)dst );
252  return true;
253  }
254  };
255  register_assignment_rule(new TypeDependentAssign(srcTypeName,dstTypeName,assign));
256  }
257 
259 
261  template<class SRC, class DST>
262  static inline void register_trivial_assignment_rule(const std::string &srcTypeName,
263  const std::string &dstTypeName){
264  struct TypeDependentTrivialAssign : public Assign{
265  TypeDependentTrivialAssign(const std::string &srcTypeName, const std::string &dstTypeName):
266  Assign(srcTypeName, dstTypeName, get_type_name<SRC>(), get_type_name<DST>()){}
267 
268  virtual bool operator()(void *src, void *dst){
269  *(DST*)dst = *(const SRC*)src;
270  return true;
271  }
272  };
273  register_assignment_rule(new TypeDependentTrivialAssign(srcTypeName,dstTypeName));
274  }
275 
276  };
277  } // namespace qt
278 }
279 
280 
Extension of the associative container MultiTypeMap.
Definition: DataStore.h:47
utils::Function< void > cb
Definition: DataStore.h:84
void operator=(const T &t)
Trys to assign an instance of T to this Data-Element.
Definition: DataStore.h:93
std::string message
Definition: DataStore.h:82
UnassignableTypesException(const std::string &tSrc, const std::string &tDst)
Definition: DataStore.h:57
undocument this line if you encounter any issues!
Definition: Any.h:37
void link(void *data)
links DrawWidget3D and GLCallback
Definition: DataStore.h:124
void registerCallback(const utils::Function< void, const std::string & > &cb)
register simple callback type
Definition: DataStore.h:147
Abstract and associative Data Container for Data of different types.
Definition: MultiTypeMap.h:67
Internal Exception type thrown if Data::operator= is called for incompatible values.
Definition: DataStore.h:56
std::string srcType
Definition: DataStore.h:221
virtual bool operator()(void *src, void *dst)
Definition: DataStore.h:226
std::vector< T > collect(const std::vector< std::string > &keys)
convenience function that allows collecting data from different source entries
Definition: DataStore.h:210
void registerCallback(const utils::Function< void > &cb)
register simple callback type
Definition: DataStore.h:142
internally used assignment structure
Definition: DataStore.h:220
Event(const std::string &msg, const utils::Function< void > &cb)
Definition: DataStore.h:80
static void register_assignment_rule(const std::string &srcTypeName, const std::string &dstTypeName, utils::Function< void, const SRC &, DST & > assign)
registers a new assignment rule to the DataStore class
Definition: DataStore.h:241
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
internally used data handling structure
Definition: MultiTypeMap.h:241
Internally used Data- Structure.
Definition: DataStore.h:78
void * data
Definition: DataStore.h:83
DataArray * data
internally reference DataStore entry
Definition: DataStore.h:66
static void register_trivial_assignment_rule(const std::string &srcTypeName, const std::string &dstTypeName)
registers trivial assignment rule to the DataStore class
Definition: DataStore.h:262
T as() const
Trys to convert a Data element into a (by template parameter) given type.
Definition: DataStore.h:102
const std::string & getTypeID() const
returns the internal type ID (obtained by C++'s RTTI)
Definition: DataStore.h:115
#define ICLQt_API
Definition: CompatMacros.h:178
Arbitrary Data encapsulation type.
Definition: DataStore.h:63
Base class for Exception handling in the ICL.
Definition: Exception.h:42
Event(const std::string &msg, const utils::Function< void, const std::string & > &cb2)
Definition: DataStore.h:81
Internal Exception type thrown if operator[] is given an unknown index string.
Definition: DataStore.h:51
void install(void *data)
data must be of type MouseHandler*
Definition: DataStore.h:129
Event(const std::string &msg="", void *data=0)
Definition: DataStore.h:79
Data(DataArray *data)
Constructor (private->only the parent DataStore is allowed to contruct Data's)
Definition: DataStore.h:69
Assign(const std::string &srcType, const std::string &dstType, const std::string &srcRTTI, const std::string &dstRTTI)
Definition: DataStore.h:222
void disable()
possible for all handle types
Definition: DataStore.h:157
void enable()
possible for all handle-types
Definition: DataStore.h:152
utils::Function< void, const std::string & > cb2
Definition: DataStore.h:85
KeyNotFoundException(const std::string &key)
Definition: DataStore.h:52
void render()
Definition: DataStore.h:119