MADARA  3.4.1
madara::utility Namespace Reference

Provides utility functions and classes for common tasks and needs. More...

Namespaces

 java
 Provides utility functions and classes for common Java tasks and needs.
 

Classes

class  CircularBuffer
 General purpose circular buffer container. More...
 
class  ComparisonGreaterThan
 Less than comparator for non-object-oriented types. More...
 
class  ComparisonLessThan
 Less than comparator for non-object-oriented types. More...
 
class  EpochEnforcer
 Enforces a periodic epoch. More...
 
class  LQueue
 Defines a generic "first-in/first-out" (FIFO) Abstract Data Type (ADT) using a circular linked list. More...
 
class  LQueueConstIterator
 Implements a forward iterator for LQueue type classes. More...
 
class  LQueueIterator
 Implements a forward iterator for LQueue type classes. More...
 
class  LQueueNode
 Defines a node in the LQueue that's implemented as a circular linked list. More...
 
class  LStack
 Defines a generic "last-in/first-out" (LIFO) Abstract Data Type (ADT) using a stack that's implemented as a linked list. More...
 
class  LStackConstIterator
 Implements a forward iterator for LStack type classes. More...
 
class  LStackIterator
 Implements a forward iterator for LStack type classes. More...
 
class  LStackNode
 Defines a node in the LStack that's implemented as a linked list. More...
 
class  NamedVectorCombinator
 A helper class for combining named vectors of strings into vectors of unique strings. More...
 
class  Refcounter
 This template class provides transparent reference counting of its template parameter T. More...
 
class  ScopedArray
 This template class provides transparent reference counting of its template parameter T. More...
 
class  ThreadSafeVector
 Manages a thread safe STL vector. More...
 
class  Timer
 Manages a timer for duration testing. More...
 
struct  type
 helper type for specifying template type parameters using a function argument instead of inside explicit "<...>". More...
 

Typedefs

typedef std::chrono::steady_clock Clock
 default clock type More...
 
template<typename T >
using decay_ = typename std::decay< T >::type
 Less verbose synonym for std::decay. More...
 
typedef std::chrono::nanoseconds Duration
 default clock duration More...
 
template<bool Pred, typename T = void>
using enable_if_ = typename std::enable_if< Pred, T >::type
 Less verbose synonym for std::enable_if. More...
 
typedef std::chrono::duration< double > SecondsDuration
 default clock duration More...
 
typedef Timer< std::chrono::steady_clock > TimerSteady
 
typedef std::chrono::time_point< ClockTimeValue
 time point More...
 

Functions

TimeValue add_seconds (const TimeValue &start, double seconds)
 Returns an offset of a time by seconds in double format. More...
 
bool approx_equal (double value1, double value2, double epsilon=0.0001)
 Checks two doubles for approximate equality. More...
 
MADARA_EXPORT bool begins_with (const std::string &input, const std::string &prefix)
 Check if input contains prefix at the beginning. More...
 
template<typename T >
bitmask_add (T mask, T values)
 Adds values to a bit mask. More...
 
template<typename T >
bool bitmask_check (T mask, T values)
 Returns true if mask contains values. More...
 
template<typename T >
bitmask_remove (T mask, T values)
 Removes values from a bit mask. More...
 
std::string clean_dir_name (const std::string &target)
 Substitutes the appropriate directory delimiter, which may help with portability between operating systems. More...
 
std::string & dds_topicify (std::string &input)
 Changes periods to underscores in compliance with OpenSplice needs. More...
 
bool endian_is_little ()
 
double endian_swap (double value)
 Converts a host format double precision into big endian. More...
 
int16_t endian_swap (int16_t value)
 Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian. More...
 
int32_t endian_swap (int32_t value)
 Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian. More...
 
int64_t endian_swap (int64_t value)
 Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian. More...
 
uint16_t endian_swap (uint16_t value)
 Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian. More...
 
uint32_t endian_swap (uint32_t value)
 Converts a host format uint64_t into big endian. More...
 
uint64_t endian_swap (uint64_t value)
 Converts a host format uint64_t into big endian. More...
 
MADARA_EXPORT bool ends_with (const std::string &input, const std::string &ending)
 Check if input contains a pattern at the end. More...
 
std::string expand_envs (const std::string &source)
 Expand any environment variables in a string. More...
 
MADARA_EXPORT std::string extract_filename (const std::string &name)
 Extracts the file name of an absolute or relative path. More...
 
MADARA_EXPORT std::string extract_path (const std::string &name)
 Extracts the path of a filename. More...
 
uint32_t file_crc (const std::string &filename, size_t max_block=1000000)
 Returns the crc of a file. More...
 
bool file_exists (const std::string &filename)
 Checks if a file exists. More...
 
MADARA_EXPORT bool file_from_fragments (const std::string &filename, uint32_t crc, bool delete_incomplete=true, bool delete_fragments=true)
 Builds a file from fragments that have the format: filename. More...
 
unsigned int file_size (const std::string &filename)
 Returns the size of a file. More...
 
size_t file_size (std::ifstream &input)
 Returns the size of a file stream and returns the stream in the same position as when called. More...
 
std::string file_to_string (const std::string &filename)
 Reads a file into a string. More...
 
