GAMS  1.4.0
Stamped.h
Go to the documentation of this file.
1 
54 #include "ReferenceFrame.h"
55 
56 #ifndef _GAMS_POSE_STAMPED_H_
57 #define _GAMS_POSE_STAMPED_H_
58 
59 #include "gams/GamsExport.h"
60 #include <string>
61 #include <cfloat>
62 #include <utility>
63 #include "gams/CPP11_compat.h"
64 #include "madara/knowledge/containers/DoubleVector.h"
65 #include "madara/knowledge/containers/NativeDoubleVector.h"
66 #include "ReferenceFrameFwd.h"
67 #include "Coordinate.h"
68 
69 namespace gams { namespace pose {
70 
71 using madara::utility::Clock;
72 using madara::utility::TimeValue;
73 using madara::utility::Duration;
74 using madara::utility::SecondsDuration;
75 
78  (x.time(), x.time(TimeValue{})));
79 
80 inline TimeValue try_get_time()
81 {
82  return {};
83 }
84 
85 template<typename T, typename... Args>
86 inline auto try_get_time(const T& v, Args&&...) ->
87  typename std::enable_if<supports_timestamp<T>::value, TimeValue>::type
88 {
89  return v.time();
90 }
91 
92 template<typename T, typename... Args>
93 inline auto try_get_time(const T&, Args&&... args) ->
94  typename std::enable_if<!supports_timestamp<T>::value, TimeValue>::type
95 {
96  return try_get_time(std::forward<Args>(args)...);
97 }
98 
101 template<typename Impl>
102 class Stamped : public Impl
103 {
104 public:
105  using Impl::self;
106 
107  using derived_type = typename Impl::derived_type;
108 
112  Stamped() = default;
113 
114 #ifdef DOXYGEN
125  template<typename... Args>
126  Stamped(Args&&... args);
127 #else
128  template<typename Arg, typename... Args,
129  typename std::enable_if<!std::is_same<typename std::decay<Arg>::type,
130  TimeValue>::value && sizeof...(Args) >= 1, void*>::type = nullptr>
131  Stamped(Arg&& arg, Args&&... args)
132  : Impl((Arg &)arg, ((Args &)args)...),
133  time_(try_get_time((Arg &)arg, ((Args &)args)...)) {}
134 
135  template<typename Arg,
136  typename std::enable_if<!std::is_same<typename std::decay<Arg>::type,
137  TimeValue>::value, void*>::type = nullptr>
138  explicit Stamped(Arg&& arg)
139  : Impl((Arg &)arg), time_(try_get_time((Arg &)arg)) {}
140 #endif
141 
147  explicit Stamped(TimeValue time)
148  : Impl(), time_(time) {}
149 
150 #ifdef DOXYGEN
157  template<typename... Args>
158  Stamped(TimeValue time, Args&&... args);
159 #else
160  template<typename... Args,
161  typename std::enable_if<(sizeof...(Args) >= 1), void*>::type = nullptr>
162  Stamped(TimeValue time, Args&&... args)
163  : Impl(std::forward<Args>(args)...), time_(time) {}
164 #endif
165 
167  TimeValue time() const { return time_; }
168 
171  TimeValue time(TimeValue v) { return (time_ = v); }
172 
174  uint64_t nanos() const {
175  namespace sc = std::chrono;
176  return sc::duration_cast<Duration>(
177  time_.time_since_epoch()).count();
178  }
179 
181  uint64_t nanos(uint64_t v) {
182  Duration dur(v);
183  time_ = TimeValue(dur);
184  return v;
185  }
186 
188  double secs() const {
189  namespace sc = std::chrono;
190  return sc::duration_cast<SecondsDuration>(
191  time_.time_since_epoch()).count();
192  }
193 
195  double secs(double v) {
196  namespace sc = std::chrono;
197  SecondsDuration dur(v);
198  Duration ndur(sc::duration_cast<Duration>(dur));
199  time_ = TimeValue(ndur);
200  return v;
201  }
202 
203 private:
204  TimeValue time_;
205 };
206 
207 template<typename Base>
208 inline std::ostream &operator<<(std::ostream &o, const Stamped<Base> &v)
209 {
210  return o << v << "@" << v.time();
211 }
212 
213 } }
214 
215 #endif
Internal class implementing coordinates stamped with timestamp.
Definition: Stamped.h:103
double secs(double v)
Set time as seconds since std::steady_clock epoch.
Definition: Stamped.h:195
Stamped()=default
Default Constructor.
uint64_t nanos(uint64_t v)
Set time as nanos since std::steady_clock epoch.
Definition: Stamped.h:181
TimeValue time(TimeValue v)
Set time from std::chrono based TimeValue.
Definition: Stamped.h:171
double secs() const
Get time as seconds since std::steady_clock epoch.
Definition: Stamped.h:188
uint64_t nanos() const
Get time as nanos since std::steady_clock epoch.
Definition: Stamped.h:174
TimeValue time() const
Get time as std::chrono based TimeValue.
Definition: Stamped.h:167
TimeValue time_
Definition: Stamped.h:204
Stamped(TimeValue time)
Construct using a TimeValue object.
Definition: Stamped.h:147
typename Impl::derived_type derived_type
Definition: Stamped.h:107
Stamped(Args &&... args)
Construct without a TimeStamp object.
Stamped(TimeValue time, Args &&... args)
Construct with a timestamp.
std::ostream & operator<<(std::ostream &o, const Eigen::Vector3d &v)
Definition: Coordinate.h:1266
TimeValue try_get_time()
Definition: Stamped.h:80
MADARA_MAKE_VAL_SUPPORT_TEST(nano_timestamp, x,(x.nanos(), x.nanos(0UL)))
Type trait to detect stamped types.
Contains all GAMS-related tools, classes and code.
Copyright (c) 2015 Carnegie Mellon University.
Copyright (c) 2015 Carnegie Mellon University.