MIRA
SDOFuture.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 MetraLabs GmbH (MLAB), GERMANY.
3  * All rights reserved.
4  * Contact: info@MetraLabs.com
5  *
6  * Commercial Usage:
7  * Licensees holding valid commercial licenses may use this file in
8  * accordance with the commercial license agreement provided with the
9  * software or, alternatively, in accordance with the terms contained in
10  * a written agreement between you and MetraLabs.
11  *
12  * GNU General Public License Usage:
13  * Alternatively, this file may be used under the terms of the GNU
14  * General Public License version 3.0 as published by the Free Software
15  * Foundation and appearing in the file LICENSE.GPL3 included in the
16  * packaging of this file. Please review the following information to
17  * ensure the GNU General Public License version 3.0 requirements will be
18  * met: http://www.gnu.org/copyleft/gpl.html.
19  * Alternatively you may (at your option) use any later version of the GNU
20  * General Public License if such license has been publicly approved by
21  * MetraLabs (or its successors, if any).
22  *
23  * IN NO EVENT SHALL "MLAB" BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
24  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
25  * OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLABS" HAS BEEN
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * "MLAB" SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
31  * "AS IS" BASIS, AND "MLAB" HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
32  * SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
33  */
34 
43 #ifndef _MLAB_SDOFUTURE_H_
44 #define _MLAB_SDOFUTURE_H_
45 
46 #ifndef Q_MOC_RUN
47 #include <boost/thread/future.hpp>
48 #endif
49 
50 namespace mira { namespace can {
51 
53 
55 {
56 public:
57  virtual ~SDOClientBase() {}
58 
59  virtual void onDestructFuture(uint64 requestID) = 0;
60 };
61 
63 
68 template <typename R>
70 {
71 public:
72 
74 
75  SDOFutureCommon(SDOClientBase* client, uint64 requestID) :
76  mClient(client), mRequestID(requestID) {}
77 
79  {
80  // remove call upon destruction of this future
81  if(mClient!=NULL)
83  }
84 
85 public:
86  // delegate to boost future
87 
89  bool isReady() const { return mFuture.is_ready(); }
90 
95  bool hasException() const { return mFuture.has_exception(); }
96 
101  bool hasValue() const { return mFuture.has_value(); }
102 
108  void wait() const { mFuture.wait(); }
109 
116  template<typename Duration>
117  bool timedWait(Duration const& relTime) const {
118  return mFuture.timed_wait(relTime);
119  }
120 
126  bool timedWaitUntil(boost::system_time const& absTime) const {
127  return mFuture.timed_wait_until(absTime);
128  }
129 
131  void swap(SDOFutureCommon<R>& other) {
132  mFuture.swap(other.mFuture);
133  std::swap(mClient, other.mClient);
134  std::swap(mRequestID, other.mRequestID);
135  }
136 
137 
138 protected:
140  uint64 mRequestID;
141  boost::unique_future<R> mFuture;
142 };
143 
145 
162 template <typename R>
163 class SDOFuture : public SDOFutureCommon<R>
164 {
165 public:
167 
168  SDOFuture(boost::unique_future<R> other, SDOClientBase* client,
169  uint64 requestID) :
170  SDOFutureCommon<R>(client, requestID)
171  {
172  this->mFuture.swap(other);
173  }
174 
176  SDOFuture(SDOFuture&& other) {
177  this->swap(other);
178  }
179 
182  this->swap(other);
183  return *this;
184  }
185 
186 public:
187 
189 #if BOOST_VERSION / 100 % 1000 > 56
190  typedef typename boost::detail::shared_state<R>::move_dest_type ReturnValue;
191 #else
192  typedef typename boost::detail::future_traits<R>::move_dest_type ReturnValue;
193 #endif
194 
200  operator ReturnValue() { return this->mFuture.get(); }
201 
210  ReturnValue get() { return this->mFuture.get(); }
211 };
212 
214 
220 template <>
221 class SDOFuture<void> : public SDOFutureCommon<void>
222 {
223 public:
225 
226  SDOFuture(boost::unique_future<void> other, SDOClientBase* client,
227  uint64 requestID) :
228  SDOFutureCommon<void>(client, requestID)
229  {
230  this->mFuture.swap(other);
231  }
232 
234  SDOFuture(SDOFuture&& other) {
235  this->swap(other);
236  }
237 
240  this->swap(other);
241  return *this;
242  }
243 
252  void get() { this->mFuture.get(); }
253 };
254 
256 
257 }} // namespaces
258 
259 #endif
bool timedWait(Duration const &relTime) const
Waits and blocks the current thread until the result of the associated RPC call is ready...
Definition: SDOFuture.h:117
virtual void onDestructFuture(uint64 requestID)=0
SDOClientBase * mClient
Definition: SDOFuture.h:139
bool timedWaitUntil(boost::system_time const &absTime) const
Waits and blocks the current thread until the result of the associated RPC call is ready...
Definition: SDOFuture.h:126
SDOFutureCommon()
Definition: SDOFuture.h:73
Definition: SDOFuture.h:54
~SDOFutureCommon()
Definition: SDOFuture.h:78
bool hasValue() const
Returns true if the RPC call associated with this future has finished with a return value value rathe...
Definition: SDOFuture.h:101
bool isReady() const
Checks to see if the result of the RPC call associated with this future is set.
Definition: SDOFuture.h:89
SDOFutureCommon(SDOClientBase *client, uint64 requestID)
Definition: SDOFuture.h:75
SDOFuture(SDOFuture &&other)
move constructor
Definition: SDOFuture.h:176
void swap(SDOFutureCommon< R > &other)
Swaps ownership of the asynchronous results associated with other and *this.
Definition: SDOFuture.h:131
SDOFuture()
Definition: SDOFuture.h:224
SDOFuture & operator=(SDOFuture &&other)
move assignment operator
Definition: SDOFuture.h:181
virtual ~SDOClientBase()
Definition: SDOFuture.h:57
bool hasException() const
Returns true if the RPC call associated with this future has finished with an exception rather than a...
Definition: SDOFuture.h:95
A SDOFuture is a proxy for the result of an asynchronous SDO upload or download.
Definition: SDOFuture.h:163
SDOFuture(boost::unique_future< void > other, SDOClientBase *client, uint64 requestID)
Definition: SDOFuture.h:226
void wait() const
Waits and blocks the current thread until the result of the associated RPC call is ready...
Definition: SDOFuture.h:108
SDOFuture(boost::unique_future< R > other, SDOClientBase *client, uint64 requestID)
Definition: SDOFuture.h:168
SDOFuture(SDOFuture &&other)
move constructor
Definition: SDOFuture.h:234
uint64 mRequestID
Definition: SDOFuture.h:140
SDOFuture()
Definition: SDOFuture.h:166
boost::unique_future< R > mFuture
Definition: SDOFuture.h:141
boost::detail::future_traits< R >::move_dest_type ReturnValue
The rvalue return value, which essentially can be thought of R&&.
Definition: SDOFuture.h:192
SDOFuture & operator=(SDOFuture &&other)
move assignment operator
Definition: SDOFuture.h:239
Wrapper for boost::unique_future that is specialized for SDO processing.
Definition: SDOFuture.h:69