MADARA  3.2.3
Utility.inl
Go to the documentation of this file.
1 #ifndef _MADARA_UTILITY_INL_
2 #define _MADARA_UTILITY_INL_
3 
4 #include "Utility.h"
5 #include "SimTime.h"
6 
7 #ifdef _WIN32
8  #include "madara/Boost.h"
9  #include "boost/asio.hpp"
10 #else
11  #include <pthread.h>
12 #endif
13 
14 namespace madara { namespace utility {
15 
16 inline bool
17 set_thread_priority (int priority)
18 {
19  bool result = false;
20 
21  #ifdef _WIN32
22 
23  if (SetPriorityClass (GetCurrentProcess (), HIGH_PRIORITY_CLASS))
24  {
25  // in Windows, normalize for highest priority
26  if (priority > THREAD_PRIORITY_HIGHEST)
27  priority = THREAD_PRIORITY_HIGHEST;
28 
29  if (SetThreadPriority (GetCurrentThread (), priority))
30  {
31  result = true;
32  }
33  }
34 
35  #else
36  // assume POSIX
37  sched_param sch;
38  int policy;
39  pthread_getschedparam (pthread_self (), &policy, &sch);
40  sch.sched_priority = priority;
41  if (0 == pthread_setschedparam (pthread_self (), SCHED_FIFO, &sch))
42  {
43  result = true;
44  }
45  #endif
46 
47  return result;
48 }
49 
51 inline std::string
53 const std::string & input, const std::string & prefix)
54 {
55  return input.substr (prefix.size ());
56 }
57 
58 
60 inline std::string &
62 {
63  for (std::string::iterator cur = input.begin ();
64  cur != input.end (); ++cur)
65  *cur = toupper (*cur);
66 
67  return input;
68 }
69 
71 inline std::string &
73 {
74  for (std::string::iterator cur = input.begin ();
75  cur != input.end (); ++cur)
76  {
77  // change periods to _
78  if (*cur == '.')
79  *cur = '_';
80  }
81 
82  return input;
83 }
84 
86 inline std::string &
88 {
89  for (std::string::iterator cur = input.begin ();
90  cur != input.end (); ++cur)
91  *cur = tolower (*cur);
92 
93  return input;
94 }
95 
96 inline bool
98 {
99  static const auto detect_little_endian = []() {
100  uint16_t tester = 1;
101  char copy[2];
102  static_assert(sizeof(uint16_t) == sizeof(copy), "uint16_t isn't 2 bytes!");
103  memcpy(copy, (char*)&tester, sizeof(copy));
104  return copy[0] != 1;
105  };
106 
107  static const bool ret = detect_little_endian ();
108  return ret;
109 }
110 
114 inline uint64_t
115 endian_swap (uint64_t value)
116 {
117  // if host is little endian, then we have work to do
118  if (endian_is_little ())
119  {
120  value = ((value << 8) & 0xFF00FF00FF00FF00ULL )
121  | ((value >> 8) & 0x00FF00FF00FF00FFULL );
122  value = ((value << 16) & 0xFFFF0000FFFF0000ULL )
123  | ((value >> 16) & 0x0000FFFF0000FFFFULL );
124  return (value << 32) | (value >> 32);
125  }
126 
127  return value;
128 }
129 
133 inline int64_t
134 endian_swap (int64_t value)
135 {
136  // if host is little endian, then we have work to do
137  if (endian_is_little ())
138  {
139  value = ((value << 8) & 0xFF00FF00FF00FF00ULL )
140  | ((value >> 8) & 0x00FF00FF00FF00FFULL );
141  value = ((value << 16) & 0xFFFF0000FFFF0000ULL )
142  | ((value >> 16) & 0x0000FFFF0000FFFFULL );
143  return (value << 32) | ((value >> 32) & 0xFFFFFFFFULL);
144  }
145 
146  return value;
147 }
148 
149 
153 inline uint32_t
154 endian_swap (uint32_t value)
155 {
156  // if host is little endian, then we have work to do
157  if (endian_is_little ())
158  {
159  value = ((value << 8) & 0xFF00FF00 ) |
160  ((value >> 8) & 0xFF00FF );
161  return (value << 16) | (value >> 16);
162  }
163 
164  return value;
165 }
166 
170 inline int32_t
171 endian_swap (int32_t value)
172 {
173  // if host is little endian, then we have work to do
174  if (endian_is_little ())
175  {
176  value = ((value << 8) & 0xFF00FF00) |
177  ((value >> 8) & 0xFF00FF );
178  return (value << 16) | ((value >> 16) & 0xFFFF);
179  }
180 
181  return value;
182 }
183 
187 inline uint16_t
188 endian_swap (uint16_t value)
189 {
190  // if host is little endian, then we have work to do
191  if (endian_is_little ())
192  {
193  return ((value << 8) & 0xFFFF) | (value >> 8);
194  }
195 
196  return value;
197 }
198 
202 inline int16_t
203 endian_swap (int16_t value)
204 {
205  // if host is little endian, then we have work to do
206  if (endian_is_little ())
207  {
208  return ((value << 8) & 0xFFFF) | (value >> 8);
209  }
210 
211  return value;
212 }
213 
217 inline double
218 endian_swap (double orig)
219 {
220  // if host is little endian, then we have work to do
221  if (endian_is_little ())
222  {
223  uint64_t value;
224  memcpy(&value, &orig, sizeof(value));
225 
226  value = endian_swap(value);
227 
228  double result;
229  memcpy (&result, &value, sizeof(result));
230  return result;
231  }
232 
233  return orig;
234 }
235 
236 static const uint64_t milli_per = 1000;
237 static const uint64_t micro_per = milli_per * 1000;
238 static const uint64_t nano_per = micro_per * 1000;
239 
240 static const uint64_t simtime_min_sleep = 100 * (nano_per / milli_per);
241 
242 inline TimeValue
244 {
245 #ifndef MADARA_FEATURE_SIMTIME
246  return Clock::now ();
247 #else
248  return TimeValue (std::chrono::nanoseconds (SimTime::time ()));
249 #endif
250 }
251 
252 inline int64_t
253 get_time (void)
254 {
255  auto current_time = get_time_value ();
256  auto epoch = current_time.time_since_epoch ();
257  return std::chrono::duration_cast<Duration> (epoch).count ();
258 }
259 
260 inline TimeValue
261 add_seconds (const TimeValue & start, double seconds)
262 {
263  return start + seconds_to_duration (seconds);
264 }
265 
266 inline Duration seconds_to_duration (double seconds)
267 {
268  return std::chrono::duration_cast<Duration> (
269  seconds_to_seconds_duration (seconds));
270 }
271 
273 {
274  return std::chrono::duration<double> (seconds);
275 }
276 
277 inline TimeValue seconds_to_time (double seconds)
278 {
279  return TimeValue (seconds_to_duration (seconds));
280 }
281 
282 inline bool approx_equal (
283  double value1, double value2, double epsilon)
284 {
285  return std::abs (value1 - value2) < epsilon;
286 }
287 
288 inline bool
289 file_exists (const std::string & filename)
290 {
291  if (FILE * file = fopen (filename.c_str(), "r"))
292  {
293  fclose(file);
294  return true;
295  }
296  else
297  {
298  return false;
299  }
300 }
301 
302 inline unsigned int
303 file_size (const std::string & filename)
304 {
305  unsigned int size = 0;
306  if (FILE * file = fopen (filename.c_str(), "r"))
307  {
308  fseek (file, 0L, SEEK_END);
309  size = ftell (file);
310  fclose (file);
311  }
312 
313  return size;
314 }
315 
316 inline
317 bool begins_with (const std::string & input,
318  const std::string & prefix)
319 {
320  bool result = false;
321 
322  if (prefix.length () <= input.length ())
323  {
324  result = input.substr (0, prefix.length ()) == prefix;
325  }
326 
327  return result;
328 }
329 
330 inline
331 bool ends_with (const std::string & input,
332  const std::string & match)
333 {
334  bool result = false;
335 
336  if (match.length () <= input.length ())
337  {
338  result =
339  input.substr (input.size () - match.length (), match.length ()) == match;
340  }
341 
342  return result;
343 }
344 
345 
346 template <typename T>
347 T bitmask_add (T mask, T values)
348 {
349  return mask | values;
350 }
351 
355 template <typename T>
356 bool bitmask_check (T mask, T values)
357 {
358  return (mask & values) > 0;
359 }
360 
364 template <typename T>
365 T bitmask_remove (T mask, T values)
366 {
367  return mask & ~values;
368 }
369 
370 
371 template <typename T>
372 bool
373 less_compare (const T & left, const T & right)
374 {
375  return left < right;
376 }
377 
378 template <typename T>
379 bool
380 greater_compare (const T & left, const T & right)
381 {
382  return right < left;
383 }
384 
385 template <typename T>
386 void
387 sift_down (T * input, int start, int end,
388  bool (*comparator) (const T & left, const T & right))
389 {
390  int root = start;
391  for (int child = root * 2 + 1; child <= end; child = root * 2 + 1)
392  {
393  int swap = root;
394 
395  // check left child
396  if (comparator (input[child], input[swap]))
397  swap = child;
398 
399  // check right child
400  ++child;
401  if (child <= end && comparator (input[child], input[swap]))
402  swap = child;
403 
404  if (swap != root)
405  {
406  std::swap (input[root], input[swap]);
407  root = swap;
408  }
409  else
410  break;
411  }
412 }
413 
414 template <typename T>
415 void
416 heapify (T * input, int size,
417  bool (*comparator) (const T & left, const T & right))
418 {
419  // start at lowest parent node and work our way back
420  for (int start = (size - 2) / 2; start >= 0; --start)
421  {
422  sift_down (input, start, size - 1, comparator);
423  }
424 }
425 
426 template <typename T>
427 void
428 heap_sort (T * input, int size,
429  bool (*comparator) (const T & left, const T & right))
430 {
431  heapify (input, size, comparator);
432 
433  for (int end = size - 1; end > 0; --end)
434  {
435  std::swap (input[end], input[0]);
436  sift_down (input, 0, end - 1);
437  }
438 }
439 
440 } }
441 
442 #endif // _MADARA_UTILITY_INL_
443 
std::chrono::duration< double > SecondsDuration
default clock duration
Definition: Utility.h:33
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
Definition: Utility.inl:356
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
Definition: Utility.inl:261
static const uint64_t simtime_min_sleep
Definition: Utility.inl:240
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
Definition: Utility.inl:253
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
Definition: Utility.inl:272
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
Definition: Utility.inl:17
T bitmask_add(T mask, T values)
Adds values to a bit mask.
Definition: Utility.inl:347
std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
Definition: Utility.inl:52
unsigned int file_size(const std::string &filename)
Returns the size of a file.
Definition: Utility.inl:303
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
Definition: Utility.inl:266
std::string & lower(std::string &input)
Converts the string to lower.
Definition: Utility.inl:87
void sift_down(T *input, int start, int end, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sifts elements down a heap according to a comparator.
Definition: Utility.inl:387
std::chrono::time_point< Clock > TimeValue
time point
Definition: Utility.h:36
static const uint64_t micro_per
Definition: Utility.inl:237
std::string & upper(std::string &input)
Converts the string to upper.
Definition: Utility.inl:61
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
Definition: Utility.inl:380
static const uint64_t nano_per
Definition: Utility.inl:238
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
Definition: Utility.inl:428
TimeValue get_time_value(void)
Returns a time of day as a chrono time value If simtime feature is enabled, this may be simulation ti...
Definition: Utility.inl:243
static struct madara::knowledge::tags::string_t string
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.inl:115
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
Definition: Utility.inl:72
void heapify(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Builds a heap out of an array of elements.
Definition: Utility.inl:416
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
Definition: Utility.inl:277
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Definition: Utility.inl:365
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
bool endian_is_little()
Definition: Utility.inl:97
bool file_exists(const std::string &filename)
Checks if a file exists.
Definition: Utility.inl:289
bool less_compare(const T &left, const T &right)
Returns true if left < right.
Definition: Utility.inl:373
Copyright (c) 2015 Carnegie Mellon University.
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:317
static const uint64_t milli_per
Definition: Utility.inl:236
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
Definition: Utility.inl:282
std::chrono::nanoseconds Duration
default clock duration
Definition: Utility.h:30
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
Definition: Utility.inl:331