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) → {bool}

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, "illegal-state", "division by zero"); to print a warning on stderr and stop the program if x==0.
    • aidesys::alert(x == 0, " illegal-state", "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. <<<<<<< HEAD
  • Fatal error, Warning and Notice: If the thrown name, =======
  • Level of alert: If the thrown name,

f65afe79662256aa0532f3b74b9d2afae14f50ce

  • Fatal error: if the name does not start wih a space, e.g., error, then an aide::exception expection is thrown, dumping the program stack, if compiled with -g option,
    • and the exception what() method returns "$thrown:$message" concatenating expection label and related message;
  • Warning with stack dump: if the name is prefixed with one space, e.g., warning, then the message is printed on stderr, with the program stack, if compiled with -g option.
  • Warning without stack dump: is prefixed with two spaces, e.g., warning, then the message is printed on stderr, without the program stack.
  • Notice: if the name is the empty string, the message is a notice, printed on stderr. <<<<<<< HEAD =======
  • Signal handling: If the program source code calls alert() at least once, the signals such as SIGSEGV (Segmentation fault) or SIGFPE are handled and the stack is dump before exiting, e.g.:
  aidesys::alert(); // does nothing but installing signal handling
  double x = std::numeric_limits < double > ::signaling_NaN(); printf("x+1: %g\n", x + 1); // Uses a NaN value

generates a numerical-error from the SIGFPE signal.

f65afe79662256aa0532f3b74b9d2afae14f50ce

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

// 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());
}
  • Note: A numerical-error can NOT be catched in a simple try { } catch( ) { } construct because it is raised by a SIGFPE signal, thus outside the main program flow, and it is strongly recommended to stop the execution after such an error, because the variable state is spurious. If it is really important to catch such an exception, the following construct is to be used:
    try {
      CATCH_SIGFPE(
      x = std::numeric_limits < double > ::signaling_NaN(); // Considering signaling NaN values …
        y = x + 1; // … generates a floating-point invalid operation numerical-error as soon as used.
    );
    } catch(std::exception& e) {
       // Some suitable action
    }
Type
exception
Returns:

The condition true or false value, unless an exception occurs.

Type
bool

(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, what, delay) → {pid}

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

    • The function corresponds to setTimeout() and setInterval() javascript timing methods.
    • Its implementation is quite straightforward and provides a very basic example of using the C/C++ fork mechanism.
    • The method returns immediatly.
    • The child process always exit with a '0' error code.
    • All childs processes are properly killed when program stops.
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, using wfork_kill(pid);
Type
pid

(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