MIRA
Stamped.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_STAMPED_H_
48 #define _MIRA_STAMPED_H_
49 
50 #include <serialization/adapters/boost/shared_ptr.hpp>
51 
52 #include <utils/Time.h>
53 
54 namespace mira {
55 
57 
63 {
64 public:
70 
75  StampedHeader(const Time& iTimestamp, uint32 iSequenceID = 0) :
76  timestamp(iTimestamp),
77  sequenceID(iSequenceID) {}
78 
83  StampedHeader(const Time& iTimestamp,
84  const std::string& iFrameID, uint32 iSequenceID = 0) :
85  timestamp(iTimestamp),
86  frameID(iFrameID),
87  sequenceID(iSequenceID) {}
88 
89 public:
90 
91  template<typename Reflector>
92  void reflect(Reflector& r)
93  {
94  r.member("Timestamp", timestamp, "The timestamp", Time::now());
95  r.member("FrameID", frameID,
96  "The id of the transformation frame, this data belongs to",
97  "");
98  r.member("SequenceID", sequenceID, "A user defined sequence ID", 0);
99  }
100 
101 public:
102 
105 
108  std::string frameID;
109 
111  uint32 sequenceID;
112 };
113 
148 template <typename T>
149 class Stamped : public StampedHeader, public T
150 {
151 public:
152 
153  typedef T value_type ;
154 
155 public:
156 
161  Stamped() {}
162 
168  Stamped(const T& iData, const Time& iTimestamp, uint32 iSequenceID = 0) :
169  StampedHeader(iTimestamp, iSequenceID), T(iData) {}
170 
172  Stamped(T&& iData, const Time& iTimestamp, uint32 iSequenceID = 0) :
173  StampedHeader(iTimestamp, iSequenceID), T(std::move(iData)) {}
174 
179  Stamped(const T& iData, const Time& iTimestamp,
180  const std::string& iFrameID, uint32 iSequenceID = 0) :
181  StampedHeader(iTimestamp, iFrameID, iSequenceID), T(iData) {}
182 
184  Stamped(T&& iData, const Time& iTimestamp,
185  const std::string& iFrameID, uint32 iSequenceID = 0) :
186  StampedHeader(iTimestamp, iFrameID, iSequenceID), T(std::move(iData)) {}
187 
188 public:
189 
190  template<typename Reflector>
191  void reflect(Reflector& r)
192  {
194  r.member("Value", (T&)*this, "The value");
195  }
196 
197 public:
198 
202  T& value() { return *this; }
203 
207  const T& value() const { return *this; }
208 
212  T& internalValueRep() { return *this; }
213 
217  const T& internalValueRep() const { return *this; }
218 
219 };
220 
222 
223 class Object; // forward declaration of mira::Object in factory/Object.h
224 
237 template <typename T>
238 class Stamped<T*> : public StampedHeader
239 {
240  static_assert(std::is_base_of<mira::Object,T>::value,
241  "Pointers that are used with Stamped<T> must be of polymorphic"
242  " classes that are derived from mira::Object. Pointers to other"
243  " classes cannot be used with Stamped<T>.");
244 public:
245 
246  typedef T* value_type ;
247 
248 public:
249 
254  Stamped() {}
255 
260  Stamped(T* iData, const Time& iTimestamp, uint32 iSequenceID = 0) :
261  StampedHeader(iTimestamp, iSequenceID), mPointer(iData) {}
262 
267  Stamped(T* iData, const Time& iTimestamp, const std::string& iFrameID,
268  uint32 iSequenceID = 0) :
269  StampedHeader(iTimestamp, iFrameID, iSequenceID), mPointer(iData) {}
270 
271 public:
272 
273  template<typename Reflector>
274  void reflect(Reflector& r)
275  {
277  r.member("Value", mPointer, "The value");
278  }
279 
280 public:
281 
285  T* value() { return dynamic_cast<T*>(mPointer.get()); }
286 
290  const T* value() const { return dynamic_cast<T*>(mPointer.get()); }
291 
293  operator const T*() const { return value(); }
294 
296  operator T*() { return value(); }
297 
299  T* operator=(T* iValue) {mPointer.reset(iValue); return iValue; }
300 
304  boost::shared_ptr<Object>& internalValueRep() { return mPointer; }
305 
309  const boost::shared_ptr<Object>& internalValueRep() const { return mPointer; }
310 
311 public:
312 
313  // Note: the following casts look dangerous, however, they're safe since
314  // all Stamped<T*> store a pointer to Object* and hence all of them are
315  // equal. Moreover, our accessor methods above take care of type safety
316  // using the dynamic_casts when accessing the underlying data.
317 
319  template <typename U>
320  operator const Stamped<U*>& () const {
321  static_assert(sizeof(Stamped<U*>)==sizeof(Stamped<T*>),
322  "The following is safe as long as all Stamped<T*> have the same size");
323  return reinterpret_cast<const Stamped<U*>&>(*this);
324  }
325 
327  template <typename U>
328  operator Stamped<U*>& () {
329  static_assert(sizeof(Stamped<U*>)==sizeof(Stamped<T*>),
330  "The following is safe as long as all Stamped<T*> have the same size");
331  return reinterpret_cast<Stamped<U*>&>(*this);
332  }
333 
334 private:
335 
337  boost::shared_ptr<Object> mPointer;
338 };
339 
341 
356 template <typename T>
358 {
359 public:
360 
361  typedef T value_type ;
362 
363 public:
364 
367 
372  StampedPrimitive(const T& iData, const Time& iTimestamp,
373  uint32 iSequenceID = 0) :
374  StampedHeader(iTimestamp, iSequenceID), mValue(iData) {}
375 
380  StampedPrimitive(const T& iData, const Time& iTimestamp,
381  const std::string& iFrameID, uint32 iSequenceID = 0) :
382  StampedHeader(iTimestamp, iFrameID, iSequenceID), mValue(iData) {}
383 
384 public:
385 
386  template<typename Reflector>
387  void reflect(Reflector& r)
388  {
390  r.member("Value", mValue, "The value");
391  }
392 
393 public:
394 
398  T& value() { return mValue; }
399 
403  const T& value() const { return mValue; }
404 
406  operator const T&() const { return mValue; }
407 
409  operator T&() { return mValue; }
410 
412  const T& operator=(const T& iValue) { return mValue = iValue; }
413 
417  T& internalValueRep() { return mValue; }
418 
422  const T& internalValueRep() const { return mValue; }
423 
424 private:
425 
427  T mValue;
428 };
429 
431 
435 #define MIRA_DECLARE_STAMPED_PRIMITIVE_SPECIALIZATION(T) \
436 template <> \
437 class Stamped<T> : public StampedPrimitive<T> \
438 { \
439 public: \
440  Stamped() {} \
441  Stamped(const T& iData, const Time& iTimestamp, uint32 iSequenceID=0) : \
442  StampedPrimitive<T>(iData, iTimestamp,iSequenceID) {} \
443  Stamped(const T& iData, const Time& iTimestamp, \
444  const std::string& iFrameID, uint32 iSequenceID=0) : \
445  StampedPrimitive<T>(iData, iTimestamp, iFrameID, iSequenceID) {} \
446 };
447 
464 
465 
470 template <typename T>
472  const std::string& frameID = std::string(),
473  uint32 sequenceID = 0)
474 {
475  return Stamped<typename std::decay<T>::type>(std::forward<T>(value), timestamp, frameID, sequenceID);
476 }
477 
481 template <typename T>
483  return data;
484 }
485 
486 template <typename T>
488  return data;
489 }
490 
491 
493 
494 }
495 
496 #endif
T * value_type
Definition: Stamped.h:243
T & value()
Returns a read-write reference to the underlying data.
Definition: Stamped.h:398
Stamped(T *iData, const Time &iTimestamp, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp and the actual data.
Definition: Stamped.h:260
T & value()
Returns a read-write reference to the underlying data.
Definition: Stamped.h:202
const T & internalValueRep() const
Returns a read-only reference to the underlying data representation.
Definition: Stamped.h:422
T value_type
Definition: Stamped.h:153
StampedPrimitive(const T &iData, const Time &iTimestamp, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp and the actual data.
Definition: Stamped.h:372
Time timestamp
The time stamp when the data was obtained.
Definition: Stamped.h:104
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Stamped()
Default constructor, that does not initialize time or frame id.
Definition: Stamped.h:161
Stamped(T &&iData, const Time &iTimestamp, const std::string &iFrameID, uint32 iSequenceID=0)
Same as above, for rvalue reference (move semantics)
Definition: Stamped.h:184
Stamped(const T &iData, const Time &iTimestamp, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp and the actual data.
Definition: Stamped.h:168
const T & internalValueRep() const
Returns a read-only reference to the underlying data representation.
Definition: Stamped.h:217
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
Time and Duration wrapper class.
STL namespace.
Stamped class specialization for polymorphic pointers.
Definition: Stamped.h:238
StampedHeader & stampedHeader(Stamped< T > &data)
Helper to extract the header from stamped data.
Definition: Stamped.h:482
boost::shared_ptr< Object > & internalValueRep()
Returns a read-write reference to the underlying data representation.
Definition: Stamped.h:304
T & internalValueRep()
Returns a read-write reference to the underlying data representation.
Definition: Stamped.h:417
StampedHeader(const Time &iTimestamp, uint32 iSequenceID=0)
Constructs the header from its time stamp and an optional sequence id.
Definition: Stamped.h:75
const T & value() const
Returns a read-only reference to the underlying data.
Definition: Stamped.h:403
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
T & internalValueRep()
Returns a read-write reference to the underlying data representation.
Definition: Stamped.h:212
void reflect(Reflector &r)
Definition: Stamped.h:92
Stamped(const T &iData, const Time &iTimestamp, const std::string &iFrameID, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp, the frame id it belongs to and the actual data...
Definition: Stamped.h:179
Stamped(T *iData, const Time &iTimestamp, const std::string &iFrameID, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp, the frame id and the actual data.
Definition: Stamped.h:267
The common header for all stamped data.
Definition: Stamped.h:62
StampedHeader()
Default constructor, that does not initialize time or frame id.
Definition: Stamped.h:69
void reflect(Reflector &r)
Definition: Stamped.h:274
Stamped()
Default constructor, no time is set and data pointer is null.
Definition: Stamped.h:254
const T & value() const
Returns a read-only reference to the underlying data.
Definition: Stamped.h:207
const boost::shared_ptr< Object > & internalValueRep() const
Returns a read-only reference to the underlying data representation.
Definition: Stamped.h:309
Mix in for adding a time stamp, an optional frame id and an optional sequence id to data types like P...
Definition: Stamped.h:149
json_spirit::mObject Object
A representation of an object (class, struct) in JSON.
Definition: JSON.h:183
StampedPrimitive()
Default constructor, that does not initializes anything.
Definition: Stamped.h:366
Stamped< typename std::decay< T >::type > makeStamped(T &&value, const Time &timestamp=Time::now(), const std::string &frameID=std::string(), uint32 sequenceID=0)
Declare stamped classes for all standard data types including std::string.
Definition: Stamped.h:471
const T * value() const
Returns the underlying pointer as constant pointer.
Definition: Stamped.h:290
StampedPrimitive(const T &iData, const Time &iTimestamp, const std::string &iFrameID, uint32 iSequenceID=0)
Constructs the Stamped data from its time stamp, the frame id and the actual data.
Definition: Stamped.h:380
Stamped class for primitive types like int, float, etc.
Definition: Stamped.h:357
Stamped(T &&iData, const Time &iTimestamp, uint32 iSequenceID=0)
Same as above, for rvalue reference (move semantics)
Definition: Stamped.h:172
T * value()
Returns the underlying pointer.
Definition: Stamped.h:285
T * operator=(T *iValue)
Assignment operator that can assign T to this.
Definition: Stamped.h:299
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
#define MIRA_DECLARE_STAMPED_PRIMITIVE_SPECIALIZATION(T)
Macro for specializing template class Stamped for primitive types.
Definition: Stamped.h:435
uint32 sequenceID
A user defined sequence ID.
Definition: Stamped.h:111
const T & operator=(const T &iValue)
Assignment operator that can assign T to this.
Definition: Stamped.h:412
void reflect(Reflector &r)
Definition: Stamped.h:191
StampedHeader(const Time &iTimestamp, const std::string &iFrameID, uint32 iSequenceID=0)
Constructs the header from its time stamp, the frame id and an optional sequence id.
Definition: Stamped.h:83
std::string frameID
The unique id of the transform frame that this data is assigned to (if any, otherwise empty) ...
Definition: Stamped.h:108
void reflect(Reflector &r)
Definition: Stamped.h:387
T value_type
Definition: Stamped.h:361