Robot Raconteur Core C++ Library
CallbackMember.h
Go to the documentation of this file.
1 
24 #pragma once
25 
28 #include "RobotRaconteur/Service.h"
29 
30 namespace RobotRaconteur
31 {
50 template <typename T>
51 class Callback : private boost::noncopyable
52 {
53 
54  protected:
55  std::string m_MemberName;
56 
57  public:
58  Callback(boost::string_ref name) { m_MemberName = RR_MOVE(name.to_string()); }
59 
60  virtual ~Callback() {}
61 
67  virtual T GetFunction() = 0;
68 
79  virtual void SetFunction(T value) = 0;
80 
100  virtual T GetClientFunction(const RR_SHARED_PTR<Endpoint>& endpoint) = 0;
101 
113  virtual T GetClientFunction(uint32_t endpoint) = 0;
114 
120  virtual std::string GetMemberName() { return m_MemberName; }
121 
122  virtual void Shutdown() {}
123 };
124 
125 template <typename T>
126 class CallbackClient : public Callback<T>
127 {
128  public:
129  CallbackClient(boost::string_ref name) : Callback<T>(name) {}
130 
131  RR_OVIRTUAL ~CallbackClient() RR_OVERRIDE {}
132 
133  private:
134  T function;
135  boost::mutex function_lock;
136 
137  public:
138  RR_OVIRTUAL T GetFunction() RR_OVERRIDE
139  {
140  boost::mutex::scoped_lock lock(function_lock);
141  if (!function)
142  throw InvalidOperationException("Callback function not set");
143  return function;
144  }
145  RR_OVIRTUAL void SetFunction(T value) RR_OVERRIDE
146  {
147  boost::mutex::scoped_lock lock(function_lock);
148  function = value;
149  }
150 
151  RR_OVIRTUAL T GetClientFunction(const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE
152  {
153  RR_UNUSED(e);
154  throw InvalidOperationException("Invalid for client side of callback");
155  }
156 
157  RR_OVIRTUAL T GetClientFunction(uint32_t e) RR_OVERRIDE
158  {
159  RR_UNUSED(e);
160  throw InvalidOperationException("Invalid for client side of callback");
161  }
162 
163  RR_OVIRTUAL void Shutdown() RR_OVERRIDE
164  {
165  boost::mutex::scoped_lock lock(function_lock);
166  function.clear();
167  }
168 };
169 
170 class ROBOTRACONTEUR_CORE_API ServiceSkel;
171 
172 class ROBOTRACONTEUR_CORE_API CallbackServerBase
173 {
174 
175  public:
176  virtual ~CallbackServerBase() {}
177 
178  protected:
179  RR_WEAK_PTR<ServiceSkel> skel;
180 
181  virtual RR_SHARED_PTR<void> GetClientFunction_internal(uint32_t e);
182 
183  virtual std::string GetMemberName() = 0;
184 };
185 
186 template <typename T>
187 class CallbackServer : public Callback<T>, public CallbackServerBase
188 {
189  public:
190  CallbackServer(boost::string_ref name, const RR_SHARED_PTR<ServiceSkel>& skel) : Callback<T>(name)
191  {
192  this->skel = skel;
193  }
194 
195  RR_OVIRTUAL ~CallbackServer() RR_OVERRIDE {}
196 
197  RR_OVIRTUAL T GetFunction() RR_OVERRIDE { throw InvalidOperationException("Invalid for server side of callback"); }
198  RR_OVIRTUAL void SetFunction(T value) RR_OVERRIDE
199  {
200  RR_UNUSED(value);
201  throw InvalidOperationException("Invalid for server side of callback");
202  }
203 
204  RR_OVIRTUAL T GetClientFunction(const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE
205  {
206  RR_UNUSED(e);
207  return GetClientFunction(e->GetLocalEndpoint());
208  }
209 
210  RR_OVIRTUAL T GetClientFunction(uint32_t e) RR_OVERRIDE
211  {
212  RR_SHARED_PTR<ServiceSkel> s = skel.lock();
213  if (!s)
214  throw InvalidOperationException("Callback server has been closed");
215  return *RR_STATIC_POINTER_CAST<T>(s->GetCallbackFunction(e, GetMemberName()));
216  }
217 
218  RR_OVIRTUAL std::string GetMemberName() RR_OVERRIDE { return Callback<T>::GetMemberName(); }
219 
220  RR_OVIRTUAL void Shutdown() RR_OVERRIDE {}
221 };
222 
223 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
225 template <typename T>
226 using CallbackPtr = RR_SHARED_PTR<Callback<T> >;
228 template <typename T>
229 using CallbackConstPtr = RR_SHARED_PTR<const Callback<T> >;
230 #endif
231 } // namespace RobotRaconteur
boost::shared_ptr< Callback< T > > CallbackPtr
Convenience alias for Callback shared_ptr.
Definition: CallbackMember.h:226
boost::shared_ptr< const Callback< T > > CallbackConstPtr
Convenience alias for Callback const shared_ptr.
Definition: CallbackMember.h:229
callback member type interface
Definition: CallbackMember.h:52
virtual void SetFunction(T value)=0
Set the callback function reference on the client side.
virtual T GetClientFunction(uint32_t endpoint)=0
Get the proxy function to call the callback for the specified client on the service side.
virtual T GetClientFunction(const boost::shared_ptr< Endpoint > &endpoint)=0
Get the proxy function to call the callback for the specified client on the service side.
virtual T GetFunction()=0
Get the currently configured callback function on client side.
virtual std::string GetMemberName()
Get the member name of the callback.
Definition: CallbackMember.h:120