MADARA_EXPORT bool filename_has_redirect (const std::string &filename)
 Checks the filename for abnormal redirects such as "..". More...
 
MADARA_EXPORT std::vector< int64_t > get_file_missing_fragments (const std::string &filename, uint32_t crc, size_t expected_size, int max_fragments=-1, size_t fragment_size=60000)
 Attempts to builds a file from fragments that have the format: filename. More...
 
MADARA_EXPORT size_t get_file_progress (const std::string &filename, uint32_t crc, size_t expected_size, size_t fragment_size=60000)
 Builds a file from fragments that have the format: filename. More...
 
int64_t get_time (void)
 Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time instead of real rtime. More...
 
TimeValue get_time_value (void)
 Returns a time of day as a chrono time value If simtime feature is enabled, this may be simulation time instead of real rtime. More...
 
uint32_t get_uint_version (const std::string &str_version=get_version())
 Converts a string version to a uint32. More...
 
char * get_var (const std::string &source, size_t cur, size_t &end)
 grab an environment variable value ( More...
 
std::string get_version (void)
 Gets the MADARA version number. More...
 
template<typename T >
bool greater_compare (const T &left, const T &right)
 Returns true if right < left. More...
 
template<typename T >
void heap_sort (T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
 Sorts an array with heap sort. More...
 
template<typename T >
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. More...
 
template<typename T >
std::unique_ptr< decay_< T > > into_unique (T &&val)
 Converts a typed value into a unique_ptr of a decayed type. More...
 
template<typename Func , typename Arg0 >
auto invoke_ (Func func, Arg0 &&arg0) -> decltype(std::forward< Arg0 >(arg0).*func)
 
template<typename Func , typename Arg0 , typename... Args>
auto invoke_ (Func func, Arg0 &&arg0, Args &&... args) -> decltype((std::forward< Arg0 >(arg0).*func)(std::forward< Args >(args)...))
 
template<typename Func , typename... Args>
auto invoke_ (Func func, Args &&... args) -> decltype(func(std::forward< Args >(args)...))
 
template<typename T >
constexpr bool is_arithmetic ()
 Less verbose equivalent for std::is_arithmetic. More...
 
template<typename Base , typename Derived >
constexpr bool is_base_of ()
 Less verbose equivalent for std::is_base_of. More...
 
template<typename Base , typename Derived >
constexpr bool is_base_of_decayed ()
 Composition of std::is_base_of and std::decay. More...
 
template<typename From , typename To >
constexpr bool is_convertible ()
 Less verbose equivalent for std::is_convertible. More...
 
bool is_directory (const std::string &path)
 Checks if a a path is a directory. More...
 
template<typename T >
constexpr bool is_enum ()
 Less verbose equivalent for std::is_enum. More...
 
template<typename T >
constexpr bool is_floating_point ()
 Less verbose equivalent for std::is_floating_point. More...
 
template<typename T >
constexpr bool is_int_numeric ()
 Is T arithmetic or an enum? More...
 
template<typename T >
constexpr bool is_integral ()
 Less verbose equivalent for std::is_integral. More...
 
template<typename T >
constexpr bool is_numeric ()
 Is T arithmetic or an enum? More...
 
template<typename T , typename U >
constexpr bool is_same ()
 Less verbose equivalent for std::is_same. More...
 
template<typename T , typename U >
constexpr bool is_same_decayed ()
 Composition of std::is_same and std::decay. More...
 
template<typename T >
bool less_compare (const T &left, const T &right)
 Returns true if left < right. More...
 
std::string & lower (std::string &input)
 Converts the string to lower. More...
 
int merge_hostport_identifier (std::string &key, const std::string &host, const std::string &port)
 Merges a host and port into a host:port key. More...
 
int merge_hostport_identifier (std::string &key, const std::string &host, unsigned short u_port)
 Merges a host and port into a host:port key. More...
 
template<typename T , typename... Args>
std::unique_ptr< T > mk_unique (Args &&... args)
 Creates a unique_ptr for the templated type. More...
 
int64_t nearest_int (double input)
 Rounds a double to the nearest integer. More...
 
std::pair< std::string, uint16_t > parse_address (std::string addr)
 
double rand_double (double floor=0.0, double ceiling=1.0, bool set_seed_to_time=true)
 Returns a random double between floor and ceiling. More...
 
int64_t rand_int (int64_t floor=0, int64_t ceiling=RAND_MAX, bool set_seed_to_time=true)
 Returns a random integer between a floor and ceiling. More...
 
int read_file (const std::string &filename, void *&buffer, size_t &size, bool add_zero_char=false)
 Reads a file into a provided void pointer. More...
 
bool recursive_mkdir (const std::string &path)
 
void safe_clear (std::vector< std::string > &strings)
 Safely clear a vector of STL strings when an application has been compiled with a different version of STL than the MADARA library. More...
 
Duration seconds_to_duration (double seconds)
 Returns seconds in double format as nanosecond duration. More...
 
int64_t seconds_to_nanoseconds (double seconds)
 Returns seconds in nanoseconds. More...
 
SecondsDuration seconds_to_seconds_duration (double seconds)
 Returns seconds in double format as seconds duration. More...
 
TimeValue seconds_to_time (double seconds)
 Returns seconds in double format as nanosecond since epoch. More...
 
MADARA_EXPORT bool set_thread_priority (int priority=20)
 Sets the thread priority in a FIFO scheme. More...
 
template<typename T >
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. More...
 
SecondsDuration sleep (const SecondsDuration &sleep_time)
 Sleeps for a certain amount of time. More...
 
double sleep (double sleep_time)
 Sleeps for a certain amount of time. More...
 
Duration sleep_until (TimeValue wake)
 
Duration sleep_until (uint64_t wake)
 
int split_hostport_identifier (const std::string &key, std::string &host, std::string &port)
 Splits a key of host:port into a corresponding host and port. More...
 
std::string & string_remove (std::string &input, char unwanted)
 Strips an unwanted character. More...
 
size_t string_replace (std::string &source, const std::string &old_phrase, const std::string &new_phrase, bool replace_all=true)
 Replaces an old phrase with a new phrase within a string. More...
 
std::vector< std::string > string_to_vector (const std::string &input, const std::string delimiter="\n", bool strip_whitespace=true)
 Splits a string into a vector of strings by delimiter. More...
 
std::string & strip_comments (std::string &input)
 Strips all comments (single-line and multi-line). More...
 
std::string & strip_extra_white_space (std::string &input)
 Strip whitespace from front and end of string and also condense multiple whitespace into a single space. More...
 
std::string strip_prefix (const std::string &input, const std::string &prefix)
 Strips a prefix from an input string and returns the result. More...
 
std::string & strip_white_space (std::string &input)
 Strip all whitespace. More...
 
MADARA_EXPORT void strncpy_safe (char *dst, const char *src, size_t dst_size)
 Performs a strncpy in a way that will compile without warnings. More...
 
std::string to_string_version (uint32_t version)
 Converts a MADARA uint32_t version number to human-readable. More...
 
void tokenizer (const std::string &input, const ::std::vector< std::string > &splitters, ::std::vector< std::string > &tokens, ::std::vector< std::string > &pivots)
 Split a string into tokens. More...
 
std::string & upper (std::string &input)
 Converts the string to upper. More...
 
bool wait_false (knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
 Waits on a knowledge record to be false without needing KaRL language. More...
 
bool wait_true (knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
 Waits on a knowledge record to be true without needing KaRL language. More...
 
ssize_t write_file (const std::string &filename, void *buffer, size_t size)
 Writes a file with provided contents. More...
 

Variables

static const uint64_t micro_per = milli_per * 1000
 
static const uint64_t milli_per = 1000
 
static const uint64_t nano_per = micro_per * 1000
 
static const uint64_t simtime_min_sleep = 100 * (nano_per / milli_per)
 

Detailed Description

Provides utility functions and classes for common tasks and needs.

Typedef Documentation

◆ Clock

typedef std::chrono::steady_clock madara::utility::Clock

default clock type

Definition at line 27 of file Utility.h.

◆ decay_

template<typename T >
using madara::utility::decay_ = typedef typename std::decay<T>::type

Less verbose synonym for std::decay.

Definition at line 78 of file StlHelper.h.

◆ Duration

typedef std::chrono::nanoseconds madara::utility::Duration

default clock duration

Definition at line 30 of file Utility.h.

◆ enable_if_

template<bool Pred, typename T = void>
using madara::utility::enable_if_ = typedef typename std::enable_if<Pred, T>::type

Less verbose synonym for std::enable_if.

Definition at line 74 of file StlHelper.h.

◆ SecondsDuration

typedef std::chrono::duration<double> madara::utility::SecondsDuration

default clock duration

Definition at line 33 of file Utility.h.

◆ TimerSteady

typedef Timer<std::chrono::steady_clock> madara::utility::TimerSteady

Definition at line 90 of file Timer.h.

◆ TimeValue

typedef std::chrono::time_point<Clock> madara::utility::TimeValue

time point

Definition at line 36 of file Utility.h.

Function Documentation

◆ add_seconds()

TimeValue madara::utility::add_seconds ( const TimeValue start,
double  seconds 
)
inline

Returns an offset of a time by seconds in double format.

Returns
new time of day as a chrono time value

Definition at line 272 of file Utility.inl.

◆ approx_equal()

bool madara::utility::approx_equal ( double  value1,
double  value2,
double  epsilon = 0.0001 
)
inline

Checks two doubles for approximate equality.

Doubles, unlike integers, are approximations of fractions. Consequently, two doubles cannot be checked for equality as easily and as portably as integers. To compare doubles for equality, use this function with an epsilon, which by default is accurate to the 10,000th (4 decimal places)

Parameters
value1the first value to compare, order does not matter
value2the second value to compare, order does not matter
epsilonthe minimum difference between two values before equality does not hold.

Definition at line 299 of file Utility.inl.

◆ begins_with()

bool madara::utility::begins_with ( const std::string &  input,
const std::string &  prefix 
)
inline

Check if input contains prefix at the beginning.

Parameters
inputstring to check for prefix
prefixstring to match
Returns
true if input begins with prefix. False otherwise.

Definition at line 638 of file Utility.inl.

◆ bitmask_add()

template<typename T >
T madara::utility::bitmask_add ( mask,
values 
)

Adds values to a bit mask.

Parameters
maskthe target bit mask
valuesvalues to bitwise add to mask
Returns
resulting mask

Definition at line 664 of file Utility.inl.

◆ bitmask_check()

template<typename T >
bool madara::utility::bitmask_check ( mask,
values 
)

Returns true if mask contains values.

Parameters
maskthe bit mask to check
valuesvalues to check for inclusion in mask
Returns
true if values in exist in mask, false otherwise

Definition at line 673 of file Utility.inl.

◆ bitmask_remove()

template<typename T >
T madara::utility::bitmask_remove ( mask,
values 
)

Removes values from a bit mask.

Parameters
maskthe target bit mask
valuesvalues to bitwise remove from mask
Returns
resulting mask

Definition at line 682 of file Utility.inl.

◆ clean_dir_name()

MADARA_EXPORT std::string madara::utility::clean_dir_name ( const std::string &  target)

Substitutes the appropriate directory delimiter, which may help with portability between operating systems.

Parameters
targetthe string to modify
Returns
a more compliant directory path string

Definition at line 396 of file Utility.cpp.

◆ dds_topicify()

std::string & madara::utility::dds_topicify ( std::string &  input)
inline

Changes periods to underscores in compliance with OpenSplice needs.

Convert string to lowercase.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 76 of file Utility.inl.

◆ endian_is_little()

bool madara::utility::endian_is_little ( )
inline

Definition at line 97 of file Utility.inl.

◆ endian_swap() [1/7]

double madara::utility::endian_swap ( double  value)
inline

Converts a host format double precision into big endian.

Converts a host format double into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 232 of file Utility.inl.

◆ endian_swap() [2/7]

int16_t madara::utility::endian_swap ( int16_t  value)
inline

Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian.

Converts a host format signed knowledge::KnowledgeRecord::Integer into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 218 of file Utility.inl.

◆ endian_swap() [3/7]

int32_t madara::utility::endian_swap ( int32_t  value)
inline

Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian.

Converts a host format signed knowledge::KnowledgeRecord::Integer into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 187 of file Utility.inl.

◆ endian_swap() [4/7]

int64_t madara::utility::endian_swap ( int64_t  value)
inline

Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian.

Converts a host format signed knowledge::KnowledgeRecord::Integer into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 153 of file Utility.inl.

◆ endian_swap() [5/7]

uint16_t madara::utility::endian_swap ( uint16_t  value)
inline

Converts a host format signed madara::knowledge::KnowledgeRecord::Integer into big endian.

Converts a host format signed knowledge::KnowledgeRecord::Integer into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 203 of file Utility.inl.

◆ endian_swap() [6/7]

uint32_t madara::utility::endian_swap ( uint32_t  value)
inline

Converts a host format uint64_t into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 171 of file Utility.inl.

◆ endian_swap() [7/7]

uint64_t madara::utility::endian_swap ( uint64_t  value)
inline

Converts a host format uint64_t into big endian.

Can also be used to convert back into host form.

Parameters
valuethe value to convert
Returns
the converted value

Definition at line 134 of file Utility.inl.

◆ ends_with()

bool madara::utility::ends_with ( const std::string &  input,
const std::string &  ending 
)
inline

Check if input contains a pattern at the end.

Parameters
inputstring to check for prefix
endingstring to match
Returns
true if input ends with ending. False otherwise.

Definition at line 650 of file Utility.inl.

◆ expand_envs()

MADARA_EXPORT std::string madara::utility::expand_envs ( const std::string &  source)

Expand any environment variables in a string.

Expands environment variables referenced in the string.

The environment variables must be specified as and not $var.

Parameters
sourcethe string to expand
Returns
a string with environment variables expanded

Definition at line 357 of file Utility.cpp.

◆ extract_filename()

std::string madara::utility::extract_filename ( const std::string &  name)
inline

Extracts the file name of an absolute or relative path.

Parameters
namename of the file to extract path of
Returns
the file name

Definition at line 384 of file Utility.inl.

◆ extract_path()

std::string madara::utility::extract_path ( const std::string &  name)
inline

Extracts the path of a filename.

Parameters
namename of the file to extract path of
Returns
the directory path of the file

Definition at line 374 of file Utility.inl.

◆ file_crc()

uint32_t madara::utility::file_crc ( const std::string &  filename,
size_t  max_block = 1000000 
)
inline

Returns the crc of a file.

Parameters
filenamepath and name of the file to open
max_blockmaximum block size to read when processing CRC. This can be important with large files. We provide a reasonable block size by default, but you can adjust as appropriate
Returns
size of the file

Definition at line 423 of file Utility.inl.

◆ file_exists()

bool madara::utility::file_exists ( const std::string &  filename)
inline

Checks if a file exists.

Parameters
filenamepath and name of the file to open
Returns
true if file exists. false otherwise.

Definition at line 304 of file Utility.inl.

◆ file_from_fragments()

bool madara::utility::file_from_fragments ( const std::string &  filename,
uint32_t  crc,
bool  delete_incomplete = true,
bool  delete_fragments = true 
)
inline

Builds a file from fragments that have the format: filename.

{}.crc.frag

Parameters
filenamename of the file to create
crccrc for completed file
delete_incompleteif true, delete if crc is incorrect
delete_fragmentsif true, delete fragments if crc correct
Returns
true if resulting file has correct crc

Definition at line 466 of file Utility.inl.

◆ file_size() [1/2]

unsigned int madara::utility::file_size ( const std::string &  filename)
inline

Returns the size of a file.

Parameters
filenamepath and name of the file to open
Returns
size of the file

Definition at line 395 of file Utility.inl.

◆ file_size() [2/2]

size_t madara::utility::file_size ( std::ifstream &  input)
inline

Returns the size of a file stream and returns the stream in the same position as when called.

Parameters
inputthe file stream to check
Returns
size of the stream

Definition at line 408 of file Utility.inl.

◆ file_to_string()

MADARA_EXPORT std::string madara::utility::file_to_string ( const std::string &  filename)

Reads a file into a string.

Parameters
filenamename of the file to read
Returns
contents of the file. Null string if unsuccessful.

Definition at line 324 of file Utility.cpp.

◆ filename_has_redirect()

bool madara::utility::filename_has_redirect ( const std::string &  filename)
inline

Checks the filename for abnormal redirects such as "..".

Parameters
filenamename of the file to check
Returns
true if filename contains "..", "//", or "~""

Definition at line 445 of file Utility.inl.

◆ get_file_missing_fragments()

std::vector< int64_t > madara::utility::get_file_missing_fragments ( const std::string &  filename,
uint32_t  crc,
size_t  expected_size,
int  max_fragments = -1,
size_t  fragment_size = 60000 
)
inline

Attempts to builds a file from fragments that have the format: filename.

{}.crc.frag and returns any missing fragments

Parameters
filenamename of the file to create
crccrc for completed file
expected_sizeexpected total size of the file (bytes)
max_fragmentsmaximum fragments to request (-1 means all)
fragment_sizemax fragment size for sending
Returns
the fragments that are missing

Definition at line 572 of file Utility.inl.

◆ get_file_progress()

size_t madara::utility::get_file_progress ( const std::string &  filename,
uint32_t  crc,
size_t  expected_size,
size_t  fragment_size = 60000 
)
inline

Builds a file from fragments that have the format: filename.

{}.crc.frag

Parameters
filenamename of the file to create
crccrc for completed file
expected_sizeexpected total size of the file (bytes)
fragment_sizemax fragment size for sending
Returns
size in bytes recieved of the fragments

Definition at line 528 of file Utility.inl.

◆ get_time()

int64_t madara::utility::get_time ( void  )
inline

Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time instead of real rtime.

Returns
time of day in nanoseconds

Definition at line 265 of file Utility.inl.

◆ get_time_value()

TimeValue madara::utility::get_time_value ( void  )
inline

Returns a time of day as a chrono time value If simtime feature is enabled, this may be simulation time instead of real rtime.

Returns
time of day as a chrono time value

Definition at line 256 of file Utility.inl.

◆ get_uint_version()

MADARA_EXPORT uint32_t madara::utility::get_uint_version ( const std::string &  str_version = get_version())

Converts a string version to a uint32.

To use this for an arbitrary version string, make sure it is in the format x[.x]*. Examples include 1, 1.2, 1.2.3, 1.2.3.4. Version components should not go above 255.

Parameters
str_versionthe string version to convert
Returns
the MADARA 4 byte version number

Definition at line 30 of file Utility.cpp.

◆ get_var()

char * madara::utility::get_var ( const std::string &  source,
size_t  cur,
size_t &  end 
)

grab an environment variable value (

Helper function for madara::utility::expand_envs which retrieves an environment variable.

See also
expand_envs)
Parameters
sourcethe string to expand
curthe current focus position of expand_vars
endthe end of the variable
Returns
the value of the environment variable

Definition at line 383 of file Utility.cpp.

◆ get_version()

MADARA_EXPORT std::string madara::utility::get_version ( void  )

Gets the MADARA version number.

Returns
the MADARA version number

Definition at line 23 of file Utility.cpp.

◆ greater_compare()

template<typename T >
bool madara::utility::greater_compare ( const T &  left,
const T &  right 
)

Returns true if right < left.

Definition at line 694 of file Utility.inl.

◆ heap_sort()

template<typename T >
void madara::utility::heap_sort ( T *  input,
int  size,
bool(*)(const T &left, const T &right)  comparator = greater_compare<T> 
)

Sorts an array with heap sort.

Parameters
inputthe array to heapify
sizethe size of the array
comparatora function for comparing two elements

Definition at line 739 of file Utility.inl.

◆ heapify()

template<typename T >
void madara::utility::heapify ( T *  input,
int  size,
bool(*)(const T &left, const T &right)  comparator = greater_compare<T> 
)

Builds a heap out of an array of elements.

Parameters
inputthe array to heapify
sizethe size of the array
comparatora function for comparing two elements

Definition at line 728 of file Utility.inl.

◆ into_unique()

template<typename T >
std::unique_ptr<decay_<T> > madara::utility::into_unique ( T &&  val)

Converts a typed value into a unique_ptr of a decayed type.

Definition at line 93 of file StlHelper.h.

◆ invoke_() [1/3]

template<typename Func , typename Arg0 >
auto madara::utility::invoke_ ( Func  func,
Arg0 &&  arg0 
) -> decltype( std::forward<Arg0>(arg0).*func )
inline

Definition at line 193 of file StlHelper.h.

◆ invoke_() [2/3]

template<typename Func , typename Arg0 , typename... Args>
auto madara::utility::invoke_ ( Func  func,
Arg0 &&  arg0,
Args &&...  args 
) -> decltype( (std::forward<Arg0>(arg0).*func)(std::forward<Args>(args)...) )
inline

Definition at line 197 of file StlHelper.h.

◆ invoke_() [3/3]

template<typename Func , typename... Args>
auto madara::utility::invoke_ ( Func  func,
Args &&...  args 
) -> decltype( func(std::forward<Args>(args)...) )
inline

Definition at line 201 of file StlHelper.h.

◆ is_arithmetic()

template<typename T >
constexpr bool madara::utility::is_arithmetic ( )
constexpr

Less verbose equivalent for std::is_arithmetic.

Definition at line 152 of file StlHelper.h.

◆ is_base_of()

template<typename Base , typename Derived >
constexpr bool madara::utility::is_base_of ( )
constexpr

Less verbose equivalent for std::is_base_of.

Definition at line 117 of file StlHelper.h.

◆ is_base_of_decayed()

template<typename Base , typename Derived >
constexpr bool madara::utility::is_base_of_decayed ( )
constexpr

Composition of std::is_base_of and std::decay.

Definition at line 124 of file StlHelper.h.

◆ is_convertible()

template<typename From , typename To >
constexpr bool madara::utility::is_convertible ( )
constexpr

Less verbose equivalent for std::is_convertible.

Definition at line 131 of file StlHelper.h.

◆ is_directory()

bool madara::utility::is_directory ( const std::string &  path)
inline

Checks if a a path is a directory.

Parameters
pathpath to check for directory status
Returns
true if the path is a directory

Definition at line 317 of file Utility.inl.

◆ is_enum()

template<typename T >
constexpr bool madara::utility::is_enum ( )
constexpr

Less verbose equivalent for std::is_enum.

Definition at line 159 of file StlHelper.h.

◆ is_floating_point()

template<typename T >
constexpr bool madara::utility::is_floating_point ( )
constexpr

Less verbose equivalent for std::is_floating_point.

Definition at line 145 of file StlHelper.h.

◆ is_int_numeric()

template<typename T >
constexpr bool madara::utility::is_int_numeric ( )
constexpr

Is T arithmetic or an enum?

Definition at line 173 of file StlHelper.h.

◆ is_integral()

template<typename T >
constexpr bool madara::utility::is_integral ( )
constexpr

Less verbose equivalent for std::is_integral.

Definition at line 138 of file StlHelper.h.

◆ is_numeric()

template<typename T >
constexpr bool madara::utility::is_numeric ( )
constexpr

Is T arithmetic or an enum?

Definition at line 166 of file StlHelper.h.

◆ is_same()

template<typename T , typename U >
constexpr bool madara::utility::is_same ( )
constexpr

Less verbose equivalent for std::is_same.

Definition at line 102 of file StlHelper.h.

◆ is_same_decayed()

template<typename T , typename U >
constexpr bool madara::utility::is_same_decayed ( )
constexpr

Composition of std::is_same and std::decay.

Definition at line 110 of file StlHelper.h.

◆ less_compare()

template<typename T >
bool madara::utility::less_compare ( const T &  left,
const T &  right 
)

Returns true if left < right.

Definition at line 688 of file Utility.inl.

◆ lower()

std::string & madara::utility::lower ( std::string &  input)
inline

Converts the string to lower.

Convert string to lowercase.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 89 of file Utility.inl.

◆ merge_hostport_identifier() [1/2]

MADARA_EXPORT int madara::utility::merge_hostport_identifier ( std::string &  key,
const std::string &  host,
const std::string &  port 
)

Merges a host and port into a host:port key.

Parameters
keya string containing a host:port
hostthe host in the key
portthe port in the key
Returns
0 if successful (always successful right now)

Definition at line 302 of file Utility.cpp.

◆ merge_hostport_identifier() [2/2]

MADARA_EXPORT int madara::utility::merge_hostport_identifier ( std::string &  key,
const std::string &  host,
unsigned short  u_port 
)

Merges a host and port into a host:port key.

Parameters
keya string containing a host:port
hostthe host in the key
u_portthe port in the key
Returns
0 if successful (always successful right now)

Definition at line 315 of file Utility.cpp.

◆ mk_unique()

template<typename T , typename... Args>
std::unique_ptr<T> madara::utility::mk_unique ( Args &&...  args)

Creates a unique_ptr for the templated type.

Definition at line 84 of file StlHelper.h.

◆ nearest_int()

MADARA_EXPORT int64_t madara::utility::nearest_int ( double  input)

Rounds a double to the nearest integer.

Parameters
inputreal number to round to integer
Returns
the nearest integer to the input

Definition at line 543 of file Utility.cpp.

◆ parse_address()

std::pair< std::string, uint16_t > madara::utility::parse_address ( std::string  addr)

Definition at line 797 of file Utility.cpp.

◆ rand_double()

MADARA_EXPORT double madara::utility::rand_double ( double  floor = 0.0,
double  ceiling = 1.0,
bool  set_seed_to_time = true 
)

Returns a random double between floor and ceiling.

Parameters
floorlowest possible double
ceilinghighest possible double
set_seed_to_timeif true, seed the random generator with time
Returns
a double between floor and ceiling (inclusive)

Definition at line 520 of file Utility.cpp.

◆ rand_int()

MADARA_EXPORT int64_t madara::utility::rand_int ( int64_t  floor = 0,
int64_t  ceiling = RAND_MAX,
bool  set_seed_to_time = true 
)

Returns a random integer between a floor and ceiling.

Parameters
floorlowest possible integer
ceilinghighest possible integer
set_seed_to_timeif true, seed the random generator with time
Returns
a integer between floor and ceiling (inclusive)

Definition at line 537 of file Utility.cpp.

◆ read_file()

MADARA_EXPORT int madara::utility::read_file ( const std::string &  filename,
void *&  buffer,
size_t &  size,
bool  add_zero_char = false 
)

Reads a file into a provided void pointer.

The void pointer will point to an allocated buffer that the user will need to delete.

Parameters
filenamethe name of the file
buffera buffer that contains the contents of the file
sizethe size of the allocated buffer (will change)
add_zero_charadd a zero char to the end of the buffer
Returns
zero if successful

Definition at line 418 of file Utility.cpp.

◆ recursive_mkdir()

bool madara::utility::recursive_mkdir ( const std::string &  path)
inline

Definition at line 336 of file Utility.inl.

◆ safe_clear()

MADARA_EXPORT void madara::utility::safe_clear ( std::vector< std::string > &  strings)

Safely clear a vector of STL strings when an application has been compiled with a different version of STL than the MADARA library.

Parameters
stringsthe vector to clear

Definition at line 811 of file Utility.cpp.

◆ seconds_to_duration()

Duration madara::utility::seconds_to_duration ( double  seconds)
inline

Returns seconds in double format as nanosecond duration.

Returns
duration for usage with TimeValues

Definition at line 277 of file Utility.inl.

◆ seconds_to_nanoseconds()

int64_t madara::utility::seconds_to_nanoseconds ( double  seconds)
inline

Returns seconds in nanoseconds.

Returns
nanoseconds that can be added to another timestamp

Definition at line 294 of file Utility.inl.

◆ seconds_to_seconds_duration()

SecondsDuration madara::utility::seconds_to_seconds_duration ( double  seconds)
inline

Returns seconds in double format as seconds duration.

Returns
duration for usage with TimeValues

Definition at line 283 of file Utility.inl.

◆ seconds_to_time()

TimeValue madara::utility::seconds_to_time ( double  seconds)
inline

Returns seconds in double format as nanosecond since epoch.

Returns
converted time

Definition at line 288 of file Utility.inl.

◆ set_thread_priority()

bool madara::utility::set_thread_priority ( int  priority = 20)
inline

Sets the thread priority in a FIFO scheme.

Parameters
prioritythe priority to attempt to set
Returns
true if set call was successful

Definition at line 26 of file Utility.inl.

◆ sift_down()

template<typename T >
void madara::utility::sift_down ( T *  input,
int  start,
int  end,
bool(*)(const T &left, const T &right)  comparator = greater_compare<T> 
)

Sifts elements down a heap according to a comparator.

Parameters
inputthe array to heapify
startindex to start from
endindex that signifies end of range to sift
comparatora function for comparing two elements

Definition at line 700 of file Utility.inl.

◆ sleep() [1/2]

MADARA_EXPORT SecondsDuration madara::utility::sleep ( const SecondsDuration sleep_time)

Sleeps for a certain amount of time.

Unlike other sleep timers this will sleep for the amount of time regardless of OS interrupts

Parameters
sleep_timetime to sleep for

Definition at line 563 of file Utility.cpp.

◆ sleep() [2/2]

MADARA_EXPORT double madara::utility::sleep ( double  sleep_time)

Sleeps for a certain amount of time.

Unlike other sleep timers this will sleep for the amount of time regardless of OS interrupts

Parameters
sleep_timetime in seconds to sleep for

Definition at line 555 of file Utility.cpp.

◆ sleep_until() [1/2]

MADARA_EXPORT Duration madara::utility::sleep_until ( TimeValue  wake)

Definition at line 612 of file Utility.cpp.

◆ sleep_until() [2/2]

MADARA_EXPORT Duration madara::utility::sleep_until ( uint64_t  wake)

Definition at line 658 of file Utility.cpp.

◆ split_hostport_identifier()

MADARA_EXPORT int madara::utility::split_hostport_identifier ( const std::string &  key,
std::string &  host,
std::string &  port 
)

Splits a key of host:port into a corresponding host and port.

Parameters
keya string containing a host:port
hostthe host in the key
portthe port in the key
Returns
1 if there was no ":". 0 if both host and port are set.

Definition at line 278 of file Utility.cpp.

◆ string_remove()

MADARA_EXPORT std::string & madara::utility::string_remove ( std::string &  input,
char  unwanted 
)

Strips an unwanted character.

This function will modify and return the input, so make a copy if needed.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
unwantedthe character value to remove from input
Returns
a reference to the modified input

Definition at line 149 of file Utility.cpp.

◆ string_replace()

MADARA_EXPORT size_t madara::utility::string_replace ( std::string &  source,
const std::string &  old_phrase,
const std::string &  new_phrase,
bool  replace_all = true 
)

Replaces an old phrase with a new phrase within a string.

Parameters
sourcesource string to change
old_phraseold phrase in the source string to replace
new_phrasenew phrase to replace the old phrase
replace_allif true, replace all instances of old phrase. if false, replace first instance of old phrase.
Returns
the number of replacements made

Definition at line 170 of file Utility.cpp.

◆ string_to_vector()

std::vector< std::string > madara::utility::string_to_vector ( const std::string &  input,
const std::string  delimiter = "\n",
bool  strip_whitespace = true 
)
inline

Splits a string into a vector of strings by delimiter.

See also
utility::tokenizer, which does the tokenization.
Parameters
inputthe text to parse
delimiterthe delimiter to use as a splitter
strip_whitespaceif true, remove whitespace around tokens

Definition at line 111 of file Utility.inl.

◆ strip_comments()

MADARA_EXPORT std::string & madara::utility::strip_comments ( std::string &  input)

Strips all comments (single-line and multi-line).

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 194 of file Utility.cpp.

◆ strip_extra_white_space()

MADARA_EXPORT std::string & madara::utility::strip_extra_white_space ( std::string &  input)

Strip whitespace from front and end of string and also condense multiple whitespace into a single space.

Strips whitespace from front and end of string and also condenses multiple whitespace into a single space.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 85 of file Utility.cpp.

◆ strip_prefix()

std::string madara::utility::strip_prefix ( const std::string &  input,
const std::string &  prefix 
)
inline

Strips a prefix from an input string and returns the result.

Convert string to uppercase.

The prefix should be at the front of the input. This does not perform checks for speed reasons.

Parameters
inputthe string to change
prefixthe start of the string
Returns
a reference to the modified input

Definition at line 60 of file Utility.inl.

◆ strip_white_space()

MADARA_EXPORT std::string & madara::utility::strip_white_space ( std::string &  input)

Strip all whitespace.

Strips all whitespace characters from a string.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 124 of file Utility.cpp.

◆ strncpy_safe()

MADARA_EXPORT void madara::utility::strncpy_safe ( char *  dst,
const char *  src,
size_t  dst_size 
)

Performs a strncpy in a way that will compile without warnings.

Parameters
srcthe c string to copy
dstthe target buffer. Will be null terminated
dst_sizethe number of bytes in the dst buffer

Definition at line 376 of file Utility.cpp.

◆ to_string_version()

MADARA_EXPORT std::string madara::utility::to_string_version ( uint32_t  version)

Converts a MADARA uint32_t version number to human-readable.

Parameters
versionthe 4 byte version number
Returns
the stringified version number

Definition at line 64 of file Utility.cpp.

◆ tokenizer()

MADARA_EXPORT void madara::utility::tokenizer ( const std::string &  input,
const ::std::vector< std::string > &  splitters,
::std::vector< std::string > &  tokens,
::std::vector< std::string > &  pivot_list 
)

Split a string into tokens.

Splits an input string into tokens.

Parameters
inputa string to be split by splitters
splittersa vector of strings to look for in input
tokensthe sections in between splitters
pivot_listthe actual splitters that were found between the tokens

Definition at line 224 of file Utility.cpp.

◆ upper()

std::string & madara::utility::upper ( std::string &  input)
inline

Converts the string to upper.

Convert string to uppercase.

This function will modify and return the input, so make a copy if needed.

Parameters
inputthe string to change
Returns
a reference to the modified input

Definition at line 67 of file Utility.inl.

◆ wait_false()

MADARA_EXPORT bool madara::utility::wait_false ( knowledge::KnowledgeBase knowledge,
const std::string &  variable,
const knowledge::WaitSettings settings = knowledge::WaitSettings() 
)

Waits on a knowledge record to be false without needing KaRL language.

Parameters
knowledgethe knowledge base
variablethe variable to wait on
settingsthe Wait Settings. Only uses poll_frequency and max_wait.

Definition at line 730 of file Utility.cpp.

◆ wait_true()

MADARA_EXPORT bool madara::utility::wait_true ( knowledge::KnowledgeBase knowledge,
const std::string &  variable,
const knowledge::WaitSettings settings = knowledge::WaitSettings() 
)

Waits on a knowledge record to be true without needing KaRL language.

Parameters
knowledgethe knowledge base
variablethe variable to wait on
settingsthe Wait Settings. Only uses poll_frequency and max_wait.

Definition at line 663 of file Utility.cpp.

◆ write_file()

MADARA_EXPORT ssize_t madara::utility::write_file ( const std::string &  filename,
void *  buffer,
size_t  size 
)

Writes a file with provided contents.

Parameters
filenamethe name of the file
buffera buffer that contains the contents of the file
sizethe size of the allocated buffer
Returns
amount of bytes written (-1 if unsuccessful)

Definition at line 487 of file Utility.cpp.

Variable Documentation

◆ micro_per

const uint64_t madara::utility::micro_per = milli_per * 1000
static

Definition at line 251 of file Utility.inl.

◆ milli_per

const uint64_t madara::utility::milli_per = 1000
static

Definition at line 250 of file Utility.inl.

◆ nano_per

const uint64_t madara::utility::nano_per = micro_per * 1000
static

Definition at line 252 of file Utility.inl.

◆ simtime_min_sleep

const uint64_t madara::utility::simtime_min_sleep = 100 * (nano_per / milli_per)
static

Definition at line 254 of file Utility.inl.