Image Component Library (ICL)
RSBIOUtil.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 : ICLIO/src/ICLIO/RSBIOUtil.h **
10 ** Module : ICLIO **
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/Uncopyable.h>
34 #include <ICLUtils/Exception.h>
35 #include <ICLUtils/Function.h>
36 #include <ICLUtils/Mutex.h>
37 #include <string>
38 #include <vector>
39 #include <map>
40 
41 #include <rsb/Factory.h>
42 #include <rsb/Handler.h>
43 #include <rsb/converter/Repository.h>
44 #include <rsb/converter/ProtocolBufferConverter.h>
45 
46 #include <rst/generic/Value.pb.h>
47 #include <rst/generic/Dictionary.pb.h>
48 #include <rst/generic/KeyValuePair.pb.h>
49 
50 namespace icl{
51  namespace io{
52 
53 
54 
55  // Tier 2: Basic data containment slice
56  template<class T>
58  virtual ~RSBIOUtilDataBase(){}
59 
60  typedef rsb::Informer<T> Informer;
61  typedef typename Informer::Ptr InformerPtr;
62  typedef typename Informer::DataPtr DataPtr;
63  typedef typename rsb::Scope Scope;
64  typedef typename rsb::ListenerPtr ListenerPtr;
65 
68 
73 
75  std::map<std::string,Callback> m_callbacks;
76  };
77 
79  template<class T>
81  typedef typename rsb::converter::ProtocolBufferConverter<T> Converter;
82  typedef typename boost::shared_ptr<Converter> ConverterPtr;
83  static void register_type(){
84  ConverterPtr c(new Converter);
85  rsb::converter::converterRepository<std::string>()->registerConverter(c);
86  }
87  };
88 
90 #define REGISTER_RSBIOUtil_COMMON_TYPE(T,NAME,FULL_NAME) \
91  template<> \
92  struct RSBIOUtilDataExtra<T> : public RSBIOUtilDataBase<T>{ \
93  static void register_type(){} \
94  };
95 
96 
97  REGISTER_RSBIOUtil_COMMON_TYPE(std::string,string,primitive.string);
98  REGISTER_RSBIOUtil_COMMON_TYPE(int32_t,int,primitive.int);
99  REGISTER_RSBIOUtil_COMMON_TYPE(uint32_t,int,primitive.int);
100  REGISTER_RSBIOUtil_COMMON_TYPE(int64_t,int,primitive.int);
101  REGISTER_RSBIOUtil_COMMON_TYPE(uint64_t,int,primitive.int);
102  REGISTER_RSBIOUtil_COMMON_TYPE(float,float,primitive.float);
103  REGISTER_RSBIOUtil_COMMON_TYPE(double,double,primitive.double);
104  REGISTER_RSBIOUtil_COMMON_TYPE(bool,bool,primitive.bool);
105 
106  /*
107  REGISTER_RSBIOUtil_COMMON_TYPE( ::rst::generic::Value,Value,rst.generic.Value);
108  REGISTER_RSBIOUtil_COMMON_TYPE( ::rst::generic::Dictionary,Dictionary,rst.generic.Dictionary);
109  REGISTER_RSBIOUtil_COMMON_TYPE( ::rst::generic::KeyValuePair,KeyValuePair,rst.generic.KeyValuePair);
110  */
111 
113 
119  template<class T>
120  class RSBIOUtil : public RSBIOUtilDataExtra<T>, public utils::Uncopyable{
121  public:
122 
125 
126  inline RSBIOUtil(const std::string &mode, const std::string &scope, bool autoRegisterType=true){
127  if(autoRegisterType){
128  static struct StaticTypeRegistration{
129  StaticTypeRegistration(){
131  }
132  } static_type_regitration;
133  }
134 
135  Super::m_scope = rsb::Scope(scope);
136 
137  rsb::Factory &factory = rsb::getFactory();
138  if(mode == "send"){
139  rsb::ParticipantConfig cfg = factory.getDefaultParticipantConfig();
140  Super::m_informer = factory.createInformer<T>(Super::m_scope,cfg);
141  Super::m_data = typename Super::DataPtr(new T);
142  }else if(mode == "receive"){
143  Super::m_listener = factory.createListener(Super::m_scope);
144  typedef typename rsb::DataFunctionHandler<T> FHandler;
145  typename FHandler::DataFunction f = boost::bind(&RSBIOUtil<T>::handle,this,_1);
146  Super::m_listener->addHandler(rsb::HandlerPtr(new FHandler(f)));
147  }else{
148  throw utils::ICLException("RSBIOUtil: invalid mode " + mode);
149  }
150  }
151 
152  void send(const T &t){
153  *Super::m_data = t;
155  }
156 
157  void send(typename Super::DataPtr data){
158  Super::m_informer->publish(data);
159  }
160 
161  void handle(typename Super::DataPtr data){
163  for(typename std::map<std::string,typename Super::Callback>::iterator it = Super::m_callbacks.begin();
164  it != Super::m_callbacks.end();++it){
165  it->second(*data);
166  }
167  }
168 
169  void registerListenerCallback(typename Super::Callback cb, const std::string &id="default"){
171  Super::m_callbacks[id] = cb;
172  }
173 
174  void unregisterListenerCallback(const std::string &id="default"){
176  typename std::map<std::string,typename Super::Callback>::iterator it = Super::m_callbacks.find(id);
177  if(it != Super::m_callbacks.end()){
178  Super::m_callbacks.erase(it);
179  }else{
180  WARNING_LOG("could not remove callback " << id << " callback was not registered");
181  }
182  }
183  };
184 
185  template<class T>
186  class RSBSender {
188  public:
189 
191 
192  inline RSBSender(const std::string &scope=""){
193  if(scope.length()) init(scope);
194  }
195  inline bool isNull() const { return !impl; }
196 
197  inline operator bool() const { return !isNull(); }
198 
199  inline void init(const std::string &scope){
200  impl = new RSBIOUtil<T>("send",scope);
201  }
202 
203  inline void send(const T &t){
204  null_check(__FUNCTION__);
205  impl->send(t);
206  }
207 
209  impl->send(data);
210  }
211 
212 
213  protected:
214  inline void null_check(const std::string &fn) const {
215  if (isNull()) throw utils::ICLException(fn + ": RSBListener is null");
216  }
217  };
218 
219  template<class T>
220  class RSBListener {
222  public:
223 
226 
227  inline RSBListener(const std::string &scope=""){
228  if(scope.length()) init(scope);
229  }
230 
231  inline bool isNull() const { return !impl; }
232 
233  inline operator bool() const { return !isNull(); }
234 
235  inline void init(const std::string &scope){
236  impl = new RSBIOUtil<T>("receive",scope);
237  }
238 
239  void registerCallback(Callback cb, const std::string &id="default"){
240  null_check(__FUNCTION__);
241  impl->registerListenerCallback(cb,id);
242  }
243 
244  void unregisterCallback(const std::string &id="default"){
245  null_check(__FUNCTION__);
246  impl->unregisterListenerCallback(id);
247  }
248 
249  protected:
250  inline void null_check(const std::string &fn) const {
251  if (isNull()) throw utils::ICLException(fn + ": RSBSender is null");
252  }
253 
254  };
255 
256  }
257 }
The General Function Template.
Definition: Function.h:284
Scope m_scope
Definition: RSBIOUtil.h:71
RSBIOUtilDataBase< T >::Callback Callback
Definition: RSBIOUtil.h:224
Definition: RSBIOUtil.h:57
Simple and ready to use RSB-Informer and RSB-Listener Interface.
Definition: RSBIOUtil.h:120
undocument this line if you encounter any issues!
Definition: Any.h:37
Class interface for un-copyable classes.
Definition: Uncopyable.h:64
utils::SmartPtr< RSBIOUtil< T > > impl
Definition: RSBIOUtil.h:187
void null_check(const std::string &fn) const
Definition: RSBIOUtil.h:214
Definition: RSBIOUtil.h:220
RSBListener(const std::string &scope="")
Definition: RSBIOUtil.h:227
REGISTER_RSBIOUtil_COMMON_TYPE(std::string, string, primitive.string)
void send(const T &t)
Definition: RSBIOUtil.h:203
bool isNull() const
Definition: RSBIOUtil.h:231
void send(const T &t)
Definition: RSBIOUtil.h:152
#define WARNING_LOG(x)
Definition: Macros.h:117
rsb::converter::ProtocolBufferConverter< T > Converter
Definition: RSBIOUtil.h:81
RSBIOUtilDataExtra< T > Super
Definition: RSBIOUtil.h:123
rsb::ListenerPtr ListenerPtr
Definition: RSBIOUtil.h:64
boost::shared_ptr< Converter > ConverterPtr
Definition: RSBIOUtil.h:82
utils::SmartPtr< RSBIOUtil< T > > impl
Definition: RSBIOUtil.h:221
void null_check(const std::string &fn) const
Definition: RSBIOUtil.h:250
InformerPtr m_informer
Definition: RSBIOUtil.h:69
void init(const std::string &scope)
Definition: RSBIOUtil.h:235
Informer::Ptr InformerPtr
Definition: RSBIOUtil.h:61
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
rsb::Scope Scope
Definition: RSBIOUtil.h:63
Informer::DataPtr DataPtr
Definition: RSBIOUtil.h:62
static void register_type()
Definition: RSBIOUtil.h:83
bool isNull() const
Definition: RSBIOUtil.h:195
ListenerPtr m_listener
Definition: RSBIOUtil.h:72
void registerListenerCallback(typename Super::Callback cb, const std::string &id="default")
Definition: RSBIOUtil.h:169
void send(typename Super::DataPtr data)
Definition: RSBIOUtil.h:157
virtual ~RSBIOUtilDataBase()
Definition: RSBIOUtil.h:58
rsb::Informer< T > Informer
Definition: RSBIOUtil.h:60
std::map< std::string, Callback > m_callbacks
Definition: RSBIOUtil.h:75
void unregisterListenerCallback(const std::string &id="default")
Definition: RSBIOUtil.h:174
Definition: RSBIOUtil.h:186
void send(DataPtr data)
Definition: RSBIOUtil.h:208
RSBIOUtil(const std::string &mode, const std::string &scope, bool autoRegisterType=true)
creates an instance with given mode and scope
Definition: RSBIOUtil.h:126
RSBSender(const std::string &scope="")
Definition: RSBIOUtil.h:192
Tier 3: branding for using protocol-buffer types by default!
Definition: RSBIOUtil.h:80
Base class for Exception handling in the ICL.
Definition: Exception.h:42
void unregisterCallback(const std::string &id="default")
Definition: RSBIOUtil.h:244
RSBIOUtilDataBase< T >::DataPtr DataPtr
Definition: RSBIOUtil.h:190
void handle(typename Super::DataPtr data)
Definition: RSBIOUtil.h:161
Mutex class of the ICL.
Definition: Mutex.h:54
DataPtr m_data
Definition: RSBIOUtil.h:70
void registerCallback(Callback cb, const std::string &id="default")
Definition: RSBIOUtil.h:239
RSBIOUtilDataBase< T >::DataPtr DataPtr
Definition: RSBIOUtil.h:225
Locks a mutex on the stack (mutex is unlocked when the stack's section is released.
Definition: Mutex.h:120
Specialization of the SmartPtrBase class for Pointers.
Definition: SmartPtr.h:75
void init(const std::string &scope)
Definition: RSBIOUtil.h:199
utils::Mutex m_mutex
Definition: RSBIOUtil.h:74
utils::Function< void, const T & > Callback
Callback type that is used for listener_callbacks.
Definition: RSBIOUtil.h:67