coral
A C++ library for distributed co-simulation
Namespaces | Classes | Functions
coral::util Namespace Reference

Misc. utilities (i.e., stuff that didn't really fit anywhere else). More...

Namespaces

 zip
 Utilities for dealing with ZIP archives.
 

Classes

class  TempDir
 An RAII object that creates a unique directory on construction and recursively deletes it again on destruction. More...
 

Functions

std::vector< std::stringCommandLine (int argc, char const *const *argv)
 Returns a string vector with the same contents as the standard C program argument array.
 
boost::optional< boost::program_options::variables_map > ParseArguments (const std::vector< std::string > &args, boost::program_options::options_description options, const boost::program_options::options_description &positionalOptions, const boost::program_options::positional_options_description &positions, std::ostream &helpOutput, const std::string &commandName, const std::string &commandDescription, const std::string &extraHelp=std::string())
 Parses program arguments and, if necessary, prints a help message. More...
 
void EncodeUint16 (std::uint16_t source, char target[2])
 Encodes a 16-bit unsigned integer using little-endian byte order.
 
void EncodeUint32 (std::uint32_t source, char target[4])
 Encodes a 32-bit unsigned integer using little-endian byte order.
 
void EncodeUint64 (std::uint64_t source, char target[8])
 Encodes a 64-bit unsigned integer using little-endian byte order.
 
std::uint16_t DecodeUint16 (const char source[2])
 Decodes a 16-bit unsigned integer using little-endian byte order.
 
std::uint32_t DecodeUint32 (const char source[4])
 Decodes a 32-bit unsigned integer using little-endian byte order.
 
std::uint64_t DecodeUint64 (const char source[8])
 Decodes a 64-bit unsigned integer using little-endian byte order.
 
int ArrayStringCmp (const char *array, size_t length, const char *stringz)
 Given a character array and its length, compares it lexicographically to a zero-terminated string. More...
 
std::string RandomUUID ()
 Returns a string that contains a random UUID.
 
std::string RandomString (size_t size, const char *charSet)
 Creates a random string. More...
 
std::string Timestamp ()
 Returns the current UTC time in the ISO 8601 "basic" format. More...
 
template<typename T1 , typename T2 >
T1 MoveAndReplace (T1 &variable, const T2 &replacement)
 Moves a value, replacing it with another one. More...
 
template<typename T >
MoveAndReplace (T &variable)
 Overload of MoveAndReplace() that replaces variable with a default-constructed value.
 
template<typename F >
void LastCall (F &f)
 Calls the given function(-like object), but only after swapping it with a default-constructed one. More...
 
template<typename Action >
ScopeGuard< Action > OnScopeExit (Action action)
 Scope guard. More...
 
void SpawnProcess (const std::string &program, const std::vector< std::string > &args)
 Starts a new process. More...
 
boost::filesystem::path ThisExePath ()
 Returns the path of the current executable. More...
 

Detailed Description

Misc. utilities (i.e., stuff that didn't really fit anywhere else).

Function Documentation

int coral::util::ArrayStringCmp ( const char *  array,
size_t  length,
const char *  stringz 
)

Given a character array and its length, compares it lexicographically to a zero-terminated string.

This is equivalent to std::strcmp, except that it only requires one of the arrays (stringz) to be null-terminated.

template<typename F >
void coral::util::LastCall ( F &  f)

Calls the given function(-like object), but only after swapping it with a default-constructed one.

This is useful for function objects that may only be called once, such as one-shot callbacks.

LastCall(f, x) is equivalent to the following:

F tmp;
swap(f, tmp);
tmp(x);

Thus, f will be left in its default-constructed state even if it throws. (However, if the default constructor throws in the first line, f will never be called at all.)

template<typename T1 , typename T2 >
T1 coral::util::MoveAndReplace ( T1 &  variable,
const T2 &  replacement 
)

Moves a value, replacing it with another one.

This function works just like std::move, except that it only works on lvalues and assigns an explicit value to the variable which is being moved from. This is inefficient for types that provide move semantics (std::move should be used for those), but useful for e.g. built-in types.

template<typename Action >
ScopeGuard<Action> coral::util::OnScopeExit ( Action  action)

Scope guard.

This function creates a generic RAII object that will execute a user-defined action on scope exit.

void Foo()
{
// DoSomething() will always be called before Foo() returns.
auto cleanup = OnScopeExit([]() { DoSomething(); });
// ...
}
boost::optional<boost::program_options::variables_map> coral::util::ParseArguments ( const std::vector< std::string > &  args,
boost::program_options::options_description  options,
const boost::program_options::options_description &  positionalOptions,
const boost::program_options::positional_options_description &  positions,
std::ostream helpOutput,
const std::string commandName,
const std::string commandDescription,
const std::string extraHelp = std::string() 
)

Parses program arguments and, if necessary, prints a help message.

This is a convenience function which takes two sets of program options, options and positionalOptions, where the former contains the ones that should be specified by the user with switches (e.g. --foo) and the latter contains the ones that are specified using "normal" (positional) arguments, and performs the following actions:

  1. Adds the --help option to options.
  2. Parses the arguments given in args, mapping them to options specified in options and positionalOptions.
  3. If the --help option was specified, or positional arguments were expected and args is empty, prints a help message and returns an empty/false object.
  4. Otherwise, returns the mapped option values.

If an empty/false object is returned, it is recommended that the program exits more or less immediately.

The help message is constructed using boost::program_options own formatted output functions, so it is recommended to include helpful option descriptions when adding options to options and positionalOptions.

Parameters
[in]argsThe command-line arguments as they were passed to the program (not including the program name).
[in]optionsThe options that should be specified with command-line switches (i.e., –switch or -s).
[in]positionalOptionsThe options that should be interpreted as positional arguments.
[in]positionsAn object that describes how to map positional arguments to options.
[in,out]helpOutputThe output stream to which a help message should be written.
[in]commandNameThe command name, as it should be displayed in the help message.
[in]commandDescriptionA description of what the command does, for the help message.
[in]extraHelpText to output below the standard help message.
Returns
A map which contains the parsed program options, or, if –help was specified or no arguments were given, an empty/false value.
std::string coral::util::RandomString ( size_t  size,
const char *  charSet 
)

Creates a random string.

This creates a string of the given size by randomly selecting characters from charSet, which must be a null-terminated string.

Exceptions
std::invalid_argumentIf charSet is null or empty.
void coral::util::SpawnProcess ( const std::string program,
const std::vector< std::string > &  args 
)

Starts a new process.

Windows warning: This function only supports a very limited form of argument quoting. The elements of args may contain spaces, but no quotation marks or other characters that are considered "special" in a Windows command line.

boost::filesystem::path coral::util::ThisExePath ( )

Returns the path of the current executable.

Exceptions
std::runtime_errorif the path could for some reason not be determined.
std::string coral::util::Timestamp ( )

Returns the current UTC time in the ISO 8601 "basic" format.

This returns a string on the form yyyymmddThhmmssZ (where the lower-case letters represent the date/time numbers).