Image Component Library (ICL)
MultiTypeMap.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 : ICLUtils/src/ICLUtils/MultiTypeMap.h **
10 ** Module : ICLUtils **
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/Mutex.h>
35 #include <ICLUtils/SmartPtr.h>
36 
37 #include <string>
38 #include <map>
39 #include <typeinfo>
40 #include <cstdio>
41 #include <vector>
42 
43 namespace icl{
44  namespace utils{
45 
47 
68  public:
70  MultiTypeMap();
71 
73  ~MultiTypeMap();
74 
76  template<class T>
77  static inline const std::string &get_type_name(){
78  static std::string NAME = typeid(T).name();
79  return NAME;
80  }
81 
83 
87  template<class T>
88  inline T *allocArray(const std::string &id,unsigned int n){
89  if(!n){
90  ERROR_LOG("unable to create an array of size 0 for id " << id << "!");
91  return 0;
92  }
93  if(contains(id)){
94  ERROR_LOG("id " << id << "is already defined");
95  return 0;
96  }
97  DataArray &da = (*m_oDataMapPtr)[id];
98  da.data = new T[n];
99  da.len = n;
100  da.type = get_type_name<T>();
101  da.release_func = DataArray::release_data_array<T>;
102  return reinterpret_cast<T*>(da.data);
103  }
104 
106 
110  template<class T>
111  inline T &allocValue(const std::string &id, const T &val=T()){
112  static T _NULL = T();
113  if(contains(id)){
114  ERROR_LOG("id " << id << "is already defined");
115  return _NULL;
116  }
117  DataArray &da = (*m_oDataMapPtr)[id];
118  da.data = new T(val);
119  da.len = 0; // indicates a single value !
120  da.type = get_type_name<T>();
121 
122  da.release_func = DataArray::release_data_array<T>;
123  return *(reinterpret_cast<T*>(da.data));
124  }
125 
127 
128  template<class T>
129  inline void release(const std::string &id){
130  if(!contains(id)){
131  ERROR_LOG("id "<< id << " not found \n");
132  return;
133  }
134  DataArray &da = (*m_oDataMapPtr)[id];
135  if(da.type != get_type_name<T>()){
136  ERROR_LOG("unable to cast "<< id << " to a given type "<< get_type_name<T>() <<"\n");
137  ERROR_LOG("type is " << da.type << "\n");
138  return;
139  }
140  da.release_func(&da);
141  m_oDataMapPtr->erase(m_oDataMapPtr->find(id));
142  }
143 
145 
149  template<class T>
150  inline T* getArray(const std::string &id, int *lenDst=0){
151  if(!contains(id)){
152  ERROR_LOG("id "<< id << " not found \n");
153  return 0;
154  }
155 
156  DataArray &da = (*m_oDataMapPtr)[id];
157 
158  if(da.type != get_type_name<T>()){
159  ERROR_LOG("unable to cast "<< id << " to a given type "<< get_type_name<T>() <<"\n");
160  return 0;
161  }
162  if(!da.len){
163  ERROR_LOG("unable to access entry " << id << " as array, because it is a value!");
164  return 0;
165  }
166  if(lenDst) *lenDst = da.len;
167  return reinterpret_cast<T*>(da.data);
168  }
169 
171 
175  template<class T>
176  inline T &getValue(const std::string &id, bool checkType=true){
177  static T _NULL;
178  if(!contains(id)){
179  ERROR_LOG("id "<< id << " not found \n");
180  return _NULL;
181  }
182 
183  // orig
184  DataArray &da = (*m_oDataMapPtr)[id];
185  //DEBUG_LOG("type of da is " << da.type);
186  if(checkType && (da.type != get_type_name<T>())){
187  ERROR_LOG("unable to cast "<< id << " to a given type "<< get_type_name<T>() <<"\n");
188  ERROR_LOG("type is " << da.type << "\n");
189  return _NULL;
190  }
191  if(da.len){
192  ERROR_LOG("unable to access entry " << id << " as value, because it is an array!");
193  return _NULL;
194  }
195  return *reinterpret_cast<T*>(da.data);
196  }
197 
198  template<class T>
199  inline const T &getValue(const std::string &id, bool checkType=true) const{
200  return const_cast<MultiTypeMap*>(this)->getValue<T>(id,checkType);
201  }
202 
203 
204 
206 
209  const std::string &getType(const std::string &id) const;
210 
212 
214  template<class T>
215  inline bool checkType(const std::string &id) const{
216  return check_type_internal(id,get_type_name<T>());
217  }
218 
220 
223  bool isArray(const std::string &id) const;
224 
226 
228  bool contains(const std::string &id) const;
229 
230 
231  // internally locks the datastore
232  inline void lock() const { m_oMutexPtr->lock(); }
233 
235  inline void unlock() const { m_oMutexPtr->unlock(); }
236  protected:
237 
238  bool check_type_internal(const std::string &id, const std::string &typestr) const;
239 
241  struct DataArray{
242 
244 
246  template<class T>
247  static void release_data_array(DataArray *da){
248  ICLASSERT_RETURN(da->type == get_type_name<T>());
249  if(da->len) delete [] reinterpret_cast<T*>(da->data);
250  else delete reinterpret_cast<T*>(da->data);
251  }
253  DataArray(void *data=0, int len=0):data(data),len(len),type(""),release_func(0){}
254  void *data; //<! identified using the type string
255  int len; //<! length of the data array or 0 if it was created using () instead of []
256  std::string type; //<! created using RTTI
257  void (*release_func)(DataArray*); //<! data release function called by the parent MultiTypeMap object
258  };
259 
260  public:
262  void listContents() const;
263 
264  // removes all data from this data store
265  void clear();
266 
267 
269  struct Entry{
270  Entry(){}
271  Entry(const std::string &key,const std::string &type, int len):
272  key(key),type(type),len(len){}
273  std::string key;
274  std::string type;
275  int len;
276  };
277 
279  std::vector<Entry> getEntryList() const;
280 
281  protected:
282 
284  typedef std::map<std::string,DataArray> DataMap;
285 
288 
291 
294 
297  };
298  } // namespace utils
299 }
300 
301 
Entry()
Definition: MultiTypeMap.h:270
undocument this line if you encounter any issues!
Definition: Any.h:37
std::string key
Definition: MultiTypeMap.h:273
Abstract and associative Data Container for Data of different types.
Definition: MultiTypeMap.h:67
SmartDataMapPtr m_oDataMapPtr
Smart-Pointer to the underlying data (allows shallow copies)
Definition: MultiTypeMap.h:293
void release(const std::string &id)
release the data element that is associated with the given id
Definition: MultiTypeMap.h:129
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
void lock() const
Definition: MultiTypeMap.h:232
Entry(const std::string &key, const std::string &type, int len)
Definition: MultiTypeMap.h:271
void(* release_func)(DataArray *)
Definition: MultiTypeMap.h:257
std::string type
Definition: MultiTypeMap.h:274
DataArray(void *data=0, int len=0)
Create an empty DataArray object.
Definition: MultiTypeMap.h:253
T * getArray(const std::string &id, int *lenDst=0)
get a T* that is associated with the given id
Definition: MultiTypeMap.h:150
bool checkType(const std::string &id) const
checks if the type-id associated with the template parameter T is compatible to the entry for id
Definition: MultiTypeMap.h:215
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
static const std::string & get_type_name()
internally used wrapper function for RTTI
Definition: MultiTypeMap.h:77
const T & getValue(const std::string &id, bool checkType=true) const
Definition: MultiTypeMap.h:199
internally used data handling structure
Definition: MultiTypeMap.h:241
std::string type
Definition: MultiTypeMap.h:256
T & getValue(const std::string &id, bool checkType=true)
get a T reference that is associated with the given id
Definition: MultiTypeMap.h:176
#define ERROR_LOG(x)
Definition: Macros.h:111
entry struct used in getEntryList function
Definition: MultiTypeMap.h:269
std::map< std::string, DataArray > DataMap
internal definition
Definition: MultiTypeMap.h:284
#define ICLASSERT_RETURN(X)
Definition: Macros.h:141
static void release_data_array(DataArray *da)
delete function, given to the data Array after construction to delete its own data
Definition: MultiTypeMap.h:247
SmartPtr< Mutex > SmartMutexPtr
internal definition
Definition: MultiTypeMap.h:290
void * data
Definition: MultiTypeMap.h:254
int len
Definition: MultiTypeMap.h:275
int len
Definition: MultiTypeMap.h:255
T * allocArray(const std::string &id, unsigned int n)
Allocates a new memory block (new T[n]) for the given id string.
Definition: MultiTypeMap.h:88
SmartMutexPtr m_oMutexPtr
mutex to handle syncronous calls
Definition: MultiTypeMap.h:296
SmartPtr< DataMap > SmartDataMapPtr
internal definition
Definition: MultiTypeMap.h:287
void unlock() const
internally unlocks the data store
Definition: MultiTypeMap.h:235
T & allocValue(const std::string &id, const T &val=T())
Allocates a new memory elements (new T(val)) for the given id string.
Definition: MultiTypeMap.h:111