std

std

factory std

Description:
  • Specifies standard functions of string formating, assertion verification and program line argument.

    • These functions are accessible via the aidesys:: prefix.

Methods

(static) echo(message, …parameters) → {string}

Description:
  • Returns a formatted message à-la printf.

Parameters:
Name Type Attributes Description
message string

The message, a string format à-la printf with more parameters if used.

parameters anything <repeatable>

Any message parameters.

Returns:

The formated message.

Type
string

(static) alert(conditionopt, thrown, message, …parametersopt)

Description:
  • Checks a condition at run-time and throws a fatal error exception and/or dumps a warning if not verified. Examples:

    • aidesys::alert(x == 0, "numerical-error", "division by zero"); to print a warning on stderr and stop the program if x==0.
    • aidesys::alert(x == 0, " numerical-error", "division by zero"); to print a warning on stderr without stopping the program.
    • aidesys::alert(verbose, "", "using aidesys::alert at %.0f", aidesys::now()); to simply echo a message if verbose.
Parameters:
Name Type Attributes Default Description
condition bool <optional>
true

If the condition is true, an alert is issued.

thrown string

The name of the thrown exception if the alert is raised.

  • Typical exception are:
    • illegal-argument: when a method or function receives an argument with a spurious value.
    • IO-exception: when an input or output operation fails.
    • numerical-error: when a numerical error occurs.
    • illegal-state: when detecting an unexpected condition (a bug) at some point in the code.
      • If the name starts with a space (e.g., warning) a simple warning is printed on stderr.
      • Otherwise a aide::exception expection is thrown.
      • In both case the backtrace progam stack is dumped, if compiler with -g option.
  • Typical non fatal messages are:
    • ``: to simply print information of stdout.
    • warning: (i.e., prefixed by a space) to print a warning on stderr, with the program stack.
    • warning: (i.e., prefixed by two spaces) to print a warning on stderr, without the program stack.
message string

The message, a string format à-la printf with more parameters if used.

parameters anything <optional>
<repeatable>

Any message parameters.

Throws:

A aide::exception std::exception object

  • The what() method returns "$thrown:$message" concatenating the expection label and related message.
// For instance the following construct catches a potential exception and reformulates the message:

try { aidesys::regexReplace(string, parseInput, parseOutput); } catch(std::exception& e) { aidesys::alert(true, "illegal-argument", "in symboling::RecordType::RecordType bad regex syntax: '%s'", e.what()); }

Type
exception
Returns:

The condition value, unless an exception occurs.

(static) argv() → {Array.<string>}

Description:
  • Returns the program command line.

    • Corresponds to the int main(int argc, const char *argv[]) parameters.
    • On OS not supporting the "/proc/" pseudo-filesystem (i.e., MasOS or Windows) the following construct must be used:
       int main(int argc, const char *argv[]) {
         aidesys::argv(argc, argv);
         // main routine code
       }
    
Returns:

A const std::vector<std::string> with the program command line arguments.

Type
Array.<string>

(static) system(command, nolineopt, timeoutopt) → {string}

Description:
  • Executes a system command and returns the stdout.

Parameters:
Name Type Attributes Default Description
command string

The command to execute. The command path must be absolute (e.g. /bin/pwd not pwd).

noline bool <optional>
false

If true replaces new lines and spaces by a unique space.

timeout uint <optional>
0

Optional timeout in milliseconds.

Returns:

The command result, or an error message.

Type
string

(static) wfork(runner, timeoutopt, statusopt) → {string}

Description:
  • Implements the execution of a runner in a child process, returning the stdout.

    • The function is called with prefix aidesys::wfork(·).
    • Typical usage:
    struct MyClass {
     // Runner returned status
     unsigned int status;
     // Here the runner is an instance method, with some parameter.
     int runner(String name) { cout << "Hello " + name + "!"; return 0; }
     // Calls the runner in a child process with timeout
     std::string run(String name, unsigned int timeout) {
       // The lambda construction allows to use it capturing the related parameters.
       return aidesys::wfork([this, name]() { return runner(name); }, timeout, &status);
    ../..
    
Parameters:
Name Type Attributes Default Description
runner function

A int() function that returns a status and print on stdout.

timeout uint <optional>
0

An optional timeout value, in millisecond. No timeout if the value is 0.

status uint <optional>
NULL

An optional unsigned int *status pointer to get the runner return status:

  • 0 : if no error;
  • 256: if in timeout;
  • otherwise, an error code between 1 and 255.
Returns:

The runner process sdtout.

  • The method returns after the runner process ends or is on timeout.
Type
string

(static) wfork(runner, what, delay) → {pid}

Description:
Parameters:
Name Type Description
runner function

A int() function that returns a status and print on stdout.

what char

Either:

  • 't' for the setTimeout mechanism, i.e, executes a the runner, after waiting a specified number of milliseconds (this is the default behavior).
  • 'i' for the setInterval mechanism, i.e, repeats the execution of the runner continuously, after waiting a 1st delay, while the runner() return '0' (no error).
delay uint

The timeout or interval delay in millisecond.

Returns:

The runner process pid:

  • It allows to send a signal to the child process, such as kill(timer_pid, SIGKILL); (using #include <signal.h>).
  • The method returns immediatly.
  • The child process always exit with a '0' error code.
Type
pid