1 #ifndef _MADARA_UTILITY_INL_ 2 #define _MADARA_UTILITY_INL_ 8 #include "madara/Boost.h" 9 #include "boost/asio.hpp" 23 if (SetPriorityClass (GetCurrentProcess (), HIGH_PRIORITY_CLASS))
26 if (priority > THREAD_PRIORITY_HIGHEST)
27 priority = THREAD_PRIORITY_HIGHEST;
29 if (SetThreadPriority (GetCurrentThread (), priority))
39 pthread_getschedparam (pthread_self (), &policy, &sch);
40 sch.sched_priority = priority;
41 if (0 == pthread_setschedparam (pthread_self (), SCHED_FIFO, &sch))
55 return input.substr (prefix.size ());
63 for (std::string::iterator cur = input.begin ();
64 cur != input.end (); ++cur)
65 *cur = toupper (*cur);
74 for (std::string::iterator cur = input.begin ();
75 cur != input.end (); ++cur)
89 for (std::string::iterator cur = input.begin ();
90 cur != input.end (); ++cur)
91 *cur = tolower (*cur);
99 static const auto detect_little_endian = []() {
102 static_assert(
sizeof(uint16_t) ==
sizeof(copy),
"uint16_t isn't 2 bytes!");
103 memcpy(copy, (
char*)&tester,
sizeof(copy));
107 static const bool ret = detect_little_endian ();
120 value = ((value << 8) & 0xFF00FF00FF00FF00ULL )
121 | ((value >> 8) & 0x00FF00FF00FF00FFULL );
122 value = ((value << 16) & 0xFFFF0000FFFF0000ULL )
123 | ((value >> 16) & 0x0000FFFF0000FFFFULL );
124 return (value << 32) | (value >> 32);
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);
159 value = ((value << 8) & 0xFF00FF00 ) |
160 ((value >> 8) & 0xFF00FF );
161 return (value << 16) | (value >> 16);
176 value = ((value << 8) & 0xFF00FF00) |
177 ((value >> 8) & 0xFF00FF );
178 return (value << 16) | ((value >> 16) & 0xFFFF);
193 return ((value << 8) & 0xFFFF) | (value >> 8);
208 return ((value << 8) & 0xFFFF) | (value >> 8);
224 memcpy(&value, &orig,
sizeof(value));
229 memcpy (&result, &value,
sizeof(result));
245 #ifndef MADARA_FEATURE_SIMTIME 246 return Clock::now ();
248 return TimeValue (std::chrono::nanoseconds (SimTime::time ()));
256 auto epoch = current_time.time_since_epoch ();
257 return std::chrono::duration_cast<
Duration> (epoch).count ();
268 return std::chrono::duration_cast<
Duration> (
274 return std::chrono::duration<double> (seconds);
283 double value1,
double value2,
double epsilon)
285 return std::abs (value1 - value2) < epsilon;
291 if (FILE * file = fopen (filename.c_str(),
"r"))
305 unsigned int size = 0;
306 if (FILE * file = fopen (filename.c_str(),
"r"))
308 fseek (file, 0L, SEEK_END);
322 if (prefix.length () <= input.length ())
324 result = input.substr (0, prefix.length ()) == prefix;
336 if (match.length () <= input.length ())
339 input.substr (input.size () - match.length (), match.length ()) == match;
346 template <
typename T>
349 return mask | values;
355 template <
typename T>
358 return (mask & values) > 0;
364 template <
typename T>
367 return mask & ~values;
371 template <
typename T>
378 template <
typename T>
385 template <
typename T>
388 bool (*comparator) (
const T & left,
const T & right))
391 for (
int child = root * 2 + 1; child <= end; child = root * 2 + 1)
396 if (comparator (input[child], input[swap]))
401 if (child <= end && comparator (input[child], input[swap]))
406 std::swap (input[root], input[swap]);
414 template <
typename T>
417 bool (*comparator) (
const T & left,
const T & right))
420 for (
int start = (size - 2) / 2; start >= 0; --start)
422 sift_down (input, start, size - 1, comparator);
426 template <
typename T>
429 bool (*comparator) (
const T & left,
const T & right))
431 heapify (input, size, comparator);
433 for (
int end = size - 1; end > 0; --end)
435 std::swap (input[end], input[0]);
442 #endif // _MADARA_UTILITY_INL_ std::chrono::duration< double > SecondsDuration
default clock duration
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
static const uint64_t simtime_min_sleep
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
T bitmask_add(T mask, T values)
Adds values to a bit mask.
std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
unsigned int file_size(const std::string &filename)
Returns the size of a file.
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
std::string & lower(std::string &input)
Converts the string to lower.
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.
std::chrono::time_point< Clock > TimeValue
time point
static const uint64_t micro_per
std::string & upper(std::string &input)
Converts the string to upper.
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
static const uint64_t nano_per
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
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...
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
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.
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Provides utility functions and classes for common tasks and needs.
bool file_exists(const std::string &filename)
Checks if a file exists.
bool less_compare(const T &left, const T &right)
Returns true if left < right.
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.
static const uint64_t milli_per
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
std::chrono::nanoseconds Duration
default clock duration
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.