MIRA
TimeHumanReadable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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_TIMEHUMANREADABLE_H_
48 #define _MIRA_TIMEHUMANREADABLE_H_
49 
50 #include <utils/Time.h>
52 #include <serialization/Accessor.h>
53 
54 namespace mira {
55 
57 
79 namespace HumanReadableSerialization {
80 
86 template<bool AsString = true>
87 class Date : public mira::Date {
88 public:
89  typedef mira::Date Base;
90 public:
91 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
92  Date() {}
93 #endif
94  Date(const Base& date) : Base(date) {}
95  using mira::Date::Date; // inherit all constructors
96 };
97 
98 } // namespace HumanReadableSerialization
99 
102 
105 
108 
109 template<typename Reflector>
110 void reflectRead(Reflector& r, DateSerializedAsIsoString& date)
111 {
112  std::string s = to_iso_extended_string(date);
113  r.delegate(s);
114 }
115 
116 template<typename Reflector>
117 void reflectRead(Reflector& r, DateSerializedAsYMD& date)
118 {
119  DateSerializedAsYMD::ymd_type ymd = date.year_month_day();
120  int y = ymd.year;
121  int m = ymd.month;
122  int d = ymd.day;
123  r.member("Year", y, "year", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
124  r.member("Month", m, "month", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
125  r.member("Day", d, "day", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
126 
127  r.roproperty("Weekday", boost::lexical_cast<std::string>(date.day_of_week()),
128  "Day of week");
129 }
130 
131 template<typename Reflector>
132 void reflectWrite(Reflector& r, DateSerializedAsIsoString& date)
133 {
134  std::string s;
135  r.delegate(s);
136  date = boost::gregorian::from_string(s);
137 }
138 
139 template<typename Reflector>
140 void reflectWrite(Reflector& r, DateSerializedAsYMD& date)
141 {
142  int y, m, d;
143  r.member("Year", y, "");
144  r.member("Month", m, "");
145  r.member("Day", d, "");
146 
147  date = DateSerializedAsYMD::Base(y, m, d);
148 }
149 
150 
151 namespace HumanReadableSerialization {
152 
158 template<bool AsString = true>
159 class Duration : public mira::Duration {
160 public:
162 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
163  Duration() {}
164 #endif
165  Duration(const Base& date) : Base(date) {}
166  using mira::Duration::Duration; // inherit all constructors
167 
169 
170  template<typename Reflector>
171  void reflectRead(Reflector& r)
172  {
173  if constexpr (AsString) {
174  std::string s = to_simple_string(*this);
175  r.delegate(s);
176  }
177  else {
178  int h = hours();
179  int m = minutes();
180  int s = seconds();
181  int ms = milliseconds();
182  int us = microseconds();
183  r.member("Hours", h, "Hours 0..23", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
184  r.member("Minutes", m, "Minutes 0..59", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
185  r.member("Seconds", s, "Seconds 0..59", REFLECT_CTRLFLAG_MEMBER_AS_ROPROPERTY);
186  r.member("MilliSeconds", ms, "Milliseconds 0..999 (optional, default=0)",
188  r.member("MicroSeconds", us, "Microseconds 0..999 (optional, default=0)",
190  }
191  }
192 
193  template<typename Reflector>
194  void reflectWrite(Reflector& r)
195  {
196  if constexpr (AsString) {
197  std::string s;
198  r.delegate(s);
199  *this = Base(boost::posix_time::duration_from_string(s));
200  } else {
201  int64 h, m, s, ms, us;
202  r.member("Hours", h, "Hours 0..23");
203  r.member("Minutes", m, "Minutes 0..59");
204  r.member("Seconds", s, "Seconds 0..59");
205  r.member("MilliSeconds", ms, "Milliseconds 0..999 (optional, default=0)", 0);
206  r.member("MicroSeconds", us, "Microseconds 0..999 (optional, default=0)", 0);
207 
208  *this = Base::microseconds(1000ll*(1000ll*(60ll*(60ll*h+m)+s)+ms)+us);
209  }
210  }
211 };
212 
213 } // namespace HumanReadableSerialization
214 
217 
220 
221 namespace HumanReadableSerialization {
222 
228 template<bool AsString = true>
229 class Time : public mira::Time {
230 public:
231  typedef mira::Time Base;
232 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
233  Time() {}
234 #endif
235  Time(const Base& date) : Base(date) {}
236  using mira::Time::Time; // inherit all constructors
237 
239 
240  template<typename Reflector>
241  void reflectRead(Reflector& r)
242  {
243  if constexpr (AsString) {
244  std::string s = to_iso_extended_string(*this);
245  r.delegate(s);
246  } else {
247  DateSerializedAsYMD dateHR(date());
248  mira::reflectRead(r, dateHR);
249  DurationSerializedAsHMS(time_of_day()).reflectRead(r);
250  }
251  }
252 
253  template<typename Reflector>
254  void reflectWrite(Reflector& r)
255  {
256  if constexpr (AsString) {
257  std::string s;
258  r.delegate(s);
259 #if BOOST_VERSION >= 107300
260  *this = Base(boost::posix_time::from_iso_extended_string(s));
261 #else
262  *this = Base(boost::date_time::parse_delimited_time<Base>(s, 'T'));
263 #endif
264  } else {
265  DateSerializedAsYMD date;
266  mira::reflectWrite(r, date);
267 
268  DurationSerializedAsHMS timeOfDay;
269  timeOfDay.reflectWrite(r);
270 
271  *this = Base(date, timeOfDay);
272  }
273  }
274 };
275 
276 } // namespace HumanReadableSerialization
277 
287 
290 
291 
292 template<typename SerializerTag>
293 class IsTransparentSerializable<DateSerializedAsIsoString, SerializerTag> : public std::true_type {};
294 
295 template<typename SerializerTag>
296 class IsTransparentSerializable<DurationSerializedAsIsoString, SerializerTag> : public std::true_type {};
297 
298 template<typename SerializerTag>
299 class IsTransparentSerializable<TimeSerializedAsIsoString ,SerializerTag> : public std::true_type {};
300 
302 
308 
331 
334 
338 
341 
344 
348 
351 
354 
358 
361 
364 
368 
371 
374 
378 
381 
384 
388 
390 
391 }
392 
393 #endif
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Definition: TimeHumanReadable.h:171
void reflectWrite(Reflector &r)
Definition: TimeHumanReadable.h:194
void reflectWrite(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:581
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
tick_type milliseconds() const
Returns normalized number of milliseconds (0..999)
Definition: Time.h:283
#define MIRA_SPLIT_REFLECT(Type)
Macro that inserts a reflect() method consisting of just a call to splitReflect() (splitting to refle...
Definition: SplitReflect.h:150
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Duration()
Default constructor constructs to Duration of 0 length.
Definition: Time.h:204
#define MIRA_SPLIT_REFLECT_MEMBER
Macro that insert a class member reflect() method just splitting reflection into a reflectRead() and ...
Definition: SplitReflect.h:209
Time and Duration wrapper class.
Holds a boost::function object to a special setter function that must meet the signature "void method...
Definition: GetterSetter.h:395
mira::Date Base
Definition: TimeHumanReadable.h:89
Contains internal accessor class that abstracts from the underlying getter and setter classes or dire...
Derivation of mira::Date with human-readable serialization.
Definition: TimeHumanReadable.h:87
boost::posix_time::ptime Base
Definition: Time.h:420
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Definition: TimeHumanReadable.h:241
Flags controlling reflector behaviour.
Derivation of mira::Duration with human-readable serialization.
Definition: TimeHumanReadable.h:159
Time()
Default constructor constructs to not_a_date_time.
Definition: Time.h:449
boost::posix_time::time_duration Base
Definition: Time.h:108
min_type minutes() const
Returns normalized number of minutes (0..59)
Definition: Time.h:273
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:278
Use this class to represent time durations.
Definition: Time.h:104
boost::gregorian::date Date
Typedef for the boost Gregorian calendar date.
Definition: Time.h:68
hour_type hours() const
Returns number of hours in the duration.
Definition: Time.h:268
Setter< DateSerializedAsIsoString > humanReadableSetter(Date &date)
Setter for Date using DateSerializedAsIsoString proxy.
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:288
Date()
Definition: TimeHumanReadable.h:92
mira::Time Base
Definition: TimeHumanReadable.h:231
The Accessor class is used as an adapter to reduce the code bloat within the reflection and serializa...
Definition: Accessor.h:244
void reflectWrite(Reflector &r)
Definition: TimeHumanReadable.h:254
Holds a boost::function object to a special getter function that must meet the signature "T method()"...
Definition: GetterSetter.h:87
void reflectRead(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:565
Time()
Definition: TimeHumanReadable.h:233
Date(const Base &date)
Definition: TimeHumanReadable.h:94
Getter< DateSerializedAsIsoString > humanReadableGetter(const Date &date)
In addition to classes which can replace Date/Duration/Time, there are getters/setters/accessors for ...
Accessor< Getter< DateSerializedAsIsoString >, Setter< DateSerializedAsIsoString > > humanReadableAccessor(Date &date)
Accessor for Date using DateSerializedAsIsoString proxy.
Duration(const Base &date)
Definition: TimeHumanReadable.h:165
mira::Duration Base
Definition: TimeHumanReadable.h:161
HumanReadableSerialization::Duration< false > DurationSerializedAsHMS
Derivation of mira::Duration with serialization as hours/minutes/seconds/milli-/microseconds members...
Definition: TimeHumanReadable.h:219
Time(const Base &date)
Definition: TimeHumanReadable.h:235
When this flag is used in calls to Reflector::member(), that member is also reflected as read-only pr...
Definition: ReflectControlFlags.h:103
Derivation of mira::Time with human-readable serialization.
Definition: TimeHumanReadable.h:229
Duration()
Definition: TimeHumanReadable.h:163
A tag type used as parameter type in humanReadableGetter, humanReadableSetter, humanReadableAccessor ...
Definition: TimeHumanReadable.h:307